1.压缩解压ZIP文件对文件名都是以UTF-8编码方式来处理的,而WinZip对文件名只会
以ASCII编码方式来处理
原代码:ZipInputStream zis = new ZipInputStream(in);
jdk1.7:ZipInputStream(InputStream in, Charset)
增加GBK编码:ZipInputStream zis = new ZipInputStream(in,Charset.forName("GBK"));
jdk1.6:
jdk1.6中 只有ZipInputStream(InputStream in),ZipEntry读取时使用UTF-8编码,所以引入ant-1.6.5.jar,来支持带中文名的文件解压缩:(与1.7类似的都是获取ZipEntry来读取压缩文件的内容,不同的是读取的方式)
//解压alipay的账单文件,略过“汇总文件”
public static String unZip(File file, String destDir) {
String fileName = null;
try {
if(file == null){
logger.error("Zip file not fund !");
return null;
}
if(StringUtils.isEmpty(destDir)){
logger.error("Zip unZipDir is Empty : " + destDir);
return null;
}
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(file, "GBK");
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
if(zipEntry.getName().contains("汇总")){
continue;
}
fileName = destDir + File.separator + zipEntry.getName();
File dirFile = new File(fileName);
fileProber(dirFile);
if (zipEntry.isDirectory()) {
//建文件夹目录 …… ……
dirFile.mkdirs();
……
} else {
InputStream in = zipFile.getInputStream((org.apache.tools.zip.ZipEntry) zipEntry);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
int count;
byte data[] = new byte[1024];
while ((count = in.read(data, 0, 1024)) != -1) {
bos.write(data, 0, count);
}
bos.close();
in.close();
}
zipFile.close();
}
zipFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e);
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
}
return fileName;
}