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");
}
}
内容来源于网络.