我正在使用Apache Commons 1.4.1庫來壓縮和解壓縮".tar.gz"
文件。如何使用Apache Commons解壓TAR文件
我在最後一位遇到問題 - 將TarArchiveInputStream
轉換爲FileOutputStream
。
奇怪的是,它打破這條線:
FileOutputStream fout = new FileOutputStream(destPath);
destPath
是一個規範的路徑文件:C:\ Documents和Settings \管理員\我的文檔\ JavaWorkspace \ BackupUtility \未解壓\測試\子目錄\ 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建議](http://stackoverflow.com/a/14211580/320399)中,他必須顯式調用'write(...)'並提供已讀入內存的字節數組。 – blong 2013-11-16 22:14:52