我正在尝试使用Apache commons compress版本 1.9 解压缩 5G 文本文件...
public String unZipFile(String pathFileName, String saveFilePath) throws IOException {
SevenZFile sevenZFile = new SevenZFile(new File(pathFileName));
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
try {
while (entry != null) {
if ("deudores.txt".equals(entry.getName())) {
Instant now = Instant.now();
LOG.info("\tunzipping: {} -> {}/{}", pathFileName, saveFilePath, entry.getName());
byte[] buffer = new byte[1024];
File newFile = newFile(new File(saveFilePath), entry.getName());
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = sevenZFile.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
LOG.info("\tunzipping done, took {} secs", now.until(Instant.now(), ChronoUnit.SECONDS));
return saveFilePath+"/"+entry.getName();
}
entry = sevenZFile.getNextEntry();
}
} finally {
sevenZFile.close();
}
return null;
}
public File newFile(File destinationDir, String name) throws IOException {
File destFile = new File(destinationDir, name);
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + name);
}
return destFile;
}
我只得到前 1 GB
有什么建议/替代方案吗?交换到另一个库不是问题。
解决了...对缓冲区的迭代是错误的。