用java写的一个解压zip格式文件的程序,对文件名中有中文的情况会抛出异常。
public static void unZip(File zipFile) throws IOException {
// 得到压缩文件所在目录
String path = zipFile.getAbsolutePath();
path = path.replaceAll("\\\\", "/");
path = path.substring(0, path.lastIndexOf("/"));
ZipFile zip = new ZipFile(zipFile);
InputStream in = null;
OutputStream out = null;
try {
for (@SuppressWarnings("rawtypes")
Enumeration entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
System.out.println(zipEntryName);
in = zip.getInputStream(entry);
// outPath输出目录
String outPath = path + "/" + zipEntryName;
// 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0,
outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经创建,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
out = new FileOutputStream(outPath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
} catch (IOException e) {
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
zip.close();
}