7

I have a method which adds inputStream to zip as an entry:

private void addToZip(InputStream is, String filename) throws Exception {
    try {
        ZipEntry zipEntry = new ZipEntry(filename);
        zos.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = is.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

The problem occurs when the filename contains an UTF-8 char like áé... In zip file it will be saved as ????? and when I unzip it in ubuntu 12.10 it looks like: N├бstroje instead of Nástroje.

For this example I used jdk6 but now I've also tried jdk7:

zos = new ZipOutputStream(fos, Charset.forName("UTF-8"));

But with no success.

I also tried Apache Commons Zip and set encoding but also with no success.

So how I can add this file with unicode symbols in filename to zip ?

6
  • I've had the same problem some time ago. I used non-Unicode charset, specific to the local language (Cp866). And it worked fine.
    – user784540
    Commented Mar 20, 2013 at 9:43
  • so instead of Nástroje I should save entry as Nastroje ? This is last options... how you convert it ?
    – hudi
    Commented Mar 20, 2013 at 11:03
  • That application was intended to run in Windows system. And I used Cp866 encoding, to support russian characters in filenames in zip archives and view them correctly when I view zip file contents. And it worked. Is there a non-unicode charset, that supports your language symbols? Try to use it, instead of utf-8.
    – user784540
    Commented Mar 20, 2013 at 11:17
  • yes: iso-8859-2 but when I use this then á is converting to c
    – hudi
    Commented Mar 20, 2013 at 11:52
  • I am sorry, but I have no idea how to overcome that.
    – user784540
    Commented Mar 20, 2013 at 11:57

2 Answers 2

4

seems this line solved my problem:

        zos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);

can someone explain me what is this doing and why it works ?

3
  • 2
    Unicode Extra Fields are an extension to the zip format to use additional custom fields in the entry headers to store a UTF-8 version of the file name alongside the default local encoding version. Archive unpackers that support this extension will use the UTF-8 name from the extra fields in preference to the standard name field. Commented Mar 20, 2013 at 12:32
  • I can't find the method setCreateUnicodeExtraFields inside ZipOutputStream Commented Jun 23, 2021 at 8:34
  • @BogdanSamondros this is an Apache Ant API ZipOutputStream Commented Mar 16, 2022 at 1:18
3

Zip archive by default uses DOS(OEM) codepage to store filenames. Linux/unix implementations uses system codepage when unpacking. Mac OS uses utf-8 by default. So in your case filename is stored correctly, but Linux archiver doesn't understand it.

4
  • but when I create zip in linux (right click and compress) it create archvive with entry name contains ?? but when I extract it there is correct name
    – hudi
    Commented Mar 20, 2013 at 11:01
  • Sure, because it uses the same encoding. Commented Mar 20, 2013 at 11:09
  • my system codepage is UTF-8 and when I use apache common archive and set endocing to utf-8 then it still doesnt work
    – hudi
    Commented Mar 20, 2013 at 11:16
  • The easiest way to check is to use hex editor/viewer on your archive. Windows zip archive codepage depends on your locale, for Russian it is CP-866, for other languages it should be something like CP-8xx Commented Mar 20, 2013 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.