Java Zip File List zipFiles(File[] listFiles2Zip, File output)

Here you can find the source of zipFiles(File[] listFiles2Zip, File output)

Description

zip Files

License

Open Source License

Declaration

public static void zipFiles(File[] listFiles2Zip, File output) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFiles(File[] listFiles2Zip, File output) throws IOException {
        // These are the files to include in the ZIP file

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));

        // Compress the files
        for (File actualFile : listFiles2Zip) {

            FileInputStream in = new FileInputStream(actualFile);

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(actualFile.getName()));

            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);//  w  w w.  j  a  va  2  s . c om
            }

            // Complete the entry
            out.closeEntry();
            in.close();
        }

        // Complete the ZIP file
        out.close();
    }
}

Related

  1. zipFiles(Collection resFileList, File zipFile)
  2. zipFiles(File file, JarOutputStream jos, String pathName)
  3. zipFiles(File output, File root, List fileList)
  4. zipFiles(File rootDir, File currDir, ZipOutputStream zos)
  5. zipFiles(File[] dirList, ZipOutputStream zos)
  6. zipFiles(final File outputFile, final File[] files)
  7. zipFiles(List files, File output)
  8. zipFiles(List files, OutputStream os)
  9. zipFiles(List srcfile, File zipfile)