Apache Commons压缩写入从一个流到另一个流挂起

时间:2017-06-10 23:16:28

标签: java android compression tar apache-commons

我需要将一个tar存档重新打包到另一个。我知道ArchieveEntry已经是一个流,所以我只是从一个流写到另一个流。这是我的代码:

public void repack (String sourceFile, String destFile) throws IOException {

    TarArchiveInputStream tarIn = new TarArchiveInputStream (new BufferedInputStream (new FileInputStream (sourceFile)));
    TarArchiveOutputStream tarOut = new TarArchiveOutputStream (new FileOutputStream (destFile));

    TarArchiveEntry tarEntry;

    while ((tarEntry = tarIn.getNextTarEntry ()) != null) {

        tarOut.putArchiveEntry (tarEntry);

        if (tarEntry.isFile ()) {

            int counter;
            byte byteData[] = new byte[(int) tarEntry.getSize()];

            while ((counter = tarIn.read (byteData, 0, byteData.length)) != -1)
            tarOut.write (byteData, 0, counter);

        }

        tarOut.closeArchiveEntry ();

    }

    tarIn.close ();

    tarOut.flush ();
    tarOut.close ();

}

它没有显示错误,只是挂断了。什么是可以成为一个问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

认为最好的方法是使用IOUtils.copy (tarIn, tarOut);,我只是将代码的循环部分更改为

if (tarEntry.isFile ())
IOUtils.copy (tarIn, tarOut);