我正在使用zip4j解压文件,我在文件名字符集方面遇到了问题。这是我的代码
try {
ZipFile zipFile = new ZipFile(source);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
System.out.println(System.getProperty("file.encoding"));
zipFile.setFileNameCharset("UTF-8");
zipFile.extractAll(destination);
} catch (ZipException e) {
System.out.println(e.getMessage());
}
}它工作得很好,但文件名如下

发布于 2016-02-07 13:43:11
如果您想解压由其他软件创建的zip文件。文件名字符集可以是系统默认字符集(如GBK、Shift-JIS或其他字符集...)
在这种情况下,如果其中一个源文件名包含unicode字符,而该字符集中不存在该字符。该ZipEntry中的文件名将转换为UTF-8。
要解压缩这种zip文件,必须通过自定义代码逐个转换文件名。
ZipFile zipFile = new ZipFile("input.zip");
UnzipParameters param = new UnzipParameters();
zipFile.setFileNameCharset("ISO8859-1");
List list = zipFile.getFileHeaders();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
FileHeader fh = (FileHeader) iterator.next();
byte[] b = fh.getFileName().getBytes("ISO8859-1");
String fname = null;
try {
fname = new String(b, "UTF-8");
if (fname.getBytes("UTF-8").length != b.length) {
fname = new String(b,"GBK");//most possible charset
}
} catch (Throwable e) {
//try other charset or ...
System.err.println("Invalid file name: "+fname);
}
z.extractFile(fh, dir, param, fname);
}发布于 2015-08-10 16:44:58
当您使用zip4j压缩和解压缩zip文件时,您使用的是相同的字符集。
(我的测试用例:压缩UTF-8,解压UTF-8,没问题/ zip4j 1.3.2)
发布于 2017-08-23 10:14:24
Beck Yang的解决方案真的很有效。
适用于使用加密密码解压中文命名文件的人。zip4j中有一个错误。必须在调用isEncrypted和setPassword之前调用setFileNameCharset。否则,输出编码是错误的。
而且,只写目录(空目录)也有编码问题。除修改源码外,无法修复。
https://stackoverflow.com/questions/28132515
复制相似问题