Java Zip File zip(final File zipFile, final File... files)

Here you can find the source of zip(final File zipFile, final File... files)

Description

zip

License

Open Source License

Declaration

private static void zip(final File zipFile, final File... files) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFFER = 2048;

    private static void zip(final File zipFile, final File... files) throws IOException {
        try (final FileOutputStream fos = new FileOutputStream(zipFile);
                final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))) {
            final byte[] data = new byte[BUFFER];

            for (final File file : files) {
                try (final FileInputStream fis = new FileInputStream(file);
                        final BufferedInputStream origin = new BufferedInputStream(fis, BUFFER)) {
                    final ZipEntry entry = new ZipEntry(file.getName());
                    zos.putNextEntry(entry);

                    int readCount;
                    while ((readCount = origin.read(data, 0, BUFFER)) != -1) {
                        zos.write(data, 0, readCount);
                    }//  w  ww .  j  a va 2s.  co m
                }
            }
        }
    }
}

Related

  1. zip(File theFileToZip)
  2. zip(File toZip, File outFile)
  3. zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)
  4. zip(final File dir, final File target)
  5. zip(final File tempLocation, final File targetZipFile)
  6. zip(final File zipFile, final File[] files)
  7. zip(final File[] filesToZip, final File zipFile)
  8. zip(final Map files)
  9. zip(final String fileName, final byte[] fileContent)