java 解压文件 文件名为中文

        java在解压zip包中的文件时,如果文件名有中文,将会抛出异常.

        网上有方法说直接修改java.util.zip.ZipInputStream中的方法

        后来又找到一种方法,使用ant的ZipFile来解决,测试可用

package test6;

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Enumeration; 

import org.apache.tools.zip.ZipFile; 

/** 
 * Author:neo_q Create_Date:2008-3-17 
 */ 
public class UnZip { 

public static final int DEFAULT_BUFFER = 1024 * 4; 

public static long copy(InputStream aIn, OutputStream aOut, int aBufferSize) 
throws IOException { 
byte[] buffer = new byte[aBufferSize]; 
int read = 0; 
long totalBytes = 0; 
while (-1 != (read = aIn.read(buffer))) { 
aOut.write(buffer, 0, read); 
totalBytes += read; 
} 
return totalBytes; 
} 

public static long copy(InputStream aIn, OutputStream aOut) 
throws IOException { 
return copy(aIn, aOut, DEFAULT_BUFFER); 
} 

public static void unzip1(File zipFileName, File outputDirectory) 
throws Exception { 
ZipFile zf = null; 
try { 
zf = new ZipFile(zipFileName); 
Enumeration en = zf.getEntries(); 
while (en.hasMoreElements()) { 
org.apache.tools.zip.ZipEntry entry = (org.apache.tools.zip.ZipEntry) en 
.nextElement(); 
System.out.println(entry.getName()); 

FileOutputStream out = null; 
try { 
System.out.println(entry.isDirectory()); 

if (!entry.isDirectory()) { 
File newFile = new File(outputDirectory, entry 
.getName()); 
newFile.getParentFile().mkdirs(); 
out = new FileOutputStream(newFile); 
copy(zf.getInputStream(entry), out); 
} 

} catch (Exception io) { 
io.printStackTrace(); 
throw io; 
} finally { 
if (out != null) 
out.close(); 
} 

} 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} finally { 
try { 
if(zf!=null) 
zf.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
} 

public static void main(String[] args) throws Exception { 

UnZip.unzip1(new File("D:\\2m.zip"), new File("C:\\testUnzip\\dest")); 
System.out.println("OK"); 
} 

}

 内容来源于网络. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值