使用Java 导入Zip文件遇到中文命名乱码问题

来源:互联网 发布:郑云灿的淘宝店 编辑:程序博客网 时间:2024/05/22 12:30

偶然做到导入Zip附件,遇到中文命名乱码,总结下,备注

ZipFile zf = new ZipFile(import_fj);//接收文件

InputStream is=new BufferedInputStream(new FileInputStream(import_fj));//通过流传递
ZipInputStream zin = new ZipInputStream(is,Charset.forName("ISO-8859-1")); //最重要的一个编码,首先通过ISO-8859-1转码
ZipEntry ze=null;
OutputStream fs =null;
Map<String, Object> map=new HashMap<String,Object>();
if(import_fj!=null){
while((ze = zin.getNextEntry()) != null) {
//判断目录结构区分文件和文件夹
ze=new ZipEntry(new String(ze.getName().getBytes("ISO8859_1"),"GBK"));//将ISO-8859-1转为GBK,此时中文已经正常显示
if (ze.isDirectory()) {
//文件夹
}else{
//文件
if(ze.getName()!=null){
//创建(身份证号码/附件名)类型文件夹格式
String[] s=ze.getName().split("/");
if(s[1]!=null&&s.length>2){
int result=examregisterService.compareBySfzhm(s[1]);
if(result==1){
//数据库存在报名表图片信息创建文件夹通过流导入图片文件
path = getRealPath("/") + "upload\\" +"seaman/examregister/fj/" +s[1];
File file = new File(path);
if(!file.exists()) {
file.mkdirs();
}
maps=getUploadFiles("seaman/examregister/fj/"+s[1]);
//判断目录下是否已经存在图片文件避免重复读取文件流
if(maps==null){
path = getRealPath("/") + "upload\\" +"seaman/examregister/fj/"+s[1]+"/"+s[2];
fs = new FileOutputStream(path);
long size = ze.getSize();
int len;
if (size > 0) {
InputStream fis=zf.getInputStream(ze);
byte[] bt = new byte[1024];
while((len=fis.read(bt)) != -1) {
fs.write(bt,0,len);
}
fs.flush();
fs.close();
fis.close();
successSize++;
}
0 0