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

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

问题描述

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



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

奇怪的是,它突破了这一行:

  FileOutputStream fout = new FileOutputStream中(destPath); 






destPath 是与使用规则路径文件:C:\Documents和Settings\Administrator\My Documents\JavaWorkspace\BackupUtility\untarred\Test\subdir\testinsub.txt



产生的错误:

 线程main中的异常java.io.IOException :系统找不到指定的路径

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



我附上了下面的整个方法(其中大部分是从此处

pre $私有无效untar(File dest)抛出IOException {
dest.mkdir();
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
// tarIn是一个TarArchiveInputStream
(tarEntry!= null){//创建一个与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 destPath = new File(dest,tarEntry.getName()); (假设文件目录是一个合适的文件,存在并且可以被你访问。

第三,在你用 File 对象,您应该检查它是否存在以及是否可以访问,这将最终帮助您找出问题所在。


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

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 is a File with a Canonical path of: C:\Documents and Settings\Administrator\My Documents\JavaWorkspace\BackupUtility\untarred\Test\subdir\testinsub.txt

Error produced:

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

解决方案

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?

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.

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天全站免登陆