简体   繁体   English

如何在java中列出tar文件的所有条目?

[英]How to list all the entries of a tar file in java?

I want to list all the entries of a tar file in my Java program. 我想在我的Java程序中列出tar文件的所有条目。 How is it possible ? 这怎么可能 ? For zip files, I can use the below code: 对于zip文件,我可以使用以下代码:

ZipFile zf = new ZipFile("ReadZip.zip");
Enumeration entries = zf.entries();
while (entries.hasMoreElements()) {.....}

But I am not sure for the tar files. 但我不确定tar文件。 Can anybody help? 有人可以帮忙吗? I am using org.apache.tools.tar.* 我正在使用org.apache.tools.tar.*

Apache Commons Compress (http://commons.apache.org/compress/) is easy to use. Apache Commons Compress(http://commons.apache.org/compress/)易于使用。

Here's an example of reading a tar's entries: 以下是读取tar条目的示例:

import java.io.FileInputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

public class Taread {
    public static void main(String[] args) {
        try {
            TarArchiveInputStream tarInput = new TarArchiveInputStream(new FileInputStream(args[0]));
            TarArchiveEntry entry;
            while (null!=(entry=tarInput.getNextTarEntry())) {
                System.out.println(entry.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This API is very similar to using Java's own ZipInputStream . 此API与使用Java自己的ZipInputStream非常相似。

To start off: 首先:

TarInputStream tis = new TarInputStream(new FileInputStream("myfile.tar"));
try
{
    TarEntry entry;
    do
    {
        entry = tis.getNextEntry();

        //Do something with the entry
    }
    while (entry != null);
}
finally
{
    tis.close();
}

More examples with different APIs are [here][2].

you were using the ZIP file code for listing the directories.... 您正在使用ZIP文件代码列出目录....

tar file for linux you can check out 你可以查看linux的tar文件

http://www.java2s.com/Code/Java/File-Input-Output/TapeArchiveListerTarfile.htm http://www.java2s.com/Code/Java/File-Input-Output/TapeArchiveListerTarfile.htm

for further references.... 进一步参考....

To read a Java .jar file, you can use the "jar" tool ... or Unzip. 要读取Java .jar文件,可以使用“jar”工具...或解压缩。 .Jar files are in .Zip format. .Jar文件采用.Zip格式。

To read a *nix .tar file, however, you need to use the "tar" tool. 但是,要读取* nix .tar文件,您需要使用“tar”工具。

If you're on Windows, I'd encourage you to try 7-Zip. 如果你在Windows上,我建议你试试7-Zip。 It's a handy tool that recognizes a zillion formats ... including both .zip (hence also .jar) and tar: 它是一个方便的工具,可以识别多种格式......包括.zip(因此也是.jar)和tar:

http://www.7-zip.org/ http://www.7-zip.org/

If you must do it programmatically, I guess the Apache Ant API "tar" is a good way to go. 如果你必须以编程方式进行,我想Apache Ant API“tar”是一个很好的方法。 It gives you a list of "TarEntry": 它为您提供了“TarEntry”列表:

http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/tar/TarEntry.html http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/tar/TarEntry.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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