Java Zip File List zipFiles(String filename, String[] files)

Here you can find the source of zipFiles(String filename, String[] files)

Description

zip Files

License

Open Source License

Declaration

public static void zipFiles(String filename, String[] files) throws IOException 

Method Source Code


//package com.java2s;
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 {
    public static void zipFiles(String filename, String[] files) throws IOException {
        ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));

        byte[] buffer = new byte[10000];

        for (int i = 0; i < files.length; i++) {
            File f = new File(files[i]);

            if (f.exists()) {
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(files[i]));
                zip.putNextEntry(new ZipEntry(files[i]));

                boolean done = false;

                while (!done) {
                    int num = in.read(buffer);

                    if (num > 0) {
                        zip.write(buffer, 0, num);
                    }//from   w w w. j  a  v  a  2 s .  c o m

                    done = (num < buffer.length);
                }

                in.close();
            }
        }

        zip.flush();
        zip.close();
    }
}

Related

  1. zipFiles(final File outputFile, final File[] files)
  2. zipFiles(List files, File output)
  3. zipFiles(List files, OutputStream os)
  4. zipFiles(List srcfile, File zipfile)
  5. ZipFiles(String destination, Vector files)
  6. zipFiles(String files[], String fielPath)
  7. zipFiles(String filesPathToZip, String pathToSave)
  8. zipFiles(String output, String sDir, String sSearch)
  9. zipFiles(String output_dir, List files)