问题
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