【问题标题】:Apache commons untar: IllegalArgumentException with getNextTarEntryApache commons untar: IllegalArgumentException with getNextTarEntry
【发布时间】:2012-03-09 09:16:20
【问题描述】:

compress apache library 有问题。我想解压一个包含二进制文件的档案。代码如下:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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

public class ArchiveManager {

    public static final int BUFFER_MAX = 2048;

    public static void untar(String fileName, String targetPath) throws IOException {
        File tarArchiveFile = new File(fileName);
        BufferedOutputStream dest = null;
        FileInputStream tarArchiveStream = new FileInputStream(tarArchiveFile);
        TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream));
        TarArchiveEntry entry = null;
        try {
            while ((entry = tis.getNextTarEntry()) != null) {
                int count;
                File outputFile = new File(targetPath, entry.getName());

                if (entry.isDirectory()) { // entry is a directory
                    if (!outputFile.exists()) {
                        outputFile.mkdirs();
                    }
                } else { // entry is a file
                    byte[] data = new byte[BUFFER_MAX];
                    FileOutputStream fos = new FileOutputStream(outputFile);
                    dest = new BufferedOutputStream(fos, BUFFER_MAX);
                    while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if (dest != null) {
                dest.flush();
                dest.close();
            }
            tis.close();
        }
    }
}

解压缩二进制文件时,getNextTarEntry() 抛出异常:

java.lang.IllegalArgumentException: Invalid byte 111 at offset 0 in 'o.txt{NUL}{NUL}{NUL}' len=8
    at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:99)
    at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:786)
    at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:308)
    at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:198)
    at com.airbus.pakito.download.ArchiveManager.untar(ArchiveManager.java:22)

我试图解压简单的文本文件。解压最后一个文件后,getNextTarEntry() 不会返回 null,而是返回一个带有空文件的对象。所以entry.getName() 是空的,显然new FileOutputStream(outputFile) 无法创建文件。

java.io.FileNotFoundException: C:\Temp (Accès refusé)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.airbus.pakito.util.Archive.untar(Archive.java:32)

你知道问题出在哪里吗?

谢谢。

【问题讨论】:

    标签: java apache compression tar illegalargumentexception


    【解决方案1】:

    我终于知道问题出在哪里了。

    这是a known bug,在 Apache compress 的 1.4 版本中已修复。我希望这个版本是available very soon

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 1970-01-01
      • 2011-01-04
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多