【问题标题】:Decompressing tar file with Apache Commons Compress使用 Apache Commons Compress 解压缩 tar 文件
【发布时间】:2013-09-04 15:23:54
【问题描述】:

我正在使用 Apache Commons Compress 创建 tar 存档并解压缩它们。我的问题始于这种方法:

    private void decompressFile(File file) throws IOException {
    logger.info("Decompressing " + file.getName());

    BufferedOutputStream outputStream = null;
    TarArchiveInputStream tarInputStream = null;

    try {
        tarInputStream = new TarArchiveInputStream(
                new FileInputStream(file));

        TarArchiveEntry entry;
        while ((entry = tarInputStream.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                File compressedFile = entry.getFile();
                File tempFile = File.createTempFile(
                        compressedFile.getName(), "");

                byte[] buffer = new byte[BUFFER_MAX_SIZE];
                outputStream = new BufferedOutputStream(
                        new FileOutputStream(tempFile), BUFFER_MAX_SIZE);

                int count = 0;
                while ((count = tarInputStream.read(buffer, 0, BUFFER_MAX_SIZE)) != -1) {
                    outputStream.write(buffer, 0, count);
                }
            }

            deleteFile(file);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null) {
            outputStream.flush();
            outputStream.close();
        }
    }
}

每次我运行代码时,compressedFile 变量为空,但 while 循环正在遍历我的测试 tar 中的所有条目。

你能帮我理解我做错了什么吗?

【问题讨论】:

  • 尝试像这样创建输出流: outputStream= new FileOutputStream(new File(entry.getName()));
  • 我需要创建一个临时文件。我可以使用 File.createTempFile 代替 new File 吗?
  • 我在下面回复了一个完整的答案...

标签: java apache-commons apache-commons-compress


【解决方案1】:

来自官方文档
从 tar 存档中读取条目:

    TarArchiveEntry entry = tarInput.getNextTarEntry();
    byte[] content = new byte[entry.getSize()];
    LOOP UNTIL entry.getSize() HAS BEEN READ {
        tarInput.read(content, offset, content.length - offset);
    }

我已经编写了一个示例,从您的实现开始,并使用一个非常简单的 .tar(只有一个文本条目)进行测试。
不知道确切的要求,我只是负责解决读取存档的问题,避免使用空指针。调试中,你也找到了这个入口

    private static void decompressFile(File file) throws IOException {

        BufferedOutputStream outputStream = null;
        TarArchiveInputStream tarInputStream = null;

        try {
            tarInputStream = new TarArchiveInputStream(
                new FileInputStream(file));

            TarArchiveEntry entry;
            while ((entry = tarInputStream.getNextTarEntry()) != null) {
                if (!entry.isDirectory()) {
                    File compressedFile = entry.getFile();
                    String name = entry.getName();

                    int size = 0;
                    int c;
                    while (size < entry.getSize()) {
                        c = tarInputStream.read();
                        System.out.print((char) c);
                        size++;
                }
    (.......)

正如我所说:我使用仅包含文本条目的 tar 进行了测试(您也可以尝试这种方法来验证代码),以确保避免出现空值。
您需要根据您的实际需求进行所有必要的调整。 很明显,您必须按照我在顶部发布的元代码中的方式处理流。
它显示了如何处理单个条目。

【讨论】:

  • compressedFile的目的是什么?
【解决方案2】:

尝试使用getNextEntry() 方法而不是getNextTarEntry() 方法。

第二种方法返回一个 TarArchiveEntry。可能这不是你想要的!

【讨论】:

  • 我正在使用 tar 文件,所以是的,这就是我想要的。无论如何,我稍后会尝试您的解决方案。感谢您的回答;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2012-07-10
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
相关资源
最近更新 更多