Java - ZIP File Creating

Introduction

You would be using the following four classes from the java.util.zip package to work with the ZIP file format:

ZipEntry
ZipInputStream
ZipOutputStream
ZipFile

ZipEntry represents an entry in a ZIP file.

If you have archived 10 files in a file called test.zip, each file in the archive is represented by a ZipEntry.

ZipInputStream reads data from a ZIP file for each entry.

ZipOutputStream writes data to a ZIP file for each entry.

ZipFile reads the entries from a ZIP file.

You can use either the ZipInputStream class or the ZipFile class when reading entries from a ZIP file.

The following code shows how to create a ZIP file.

Demo

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

public class Main {
  public static void main(String[] args) {
    String zipFileName = "ziptest.zip";
    String[] entries = new String[2];
    entries[0] = "test1.txt";
    entries[1] = "notes" + File.separator + "test2.txt";
    zip(zipFileName, entries);//from  www.  j a va2s. c o  m
  }

  public static void zip(String zipFileName, String[] zipEntries) {
    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;
        }

        // 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();
  }
}

Result