Tôi đang cố giải nén một tệp lưu trữ trong java có chứa các thư mục cũng như các tệp bên trong tệp lưu trữ. Vấn đề là nó ném một ngoại lệ FNF bất cứ khi nào nó được vào các thư mục và cố gắng giải nén chúng. đang giải nén của tôi là như sau:Java giải nén lưu trữ nén với các thư mục FileNotFound ngoại lệ
private void unZipUpdate(String pathToUpdateZip, String destinationPath){
byte[] byteBuffer = new byte[1024];
try{
ZipInputStream inZip = new ZipInputStream(new FileInputStream(pathToUpdateZip));
ZipEntry inZipEntry = inZip.getNextEntry();
while(inZipEntry != null){
String fileName = inZipEntry.getName();
File unZippedFile = new File(destinationPath + File.separator + fileName);
System.out.println("Unzipping: " + unZippedFile.getAbsoluteFile());
new File(unZippedFile.getParent()).mkdirs();
FileOutputStream unZippedFileOutputStream = new FileOutputStream(unZippedFile);
int length;
while((length = inZip.read(byteBuffer)) > 0){
unZippedFileOutputStream.write(byteBuffer,0,length);
}
unZippedFileOutputStream.close();
inZipEntry = inZip.getNextEntry();
}
inZipEntry.clone();
inZip.close();
System.out.println("Finished Unzipping");
}catch(IOException e){
e.printStackTrace();
}
}
tôi nghĩ rằng tôi đã nén thư mục xử lý với
new File(unZippedFile.getParent()).mkdirs();
Nhưng điều đó dường như không khắc phục vấn đề. Tôi đang thiếu gì ở đây?
stacktrace:
Unzipping: D:\UnzipTest\aspell
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:47)
at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:33)
at shopupdater.ShopUpdater.main(ShopUpdater.java:67)
"aspell" là một thư mục mà là bên trong các kho lưu trữ.
Tôi cố gắng gợi ý Daniel thêm
unZippedFile.createNewFile();
sau
new File(UnzippedFile.getParent()).mkdirs();
Đó ném một ngoại lệ khác nhau:
Unzipping: D:\UnzipTest\aspell
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:56)
at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:33)
at shopupdater.ShopUpdater.main(ShopUpdater.java:76)
Vui lòng đăng stacktrace. –
@SotiriosDelimanolis được thêm vào bài đăng gốc – user1806716
Tôi khá chắc chắn đó là vì bạn không kiểm tra nếu (inZipEntry.isDirectory()), Nếu đó là một thư mục, hãy thay vì viết byte. – tom