ZipArchiveEntry字符集编码错误

时间:2012-02-06 10:15:03

标签: java apache encoding character-encoding zip

我必须使用apache-commons-compress-1.x API创建文件zip。 我使用了以下代码:

File fileZip = new File("D:\\file.zip");
ZipEncoding zipEncoding = ZipEncodingHelper.getZipEncoding("UTF8");
ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(fileZip);
zipOut.setEncoding("UTF-8");

File entryFile = new File("D:\\attività.jpg");
String entryName = entryFile.getName();
entryName = new String(entryName.getBytes("UTF-8"), "UTF-8");

ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
entry.setSize(entryFile.length());

FileInputStream fInputStream = new FileInputStream(entryFile);
zipOut.setUseLanguageEncodingFlag(true); 
 zipOut.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
zipOut.putArchiveEntry(entry);
zipOut.write(IOUtils.toByteArray(fInputStream));
zipOut.closeArchiveEntry();

zipOut.flush();
zipOut.close();

zip条目文件名有编码错误。如果我使用zip管理器构建的windows xp打开压缩文件,则文件名为attivit+á.jpg。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

待办事项

entryName = zipEncoding.encode("attivit\u00E0.jpg");