67,513
社区成员
发帖
与我相关
我的任务
分享
/**
* 解压文件
*
* @param zipFilename
* 需要解压的文件
* @param outputDirectory
* 解压后的文件
* @throws IOException
*/
public synchronized static void unzip(String zipFilename, String outputDirectory)
throws Exception {
InputStream in = null;
try {
File outFile = new File(outputDirectory);
if (!outFile.exists()) {
outFile.mkdirs();
}
ZipFile zipFile = new ZipFile(zipFilename);
// Enumeration en = zipFile.getEntries();
Enumeration en = zipFile.entries();
ZipEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + File.separator
+ dirName);
f.mkdirs();
} else {
FileOutputStream out = null;
// unzip file
File f = new File(outFile.getPath() + File.separator
+ zipEntry.getName());
f.createNewFile();
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
if(out !=null){
out.close();
}
}
}
} finally {
if (in != null)
in.close();
}
}
File outFile = new File(outputDirectory);
if (!outFile.exists()) {
outFile.mkdirs();
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFilename);
} catch (Exception e) {
throw new Exception("压缩文件已损坏",e);
}
// Enumeration en = zipFile.getEntries();
Enumeration en = zipFile.entries();
ZipEntry zipEntry = null;