java 解压 Zip文件乱码的问题

f250303748 2008-09-10 10:20:35
我写了一段解压Zip的代码,但是如果Zip文件里面有中文文件名解压出来就是乱码。
请问有没有解决的办法?
public void releaseZipToFile(String sourceZip, String outFileName)
throws IOException {
ZipFile zfile = new ZipFile(sourceZip);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {

ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
continue;
}

OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(outFileName, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
is.close();
os.close();

System.out.println("Extracted: " + ze.getName());
}
zfile.close();
File file = new File(sourceZip);
file.delete();
}
...全文
496 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzwlord 2010-07-07
  • 打赏
  • 举报
回复
org.apache.tools.zip包下的zip工具可以解决,引入ant.jar
lord_is_layuping 2008-09-18
  • 打赏
  • 举报
回复
在java.util.zip包也可以用来处理解压问题,不过对含有中文文件名的压缩包无能为力,这是因为ZipOutputStream压缩和解压 ZIP文件对文件名都是以UTF-8编码方式来处理的,而我们用winzip压缩文件对文件名只会以ASCII编码方式来处理.所以会出现编码不一致的问题.
有两种解决方案:
第一种就是修改ZipOutputStream,参考修改如下:(这个我没有测试过)
// ZipEntry e = createZipEntry(getUTF8String(b, 0, len));
ZipEntry e=null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
e=createZipEntry(getUTF8String(b, 0, len));
else
e=createZipEntry(new String(b,0,len,this.encoding));
}catch(Exception byteE) {
e=createZipEntry(getUTF8String(b, 0, len));
}

再加一个
public ZipInputStream(InputStream in,String encoding) {
super(new PushbackInputStream(in,512),new Inflater(true),512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
this.encoding=encoding;
}

第二种方法就是用ant包,可以在官方网站http://ant.apache.org/bindownload.cgi下载,把ant.jar导入到类中.
参考用例如下:
public void unzip(String zipFileName,String outputDirectory) throws Exception{
try {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
org.apache.tools.zip.ZipEntry zipEntry = null;
while (e.hasMoreElements()){
zipEntry = (org.apache.tools.zip.ZipEntry)e.nextElement();
System.out.println("unziping "+zipEntry.getName());
if (zipEntry.isDirectory()){
String name=zipEntry.getName();
name=name.substring(0,name.length()-1);
System.out.println("输出路径:"+outputDirectory+File.separator+name);
File f1=new File(outputDirectory+File.separator);
f1.mkdir();
File f=new File(outputDirectory+File.separator+name);
f.mkdir();
System.out.println("创建目录:"+outputDirectory+File.separator+name);
}else{
File f=new File(outputDirectory+File.separator+zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out=new FileOutputStream(f);
//--------解决了图片失真的情况
int c;
byte[] by=new byte[1024];
while((c=in.read(by)) != -1){
out.write(by,0,c);
}
out.close();
in.close();
}
}
}
catch (Exception ex){
}
}
f250303748 2008-09-18
  • 打赏
  • 举报
回复
有人能给出解决方案嘛?
胡须棉花糖 2008-09-10
  • 打赏
  • 举报
回复
名字解码
f250303748 2008-09-10
  • 打赏
  • 举报
回复
帮帮解决一下啊
f250303748 2008-09-10
  • 打赏
  • 举报
回复
请问还有别的解决办法吗?或者给出具体的代码

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧