Java Zip File zip(String zipFileName, String[] zipEntries)

Here you can find the source of zip(String zipFileName, String[] zipEntries)

Description

zip

License

Open Source License

Declaration

public static void zip(String zipFileName, String[] zipEntries) 

Method Source Code


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

import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.zip.Deflater;

public class Main {
    public static void zip(String zipFileName, String[] zipEntries) {
        // Get the current directory for later use
        String currentDirectory = System.getProperty("user.dir");

        try (ZipOutputStream zos = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFileName)))) {

            // Set the compression level to best compression
            zos.setLevel(Deflater.BEST_COMPRESSION);

            // Add each entry to the ZIP file
            for (int i = 0; i < zipEntries.length; i++) {
                // Make sure the entry file exists
                File entryFile = new File(zipEntries[i]);
                if (!entryFile.exists()) {
                    System.out.println("The entry file " + entryFile.getAbsolutePath() + " does not exist");
                    System.out.println("Aborted processing.");
                    return;
                }/*from www  . j  av  a2 s  .co m*/

                // Create a ZipEntry object
                ZipEntry ze = new ZipEntry(zipEntries[i]);

                // Add zip entry object to the ZIP file
                zos.putNextEntry(ze);

                // Add the contents of the entry to the ZIP file
                addEntryContent(zos, zipEntries[i]);

                // We are done with the current entry
                zos.closeEntry();
            }

            System.out.println("Output has been written to " + currentDirectory + File.separator + zipFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void addEntryContent(ZipOutputStream zos, String entryFileName)
            throws IOException, FileNotFoundException {

        // Create an input stream to read data from the entry file
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFileName));

        byte[] buffer = new byte[1024];
        int count = -1;
        while ((count = bis.read(buffer)) != -1) {
            zos.write(buffer, 0, count);
        }
        bis.close();
    }
}

Related

  1. zip(String src, String des)
  2. zip(String src, String dest, PrintStream stream)
  3. zip(String srcPath)
  4. zip(String zipFileName, Map entries)
  5. zip(String zipFileName, String inputFile)
  6. zip(String zipName, String[] fileNames, boolean path)
  7. zip(String zipPath, Map input)
  8. zipFile(File aFileToZip)
  9. zipFile(File file)