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();
}
}
}
http://blog.csdn.net/xumiaohua/article/details/4297100
帮你google了一个。。。
------解决方案--------------------
/**
* 解压文件
*
* @param zipFilePath
* 压缩文件绝对路径
* @param unZipPath
* 解压的位置
*/
@SuppressWarnings("rawtypes")
public static void unZip(String zipFilePath, String unZipPath) throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration zipEntryList = zipFile.entries();
ZipEntry zipEntry = null;
byte[] buf;
while (zipEntryList.hasMoreElements()) {
zipEntry = (ZipEntry) zipEntryList.nextElement();
if (zipEntry.isDirectory()) {
File file = new File(unZipPath + zipEntry.getName());
file.mkdir();
continue;
}
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(getRealFileName(unZipPath,
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();
}