java解压中文乱码_java zip解压中文乱码问题

这段代码可以解决中文zip和zip包中的文件也是中文的问题

importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importorg.apache.tools.zip.ZipFile;public classunzipandrar {/*** 解压zip格式的压缩文件到指定位置

*@paramzipFileName 压缩文件

*@paramextPlace 解压目录

*@throwsException*/@SuppressWarnings("unchecked")public synchronized voidunzip(String zipFileName, String extPlace){try{

(newFile(extPlace)).mkdirs();

File f= newFile(zipFileName);

ZipFile zipFile= newZipFile(zipFileName);if((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");

}

String strPath, gbkPath, strtemp;

File tempFile= newFile(extPlace);

strPath=tempFile.getAbsolutePath();

java.util.Enumeration e=zipFile.getEntries();while(e.hasMoreElements()){

org.apache.tools.zip.ZipEntry zipEnt=(org.apache.tools.zip.ZipEntry)e.nextElement();

gbkPath=zipEnt.getName();if(zipEnt.isDirectory()){

strtemp= strPath + File.separator +gbkPath;

File dir= newFile(strtemp);

dir.mkdirs();continue;

}else{//读写文件

InputStream is =zipFile.getInputStream(zipEnt);

BufferedInputStream bis= newBufferedInputStream(is);

gbkPath=zipEnt.getName();

strtemp= strPath + File.separator +gbkPath;//建目录

String strsubdir =gbkPath;for(int i = 0; i < strsubdir.length(); i++) {if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {

String temp= strPath + File.separator + strsubdir.substring(0, i);

File subdir= newFile(temp);if(!subdir.exists())

subdir.mkdir();

}

}

FileOutputStream fos= newFileOutputStream(strtemp);

BufferedOutputStream bos= newBufferedOutputStream(fos);intc;while((c = bis.read()) != -1) {

bos.write((byte) c);

}

bos.close();

fos.close();

}

}

}catch(Exception e) {

e.printStackTrace();try{throwe;

}catch(Exception e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值