我正在尝试使用 Apache Commons compress 来读取 7-zip 文件的内容。我对阅读/提取内容不感兴趣,我只想获取所有条目的列表。
我编写了这段代码,但对于 4MB 的存档,读取整个文件需要 6 秒。
public static void main(String[]args) throws IOException{
File sevenz = new File("testfile.7z");
System.out.println("Reading 7-zip...");
SevenZFile sevenZFile = new SevenZFile(sevenz);
long s = System.currentTimeMillis();
SevenZArchiveEntry entry;
while((entry=sevenZFile.getNextEntry())!=null){
System.out.print(entry.isDirectory()?"Dir":"File");
System.out.print("\t");
System.out.print("*********.***"); //entry.getName();
System.out.print("\t");
System.out.println(entry.getHasCrc()?"CRC":"NO-CRC");
}
System.out.println("------------------------------");
System.out.println("7-zip\t"+(System.currentTimeMillis()-s)+" ms to read.");
}
输出为:
Reading 7-zip...
File *********.*** CRC
File *********.*** CRC
File *********.*** CRC
File *********.*** CRC
File *********.*** CRC
------------------------------
7-zip 6236 ms to read.
文件列出过程是否应该花费这么长时间,还是我做错了什么? 我也尝试删除所有打印,但读取文件所需的时间是相同的。
最佳答案
这看起来确实有点偏高。我要做的第一件事就是消除多余的精力,只花时间阅读部分。
这意味着注释掉循环内的所有 System.out.println
命令:
while ((entry = sevenZFile.getNextEntry()) != null) {
}
System.out.println("total\t" + (System.currentTimeMillis()-s) + " ms.");
这样做,看看是否会有所不同。这将告诉您是条目扫描本身还是打印和/或从每个条目中提取数据。
除此之外,您还可以通过以下方式了解每次迭代所花费的时间:
while ((entry = sevenZFile.getNextEntry()) != null) {
long s2 = System.currentTimeMillis();
System.out.println("entry\t" + (s2-s) + " ms.");
s = s2;
}
我模糊记得 Apache Commons Compress 在启动时读取了整个条目列表,根据源代码 here 看来情况就是如此。 .
一种可能性是获取该源代码,暂时将其合并到您自己的代码中,然后对其进行分析以查看实例化过程中大部分时间都花在哪里。
关于java - 7-zip 存档内的文件列表需要几秒钟才能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26273285/