我正在使用 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/