简体   繁体   中英

How to listfiles() from jar in a tar by using truezip in Java?

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

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM