11

I need to create a zip file from my java program.

I need a library that be able to create zip files and store entries from text and file in it and the most important thing is i need it be able to store entire directory in it (A directory with several levels of other directories that each have some files in them).

Can you suggest me one?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Ariyan
  • 14,760
  • 31
  • 112
  • 175

6 Answers6

23

Check out Zip4j - http://www.lingala.net/zip4j/

I've ran into this problem today and I refuse to do such low level crap that the JDK wants us to do. Hopefully this little library will work

Colin Goudie
  • 845
  • 5
  • 10
9

A library for doing the hard part of handling Zip files (i.e. the compression) is built right in to Java SE (java.util.zip):

http://download.oracle.com/javase/1.5.0/docs/api/java/util/zip/package-summary.html

For your higher level functions it wouldn't be that hard to write some functions to recursively traverse a directory and copy the files into a ZipOutputStream - probably less than 50 lines of code or so.

There's a good example at http://www.javareference.com/jrexamples/viewexample.jsp?id=108 - it needs a little bit of work to do single files.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
4

Another option that you can check out is zt-zip available from Maven central and project page at https://github.com/zeroturnaround/zt-zip

It has the standard packing and unpacking functionality (on streams and on filesystem) + lots of helper methods to test for files in an archive or add/remove entries.

toomasr
  • 4,731
  • 2
  • 33
  • 36
4

In such areas, there is usually something to consider from Apache (or, maybe more specifically: Apache Commons). I've found this: http://commons.apache.org/compress/zip.html (I've not checked it though).

KarolDepka
  • 8,318
  • 10
  • 45
  • 58
  • it only seems to make "better" zip files, it doesn't appear to include the directory traversal or anything to actually do the copying from disk files into the zip file. – Alnitak Mar 19 '11 at 14:08
3

There is a Java binding of famous 7zip library, you can check it out at http://sevenzipjbind.sourceforge.net/

atabek
  • 89
  • 1
  • 3
  • 12
  • 6
    From personal experience the 7zip library's Java binding is capable of dealing with Zip, GZip, RAR, and other formats, both for reading and writing. – Ian Durkan Jun 11 '12 at 14:35
1

easyzip4j

Download it from github.

https://github.com/willwarren/easyzip4j/downloads

Usage

EasyZip.zip("./folderToZip", "./myZipFile.zip");

OR

EasyZip.zip("./folderToZip", "./myZipFile.zip", 
    new EasyZipParameters().setZipFolderContentsNotFolder(true));

(also covered in the readme https://github.com/willwarren/easyzip4j/blob/master/README.md)

This is a maven based project.
Depends on commons-io and log4j. Feel free to edit!

Will
  • 6,179
  • 4
  • 31
  • 49