четверг, 25 сентября 2014 г.

Java zip file creation.

I had a task: get the files from database, create the zip file from them and serve the file to user.

The most tricky part of this process is to combine files into zip archive.

On my developer's computer I use windows 7  + Oracle JDK, on the test site we have Cent os and Websphere portal 7.

I have successfully wrote the code, tested it on developer's computer and deployed to test system. I have been surprised by the behavior of the code.
The code is as simple as:
 
ZipOutputStream zipStream = ...
ZipEntry zipEntry = new ZipEntry(zipEntryName);
zipStream.putNextEntry(zipEntry);
zipStream.closeEntry();
zipStream.finish();
zipStream.close();
 
My zip file contains entries made of Cyrillic symbols. But when I had got the file from test system I have found out that the filenames in zip archive have been corrupted.  On developer's computer everything worked fine.

The question is in encoding. I suggest that IBM JRE uses the system encoding for encoding the entry names in archive. Java 7 has the constructor
ZipOutputStream(OutputStream out, Charset charset) 
which accept charset for entry names encoding. But WebSphere Portal comes with java 6.

After googling I have found out the project Apache commons-compress project. It allows you to create zip files with specified zip entries encoding like the following:
zos = new ZipArchiveOutputStream(os);
zos.setEncoding("UTF-8");
zos.setFallbackToUTF8(true);
zos.setUseLanguageEncodingFlag(true);


Change the code to use commons-compress and I could produce identical results on developer's and test computers. Happy coding!

Комментариев нет:

Отправить комментарий