How to listfiles() from jar in a tar by using true

2019-08-01 04:45发布

问题:

I am trying to list all files in a tar, including files in jar.

How can I do this by using truezip in Java or other api?

Thanks

回答1:

Using TrueZIP 7, you could use something like this:

public static void main(String args[]) throws IOException {
    // Remember to set to add the following dependencies to the class path:
    // Compile time artifactId(s): truezip-file
    // Run time artifactId(s): truezip-kernel, truezip-driver-file, truezip-driver-tar, truezip-driver-zip
    TFile.setDefaultArchiveDetector(new TDefaultArchiveDetector("tar|zip"));
    search(new TFile(args[0])); // e.g. "my.tar" or "my.zip"
    TFile.umount(); // commit changes
}

private void search(TFile entry) throws IOException {
    if (entry.isDirectory()) {
        for (TFile member : dir.listFiles())
            search(member);
    } else if (entry.isFile()) {
        // [do something with entry]
    } // else is special file or non-existent
}


回答2:

Found a similar thread here in stackoverflow How do I extract a tar file in Java?

Hope the above link helps.

This will list files in a jar. You can use a combination of JarFile and JarEntry to get the list.

JarFile randomJar = new JarFile("randomname.jar");
Enumeration enumEntries = randomJar.entries();
while (enumEntries.hasMoreElements()) {
    JarEntry jarEntry = (JarEntry)enumEntries.nextElement();
    String name = jarEntry.getName();
    System.out.println("Name = " + name );
}

http://download.oracle.com/javase/1.4.2/docs/api/java/util/jar/JarFile.html http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipEntry.html