How to add zip entry with utf-8 name in zip

I have a method that adds an inputStream to a zip as a record:

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 file name contains UTF-8 char as áé ... In the zip file it will be saved as ????? , and when I unzip it in ubuntu 12.10, it looks like this: N├stroje instead of Nástroje .

In this example, I used jdk6, but now I also tried jdk7:

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

But without success.

I also tried Apache Commons Zip and set the encoding, but also did not succeed.

So, how can I add this file with Unicode characters in the file name for zip?

+7
source share
2 answers

This line seems to have solved my problem:

  zos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS); 

can someone explain to me what this does and why it works?

+3
source

The Zip archive uses the DOS (OEM) code page by default to store file names. The Linux / Unix implementation uses the system codepage when unpacking. Mac OS uses utf-8 by default. Therefore, in your case, the file name is saved correctly, but the Linux archiver does not understand this.

+1
source

All Articles