Java Zip File List zip(String[] sourceFiles, String zipFile, String directory)

Here you can find the source of zip(String[] sourceFiles, String zipFile, String directory)

Description

zip

License

Open Source License

Declaration

public static void zip(String[] sourceFiles, String zipFile, String directory) 

Method Source Code


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

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 zip(String[] sourceFiles, String zipFile, String directory) {
        try {/*w  w w  . jav a 2s .c o m*/
            zipFile = directory + File.separatorChar + zipFile;
            // create byte buffer
            byte[] buffer = new byte[65535];

            int progress = 0;

            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);

            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

            for (int i = 0; i < sourceFiles.length; i++) {
                sourceFiles[i] = directory + File.separatorChar + sourceFiles[i];
                File srcFile = new File(sourceFiles[i]);

                FileInputStream fileInputStream = new FileInputStream(srcFile);

                // begin writing a new ZIP entry, positions the stream to the start of the entry data
                zipOutputStream.putNextEntry(new ZipEntry(srcFile.getName()));

                int length;

                while ((length = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, length);
                    progress += length;
                    System.out.println("Progress : " + progress);
                }

                zipOutputStream.closeEntry();

                // close the InputStream
                fileInputStream.close();

            }

            // close the ZipOutputStream
            zipOutputStream.close();

        } catch (IOException e) {
            System.out.println("Error creating zip file: " + e);
        }

    }
}

Related

  1. zip(List fileNames, List fileContents)
  2. zip(List fileNames, String outFileName)
  3. zip(List srcFiles, OutputStream os)
  4. zip(String[] filename, String outname)
  5. zip(String[] sFl, String sZip)
  6. zip(String[] srcFiles, String dstFile, String comment)
  7. zipFileList(List files, File destinationDir)
  8. zipFiles(ArrayList files, String destZipFile)
  9. zipFiles(Collection resFileList, File zipFile)