80,351
社区成员
发帖
与我相关
我的任务
分享
public static boolean unZip(String zipDirectory, String storageDirectory,
String fileName) {
boolean result = false;
String zipName = zipDirectory + fileName;
String filePath = storageDirectory;
ZipFile zipFile = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
zipFile = new ZipFile(zipName);
Enumeration<? extends ZipEntry> emu = zipFile.entries();
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()) {
new File(filePath + entry.getName()).mkdirs();
continue;
}
bis = new BufferedInputStream(zipFile.getInputStream(entry));
File file = new File(filePath + entry.getName());
File parent = file.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
}
result = true;
}
catch (Exception e) {
e.printStackTrace();
result = false;
}
finally {
try {
if (null != bos) {
bos.close();
}
if (null != bis) {
bis.close();
}
if (null != zipFile) {
zipFile.close();
}
}
catch (IOException e) {
e.printStackTrace();
result = false;
}
}
return result;
}
原来写过的一个解压方法,可以正常解压。
zipDirectory 是压缩文件路径
storageDirectory 是解压文件路径
fileName 是压缩文件名(xxx.zip)