如何使用 Apache Commons 解压 TAR 文件 [英] How to untar a TAR file using Apache Commons

查看:59
本文介绍了如何使用 Apache Commons 解压 TAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Apache Commons 1.4.1 library to compress and uncompress ".tar.gz" files.

我在最后一点遇到了问题——将 TarArchiveInputStream 转换为 FileOutputStream.

I'm having trouble with the last bit -- converting a TarArchiveInputStream into a FileOutputStream.

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

Oddly enough, it's breaking on this line:

FileOutputStream fout = new FileOutputStream(destPath);

<小时>

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


destPath is a File with a Canonical path of: 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

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

Any idea what it could be? And why is it not able to find the path?

我在下面附上了整个方法(其中大部分来自 此处).

I'm attaching the whole method below (most of which is lifted from 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();
}

推荐答案

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

A couple of general points, why do you do voodoo with the File constructor, where there is a perfectly usable constructor where you can define the name of the File you want to create and give a parent File?

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

Secondly I am not too sure how empty spaces are handled in paths in windows. It might be the cause of your problems. Try using the constructor I mentioned above and see if it makes a difference: File destPath = new File(dest, tarEntry.getName()); (assuming that File dest is a proper file, and exists and is accessible by you.

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

Third, before you do anything with a File object you should check if it exists and if it is accessible. That will ultimately help you pinpoint the problem.

这篇关于如何使用 Apache Commons 解压 TAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆