1 回答
TA贡献2011条经验 获得超2个赞
解决方案
您可以使用zip4j来提取 ZIP 文件。
public class Decompress {
private String zip;
private String loc;
public Decompress(String zipFile, String location) {
zip = zipFile;
loc = location;
dirChecker();
}
private void dirChecker() {
File f = new File(loc);
if(!f.isDirectory()) {
f.mkdirs();
}
}
public void unzip() {
try {
ZipFile zipFile = new ZipFile(zip);
List<FileHeader> fileHeaders = zipFile.getFileHeaders();
for (FileHeader fileHeader : fileHeaders) {
String fileName = fileHeader.getFileName();
if (fileName.contains("\\")) {
fileName = fileName.replace("\\", "\\\\");
String[] Folders = fileName.split("\\\\");
StringBuilder newFilepath = new StringBuilder();
newFilepath.append(loc);
for (int i = 0; i < Folders.length - 1; i++) {
newFilepath.append(File.separator);
newFilepath.append(Folders[i]);
}
zipFile.extractFile(fileHeader, newFilepath.toString(), null, Folders[Folders.length - 1]);
} else {
zipFile.extractFile(fileHeader, loc);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加回答
举报