java 如何使用 Apache Commons 解压 TAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11431143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to untar a TAR file using Apache Commons
提问by Redandwhite
I'm using the Apache Commons 1.4.1 library to compress and uncompress ".tar.gz"
files.
我正在使用 Apache Commons 1.4.1 库来压缩和解压缩".tar.gz"
文件。
I'm having trouble with the last bit -- converting a TarArchiveInputStream
into a FileOutputStream
.
我在最后一点遇到了麻烦 - 将 aTarArchiveInputStream
转换为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
destPath
是具有规范路径的文件: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();
}
采纳答案by posdef
A couple of general points, why do you do voodoo with the File
constructor, where there is a perfectly usable constructorwhere you can define the name of the File
you want to create and give a parent File?
几个一般点,为什么你用File
构造函数做伏都教,那里有一个完美可用的构造函数,你可以定义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.
其次,我不太确定如何处理 Windows 路径中的空白空间。这可能是您遇到问题的原因。尝试使用我上面提到的构造函数,看看它是否有所作为:(File destPath = new File(dest, tarEntry.getName());
假设这File dest
是一个正确的文件,并且存在并且您可以访问。
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.
第三,在你对一个File
对象做任何事情之前,你应该检查它是否存在以及它是否可以访问。这将最终帮助您查明问题。
回答by tommybee
Your program has the java heap space error. so I think a little change needed. here is code...
您的程序有 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();
}
good luck
祝你好运