java - 如何使用 Apache Commons 解压 TAR 文件

标签 java file-io tar apache-commons compression

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

我在最后一点遇到了麻烦——将 TarArchiveInputStream 转换为 FileOutputStream

奇怪的是,它在这条线上中断了:

FileOutputStream fout = new FileOutputStream(destPath);

destPath 是一个具有规范路径的文件: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

知道它可能是什么吗?为什么找不到路径?

我在下面附加了整个方法(其中大部分是从 here 中提取的)。

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();
}

最佳答案

您的程序有 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();
} 

祝你好运

关于java - 如何使用 Apache Commons 解压 TAR 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11431143/

相关文章:

php - PharData 文件名长度限制

java - 从文件数据中提取特定段落的最佳方法

java - MySql 数据库连接错误 Managed ConnectionFactory is null

java - 我可以使用 Java 打开 Windows Enterprise 上用户主目录中的文件吗?

tar - Autotools-tar这看起来不像tar存档

python-3.x - 在 Google-Colab 中提取 tar.gz 文件时遇到问题

Java,用冒号分割输入文件

java - 从较长的文件路径中获取文件路径的一部分

c - Strtol 未返回正确的 endptr - C

c++ - IO 文件代码不一致