我已经成功读取了 tar.gz 格式文件夹中的 pdf 文件。但我遇到了性能问题 - 需要更多时间打开包含 1000 多个小 pdf 文件的 tar.gz 文件夹,每个文件大小为 10 - 25 MB。文件夹的总大小为 2GB
如何提高解压文件读取性能?
FileInputStream fin = new FileInputStream(tarName);
BufferedInputStream in = new BufferedInputStream(fin);
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
TarArchiveInputStream tarIn1 = new TarArchiveInputStream(tarIn);
TarArchiveEntry entry = null;
byte[] buffer = new byte[5024];
int nrBytesRead;
while ((entry = (TarArchiveEntry) tarIn1.getNextEntry()) != null) {
System.out.println("it finds a file "
+ entry.getName().toString());
if (entry.getName().toString().equals(fileName)) {
while ((nrBytesRead = tarIn1.read(buffer)) > 0) {
out.write(buffer, 0, nrBytesRead);
}
break;
}
}
最佳答案
tar.gz 格式不是为随机访问而设计的。它旨在用于一次打开整个包裹的时间。这允许将一系列文件压缩为单个流,通常可以提高压缩率,尤其是对于许多小文件。但是,如果您尝试从中间某处只提取一个文件,则需要先解压缩到该点的所有文件。
对于单个文件的随机访问,您应该考虑使用 zip 格式重新打包。压缩不会那么好,你可以很快地取出单个文件。在 Java 中,查看 ZipFile class .
关于java - 如何提高读取 tar.gz 文件夹中文件的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866245/