76

I need to zip and password-protect a file. Is there a good (free) library for this?

This needs to be opened by a third party, so the password protection needs to work with standard tools.

Kaal_Hari
  • 15
  • 7
nearly_lunchtime
  • 12,203
  • 15
  • 37
  • 42
  • 9
    How such helpful question got closed? – Dmitry Zaytsev May 28 '14 at 16:43
  • 2
    Because Meta decided that useful questions that could ever so slightly turn into matters of opinion are not meant to fit StackOverflow. It's sad, but true. – user314104 Oct 26 '14 at 22:14
  • Zip4j supports the encryption of the file list: http://stackoverflow.com/questions/15085249/how-to-encrypt-zip-file-using-zip4j/41568009 – kinjelom Jan 10 '17 at 12:29

9 Answers9

57

You can try Zip4j, a pure java library to handle zip file. It supports encryption/ decryption of PKWare and AES encryption methods.

Key features:

  • Create, Add, Extract, Update, Remove files from a Zip file
  • Read/Write password protected Zip files
  • Supports AES 128/256 Encryption
  • Supports Standard Zip Encryption
  • Supports Zip64 format
  • Supports Store (No Compression) and Deflate compression method
  • Create or extract files from Split Zip files (Ex: z01, z02,...zip)
  • Supports Unicode file names
  • Progress Monitor

License:

Matt
  • 621
  • 5
  • 2
37

UPDATE 2020: There are other choices now, notably Zip4J.


After much searching, I've found three approaches:

A freely available set of source code, suitable for a single file zip. However, there is no license. Usage is AesZipOutputStream.zipAndEcrypt(...). http://merkert.de/de/info/zipaes/src.zip (https://forums.oracle.com/forums/thread.jspa?threadID=1526137)

UPDATE: This code is now Apache licensed and released at https://github.com/mobsandgeeks/winzipaes (exported from original home at Google code) . It worked for me (one file in the zip), and fills a hole in Java's opens source libraries nicely.

A commercial product ($500 at the time of writing). I can't verify if this works, as their trial license approach is complex. Its also a ported .NET app: http://www.nsoftware.com/ipworks/zip/default.aspx

A commercial product ($290 at the time of writing). Suitable only for Wnidows as it uses a dll: http://www.example-code.com/java/zip.asp

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
  • Does anyone have a link to how to use the winzipaes code that is posted in this link? I need to add more than one file and could do with some help understanding how to use it – frak Oct 16 '11 at 16:21
  • @frak Look at the source code for AesFileEncrypter: all you have to do is create an instance of that class and then add whatever you want to it. – Christopher Schultz Aug 14 '12 at 16:27
  • 1
    winzipaes is available via Maven repo http://mvnrepository.com/artifact/de.idyl/winzipaes – Marc Oct 24 '12 at 15:05
13

This isn't an answer, but it is a caution to keep in mind when evaluating potential solutions.

One very important thing about zip encryption:

There are several types of zip encryption. The old type (part of the original zip standard) is not at all worth bothering with (it can be cracked in less than 10 minutes with apps easily available online).

If you are doing any sort of encryption of zip files, please, please be sure you use one of the strong encryption standards (I believe that WinZip's 128- and 256-bit AES standard is the best supported). Here are the technical specs - we used this when developing our own Java encrypted zip system (can't provide source - sorry - it's internal use only)

The only thing worse than having no encryption is thinking that you have encryption and being wrong :-)

Kevin Day
  • 16,067
  • 8
  • 44
  • 68
  • 2
    You may want to support both. AES-encrypted zip files are, aFAIK, not yet supported by Windows Explorer, while PKZIP-encrypted zip files are. You can view the entries of either type of encrypted zip file, but you can only extract from a PKZIP-encrypted (weakly encrypted) zipfile using Windows Explorer. You will need WinZip or another suitable tool on Windows to open AES-encrypted zips. (The free DotNetZip ships with such a tool). – Cheeso May 11 '09 at 17:46
  • Windows XP SP3 and Vista both support AES zip files. My opinion (which any and all are free to disagree with) is that supporting something that isn't even remotely secure is a bad idea. M$ not supporting a secure format is not a valid reason for implementing something that is insecure. – Kevin Day May 13 '09 at 04:24
  • Winzipaes-created (see above) .zip files do not open on my up to date Windows XP image here, Windows moans about an unsupported compression scheme. They do, however, open fine with 7z. – Tom Chiverton Jul 05 '11 at 12:33
4

7-Zip has the option to add a password in its command-line mode. Perhaps you can exec it to get this result (and it has a good compression ration too).

Drawbacks: external process, hard to make portable (even if 7-Zip is portable itself), not sure of distribution license.

Note that InfoZip's Zip utility, highly portable too, also supports password.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • 7-zip has libraries to use in self-written programs I believe, so it would not have to be an external process. – Quagmire Oct 15 '09 at 11:07
  • @Quagmire 7-zip does not have a native Java build (except for the LZMA algorithm), so using 7z to work with ZIP files will require an external process. – Christopher Schultz Aug 14 '12 at 16:29
  • @ChristopherSchultz indeed. I suppose one can also write a JNI or JNA wrapper, for less trouble. – PhiLho Aug 28 '12 at 10:21
  • It's also worth pointing out that ZIP 2.0-compatible "password" is horribly broken and shouldn't be used for anything other than keeping toddlers from looking at your files. – Christopher Schultz Aug 29 '12 at 21:18
3

You can also try TrueZip. See the following links for features: https://christian-schlichtherle.bitbucket.io/truezip/

The successor of TrueZip can be found here: https://christian-schlichtherle.bitbucket.io/truevfs/

Kai Mechel
  • 753
  • 8
  • 19
  • 1
    I wish I'd seen this sooner, Zip4j is fine but this is better. In particular it handles a broader range of inputs, such as self extracting and nested zip files and math other things, and isn't abandoned. – thyme Feb 25 '16 at 01:48
  • 1
    Update for people reading this nowadays: Zip4j development has been picked up again as of mid-2021. – Daniel Werner Sep 10 '21 at 16:23
2

Here's an example using winzipaes 1.0.1. Note this is just a gist, I have not tested this code in exactly this form.

import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;

File aNewZipFile = new File("/tmp/foo.zip");
File existingUnzippedFile = new File("/tmp/src.txt");

// We use the bouncy castle encrypter, as opposed to the JCA encrypter
AESEncrypterBC encrypter = new AESEncrypterBC();
encrypter.init("my-password", 0);  // The 0 is keySize, it is ignored for AESEncrypterBC

AesZipFileEncrypter zipEncrypter = new AesZipFileEncrypter(aNewZipFile, encrypter);
zipEncrypter.add(existingUnzippedFile, "src.txt", "my-password"); 

// remember to close the zipEncrypter
zipEncrypter.close();

You can them unzip "/tmp/foo.zip" using Winzip (v9+) or 7za (i.e. 7zip) on a Mac, using password "my-password".

Note: it's not clear to me why it is necessary to specify the password twice in the code above. I do not know what would happen if you used different passwords in these two places.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
Marc
  • 824
  • 10
  • 18
1

If you give a better usage scenario then there are other alternatives.

  1. Do you require the zip to be opened by the standard Zip tools that can handle a zip password?
  2. The same question as previous are you going to pass this zip to an external entity that has to open the zip?
  3. Is it internal only and you just want to protect the contents of the zip?

For 3 then you can just use java to encrypt the stream contents of the zip as a normal file, probably best to change the file extension to .ezip or somesuch too.

For 1 and 2 then you can use the chillkat solution as mentioned, or an equivalent. However be aware that chillkat is not a pure Java solution, it uses JNI.

Donal Tobin
  • 95
  • 1
  • 4
1

Additional info: I googled a bit more and indeed, it is a quite common question, and it appears there is no free solution (yet?).

Now, the standard algorithm of Zip encryption is well defined: See PKWARE's Application Note on the .ZIP file format. It appears to be an encryption done on the encrypted stream. If somebody feels like coding it...

Now, I wonder why Sun didn't include it in its library? Lack of standard? Patent/legal issue? Too weak to be usable?

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • I did exactly this about 5 years ago for one of our apps. It took some effort (and I can't, unfortunately, post code for it) - but it's good to know that it can be done. We actually implemented the AES strong encryption system promoted by WinZip. – Kevin Day Oct 04 '08 at 02:42
-1

Is there a good (free) library for this?

java.util.zip will do the zipping, but it won't do the passwords. And no, I don't know of any free ones that will. The cheapest I've seen is $150 for a developer seat.

Oli
  • 235,628
  • 64
  • 220
  • 299