在解析zip文件时,由于默认版本不支持中文目录,导致解析时遇到中文文件目录会出现乱码:
从网上找到一些资料,自己写了个样例。有兴趣的可以看看。(需要修改原有的zip包,添加构造方法,支持gbk编码)
具体修改class为 ZipinputStream和ZipOutputStream(代码见附件中)
见下方压缩与解析代码:
package ziphandle;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import zip.ZipEntry;
import zip.ZipInputStream;
import zip.ZipOutputStream;
@Transactional
@Component
public class FileHandle {
public static void zip(File outZipPath, File zipFileRoot) {
try {
// filePath.
OutputStream os = new FileOutputStream(outZipPath);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zip = new ZipOutputStream(bos, "GBK");
zip(zip, zipFileRoot, "");
zip.flush();
zip.closeEntry();
zip.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 写入文件目录与文件内容
*/
private static void zip(ZipOutputStream out, File f, String base) throws Exception {
System.out.println("Zipping " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
// base = new String(base.getBytes(), "GBK");
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else if (!f.getName().endsWith(".zip")) {
// base = new String(base.getBytes(), "GBK");
out.putNextEntry(new ZipEntry(base));
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
byte[] bt = new byte[1024];
int b;
while ((b = in.read(bt)) != -1) {
out.write(bt, 0, b);
}
in.close();
}
}
@Transactional
public void unzip(String zipFileName) throws Exception {
ZipInputStream in = null;
try {
in = new ZipInputStream(new FileInputStream(zipFileName), "gbk");
ZipEntry z;
int totalCount = 0;
while ((z = in.getNextEntry()) != null) {
if (z.isDirectory()) {
String name = z.getName();
if (StringUtils.isNotBlank(name)) {
System.out.println("目录:" + name);
int index = name.lastIndexOf("/");
String currentName = null;
String parentName = null;
if (index != -1) {
currentName = name.substring(index + 1, name.length());
parentName = name.substring(0, index);
} else {
currentName = name;
}
currentName = currentName.substring(0, name.length() - 1); //去掉斜线
System.out.println("输出目录名称:"+currentName);
}
} else {
String name = z.getName();
System.out.println("文件名:" + name);
if (StringUtils.isNotBlank(name)) {
String currentName = null;
String parentName = null;
int index = name.lastIndexOf("/");
if (index != -1) {
currentName = name.substring(index + 1, name.length());
parentName = name.substring(0, index);
} else {
currentName = name;
}
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String b;
int row = 0;
while ((b = br.readLine()) != null) {
totalCount++;
row++;
System.out.println("读出文件内容:"+b); //打出读取的内容
}
System.out.println("读取文件 " + currentName + " 结束");
}
}
}
} catch (Exception e) {
throw e;
} finally {
in.close();
File tempFile = new File(zipFileName);
tempFile.delete();
tempFile.deleteOnExit();
}
}
public static void main(String[] args) {
try {
// OutputStream os = new FileOutputStream(new File("D:/中文new.zip"));
// BufferedOutputStream bos = new BufferedOutputStream(os);
// ZipOutputStream zip = new ZipOutputStream(bos, "GBK");
// zip(zip, new File("D://中文"), "");
// zip.flush();
// zip.closeEntry();
// zip.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}