Creating a zip file in Java using the Zip4j library

This morning brought a new challenge — creating a compressed zip file of a directory from within an existing Java program for archival purposes.

Rather than installing a 3rd party program like 7Zip, this code needed to be fully self-contained in Java. This isn’t a dig on 7zip, which is a great program, but depending on a separately installed program wasn’t a valid option.

After exhausing the list of usual suspects for advice, a.k.a co-workers, it was time to turn to Google. After reading about several libraries, one name kept bubbling to the top, a library called Zip4j.  This library was created by Srikanth Reddy Lingala and is offered for free.  Sounds perfect!

The first step is to download zip4j_1.3.1.jar from lingala.net and save to the JRE’s lib folder, i.e. c:Java1.6jrelib.  Alternatively, save the jar file to a different location and add it to the application’s classpath.

You may need to change the file extension from .zip to .jar after downloading the file — that was my experience anyway.

Import the following packages to your Java application:

import net.lingala.zip4j.core.*;
import net.lingala.zip4j.model.*;
import net.lingala.zip4j.util.*;

The following example method will create a compressed archive of a folder, adding a timestamp in milliseconds to the archive name.  This allows your application to create multiple archives without overwriting previous archives.

private void archiveDir(String path) {
 try {
  Calendar calendar = Calendar.getInstance();
  Date time = calendar.getTime();
  long milliseconds = time.getTime();

  // Initiate ZipFile object with the path/name of the zip file.
  ZipFile zipFile = new ZipFile(path+"_"+milliseconds+".zip");

  // Folder to add
  String folderToAdd = path;

  // Initiate Zip Parameters which define various properties such
  // as compression method, etc.
  ZipParameters parameters = new ZipParameters();

  // set compression method to store compression
  parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

  // Set the compression level
  parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

  // Add folder to the zip file
  zipFile.addFolder(folderToAdd, parameters);

 } catch (Exception e) {
  e.printStackTrace();
 }
}
Advertisements

3 thoughts on “Creating a zip file in Java using the Zip4j library

  1. Pingback: My Homepage

  2. Hi Brian,

    We like the zip file sample code above and were interested in using it with slight modifications in our product (www.bitzermobile.com). Can you confirm that this sample code is in the public domain? Or if not then what open source license you release it under (MIT, BSD, etc).

    Thanks alot.

    John

    Like

  3. Hi,
    How can I create an encrypted ZIP file, it’s entry file name are keeped hidden until then I decrypted the ZIP file? It should be like using of option (parameter) “-mhe=on” in case of call 7Zip program to compress some files in an encrypt ed ZIP file.

    Mahmud

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s