Skip to content
accelerando

Create ZIP Archives containing Unicode filenames with Java

It is harder than i thought to create a simple Zip Archive from within Java that contains entries with unicode names in it.

I’m actually to lazy to read all the specs, but it says something that the entries in a zip archive are encoded using “Cp437″. The buildin Java compressing api has nothing to offer for setting the encoding so i tried Apache Commons Compress.

The manual says the following about interop :

For maximum interop it is probably best to set the encoding to UTF-8, enable the language encoding flag and create Unicode extra fields when writing ZIPs. Such archives should be extracted correctly by java.util.zip, 7Zip, WinZIP, PKWARE tools and most likely InfoZIP tools. They will be unusable with Windows’ “compressed folders” feature and bigger than archives without the Unicode extra fields, though.

That didn’t work for me.

After some cursing, this is my solution:

final ZipArchiveOutputStream zout = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(fc.getSelectedFile())));
zout.setEncoding("Cp437");
zout.setFallbackToUTF8(true);
zout.setUseLanguageEncodingFlag(true);								
zout.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);

I specifying explicitly the encoding but instead of using utf-8, which didn’t work for my utf-8 strings (wtf??), i’m using the Cp437 from the specs and some other magic options and it works for me in 7zip, WinZip and even Windows’ “compressed folders”.

Edit: Unfortunately, in Mac OS X’s Unzip utility, the non Cp437 are broken. If anyone has a good idea, feel free to leave a comment.

6 Comments

  1. Ricardo wrote:

    Hello,

    Your post has been very useful to me. My zip files now have proper filename encoding. Thanks for sharing your knowledge.

    Posted on 06-May-10 at 10:14 am | Permalink
  2. Michael wrote:

    You’re very welcome!

    Posted on 06-May-10 at 10:39 am | Permalink
  3. Ricardo Costa wrote:

    Thank you very much my friend. I have spent too much time to solve my encoding filename problem.
    Congratulations.

    Posted on 17-Jun-10 at 4:12 pm | Permalink
  4. Michael wrote:

    Thank your for your kind comment :)

    Posted on 17-Jun-10 at 5:32 pm | Permalink
  5. yogav wrote:

    it does not seem to work for me, would you consider publishing your full code?

    Posted on 13-Feb-11 at 10:02 pm | Permalink
  6. Michael wrote:

    yogav: This is the full code for configuring the outputstream. Everything else is in the api.

    Posted on 09-Mar-11 at 8:24 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

*