使用java代码解压zip压缩文件时,文件或文件夹出现中文名称是报错
!!!ZipInputStream默认使用UTF_8,这样遇到中文文件时,会出现乱码报错为
IllegalArgumentException
只需要将编码转换为GBK,问题即可解决,希望帮助到各位
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Zip {
/**
* 解压压缩文件
* @param inputStream 压缩文件流
* @param container
* @throws IOException
*/
public void unZip(InputStream inputStream, Map<String, byte[]> container) throws IOException {
ZipInputStream zip = new ZipInputStream(inputStream, Charset.forName("GBK"));
ZipEntry zipEntry = null;
while ((zipEntry = zip.getNextEntry()) != null) {
String fileName = zipEntry.getName();// 得到文件名称
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] byte_s = new byte[1024];
int num = -1;
while ((num = zip.read(byte_s, 0, byte_s.length)) > -1) {// 通过read方法来读取文件内容
byteArrayOutputStream.write(byte_s, 0, num);
}
container.put(fileName, byteArrayOutputStream.toByteArray());
}
}
}