如何使用Apache Commons解压缩TAR文件

Red*_*ite 6 java compression file-io tar apache-commons

我正在使用Apache Commons 1.4.1库来压缩和解压缩".tar.gz"文件.

我在最后一点遇到麻烦 - 将a转换TarArchiveInputStream成a FileOutputStream.

奇怪的是,它在这条线上打破了:

FileOutputStream fout = new FileOutputStream(destPath);
Run Code Online (Sandbox Code Playgroud)

destPath 是一个具有Canonical路径的文件:C:\ Documents and Settings\Administrator\My Documents\JavaWorkspace\BackupUtility\untarred\Test\subdir\testinsub.txt

出错:

Exception in thread "main" java.io.IOException: The system cannot find the path specified
Run Code Online (Sandbox Code Playgroud)

知道它可能是什么?为什么它无法找到路径?

我正在附上下面的整个方法(其中大部分是从这里解除的).

private void untar(File dest) throws IOException {
    dest.mkdir();
    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {// create a file with the same name as the tarEntry
        File destPath = new File(dest.toString() + System.getProperty("file.separator") + tarEntry.getName());
        System.out.println("working: " + destPath.getCanonicalPath());
        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            FileOutputStream fout = new FileOutputStream(destPath);
            tarIn.read(new byte[(int) tarEntry.getSize()]);
            fout.close();
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}
Run Code Online (Sandbox Code Playgroud)

tom*_*bee 16

您的程序有Java堆空间错误.所以我认为需要做一点改变.这是代码......

public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    dest.mkdir();
    TarArchiveInputStream tarIn = null;

    tarIn = new TarArchiveInputStream(
                new GzipCompressorInputStream(
                    new BufferedInputStream(
                        new FileInputStream(
                            tarFile
                        )
                    )
                )
            );

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {// create a file with the same name as the tarEntry
        File destPath = new File(dest, tarEntry.getName());
        System.out.println("working: " + destPath.getCanonicalPath());
        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            //byte [] btoRead = new byte[(int)tarEntry.getSize()];
            byte [] btoRead = new byte[1024];
            //FileInputStream fin 
            //  = new FileInputStream(destPath.getCanonicalPath());
            BufferedOutputStream bout = 
                new BufferedOutputStream(new FileOutputStream(destPath));
            int len = 0;

            while((len = tarIn.read(btoRead)) != -1)
            {
                bout.write(btoRead,0,len);
            }

            bout.close();
            btoRead = null;

        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
} 
Run Code Online (Sandbox Code Playgroud)

祝好运

  • 反应非常好。但是,应修改以下 `deskPath.createNewFile();` 以创建父目录 `if (!destPath.getParentFile().exists()) { destPath.getParentFile().mkdirs(); } destPath.createNewFile();` (2认同)

pos*_*def 5

几个一般要点,为什么你用File构造函数做voodoo ,哪里有一个完全可用的构造函数,你可以在其中定义File你想要创建的名称并给出一个父文件?

其次,我不太确定在Windows中的路径中如何处理空白空间.这可能是你问题的原因.尝试使用我上面提到的构造函数,看看它是否有所不同:( File destPath = new File(dest, tarEntry.getName());假设这File dest是一个正确的文件,并且存在且可供您访问.

第三,在对File对象执行任何操作之前,应检查它是否存在以及是否可访问.这最终将帮助您找出问题所在.