How to list the contents of a compressed tar file in Java

淺唱寂寞╮ 提交于 2019-12-07 07:00:00

问题


How do I list the contents of a compressed tar file in Java without extracting the files? I have looked at apache ant api, and I can see how to extract files, but I cannot work out how to just list them. The tar files are either bz2 or gz.


回答1:


I recommend using TrueZIP. It's really good.

TrueZIP is a framework for virtual file systems and a library for accessing archive files as if they were just plain old directories.

As a framework, TrueZIP provides the interfaces and classes to write file system drivers which plug-in to its federated file system space.

As a library, TrueZIP provides convenient multi-threaded read/write access to archive files as if they were just plain old directories in a file system path.

It's an excellent, fast library. It has TAR drivers, and can handle *.tar.gz and *.tar.bz2 files. The JavaDocs are clear and complete, though I found the standalone tutorials insufficient - I had to read the JavaDoc to get things working.

...Which brings me to the only downside I found: it might take a little while to wrap your head around TrueZIP. Initially, I had issues getting anything to work, but it "clicked" after a little while and I had no more difficulties.




回答2:


TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(tempFile));
ArchiveEntry entry = tin.getNextEntry();
while (entry != null) {
  System.out.println("File Name " + entry.getName());//List file name      
}


来源:https://stackoverflow.com/questions/5320164/how-to-list-the-contents-of-a-compressed-tar-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!