2

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

Tom
  • 369
  • 3
  • 5
  • 17

2 Answers2

5

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
}
Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47
0

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

Community
  • 1
  • 1
kensen john
  • 5,439
  • 5
  • 28
  • 36
  • 1
    hi, thanks for replying, I think it's a little bit different, I am listing files under a tar – Tom Mar 22 '11 at 21:15
  • Found another thread asking the same question. Looks like they have some answers: [How do I extract a tar file in Java?](http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java) – kensen john Mar 23 '11 at 03:58