Tôi đang sử dụng thư viện Apache Commons 1.4.1 để nén và giải nén các tệp ".tar.gz"
.Cách gỡ bỏ tệp TAR bằng cách sử dụng Apache Commons
Tôi đang gặp sự cố với bit cuối cùng - chuyển đổi TarArchiveInputStream
thành FileOutputStream
.
Lạ lùng thay, nó phá vỡ vào dòng này:
FileOutputStream fout = new FileOutputStream(destPath);
destPath
là một tập tin với một con đường Canonical của: C: \ Documents and Settings \ Administrator \ My Documents \ JavaWorkspace \ BackupUtility \ untarred \ Test \ subdir \ testinsub.txt
Lỗi sản xuất:
Exception in thread "main" java.io.IOException: The system cannot find the path specified
Bất kỳ ý tưởng nó có thể là gì? Và tại sao nó không thể tìm ra con đường?
Tôi đang đính kèm toàn bộ phương pháp bên dưới (hầu hết trong số đó được gỡ bỏ từ 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();
}
Xấu hổ khi hỏi điều này, nhưng tôi đã thử sử dụng mẫu mã của bạn và thấy nó hoạt động với một tệp 'gzip' cụ thể mà tôi đang làm việc. Làm thế nào nó hoạt động mà không có bất kỳ cuộc gọi đến 'fout.write (...)' cho nội dung đọc trên inputStream? Trong [answer @ user1894600 gợi ý] (http://stackoverflow.com/a/14211580/320399), anh ta phải gọi 'write (...)' và cung cấp mảng byte đã được đọc vào bộ nhớ. – blong