JAVA将zip压缩文件解压
求一段代码,能将zip文件解压,效果如同正常使用的时候解压出来就行!急求!!!
[解决办法]
public static void unZip(String zipfile, String destDir) {
destDir = destDir.endsWith( "//" ) ? destDir : destDir + "//" ;
byte b[] = new byte [1024];
int length;
ZipFile zipFile;
try {
zipFile = new ZipFile( new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null ;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists())
loadFile.getParentFile().mkdirs();
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
}
}
System. out .println( " 文件解压成功 " );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
zipEntry.getName())));
InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
int readLen = 0;
buf = new byte[1024];
while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, readLen);
}
inputStream.close();
outputStream.close();
}
zipFile.close();
}