我正在尝试列出 tar 中的所有文件,包括 jar 中的文件。
如何通过在 Java 或其他 api 中使用 truezip 来做到这一点?
谢谢
使用 TrueZIP 7,您可以使用如下内容:
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
}
在 stackoverflow 中找到了一个类似的线程 如何在 Java 中提取 tar 文件?
希望上面的链接有帮助。
这将列出 jar 中的文件。您可以使用 JarFile 和 JarEntry 的组合来获取列表。
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