我正在使用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
有任何想法是什么?为什么它找不到路径?
我在下面附上整个方法(其中大部分内容摘自这里)。
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();
}
gzip
文件上工作。既然从输入流中读取了内容,为什么没有调用fout.write(...)
就能正常工作呢?在用户@user1894600的回答中,他必须显式调用write(...)
并提供已读入内存的字节数组。 - blong