【发布时间】:2012-07-10 23:33:57
【问题描述】:
我正在使用 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();
}
【问题讨论】:
-
不好意思问这个问题,但我尝试使用您的代码示例并看到它在给定我正在使用的特定
gzip文件的情况下工作。考虑到在 inputStream 上读取的内容,它如何在不调用fout.write(...)的情况下工作?在answer @user1894600 suggests中,他要显式调用write(...),并提供已经读入内存的字节数组。
标签: java file-io tar apache-commons compression