Java解压缩文件夹压缩存档FileNotFound异常
我想解压缩包含文件夹以及文件内的文件的Java存档。问题在于,只要到达文件夹并尝试将其解压缩,就会引发FNF异常。我解压的代码如下:Java解压缩文件夹压缩存档FileNotFound异常
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();
}
}
我想我已经压缩与
new File(unZippedFile.getParent()).mkdirs();
处理的文件夹,但是,这似乎并没有解决问题。我在这里错过了什么?
堆栈跟踪:
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” 是,是存档中的文件夹。
我想丹尼尔的建议添加
unZippedFile.createNewFile();
的
new File(UnzippedFile.getParent()).mkdirs();
抛出一个不同的异常后:
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)
试试这个代码,它的工作原理在我的机器上(Ubuntu的)
private static 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());
if (inZipEntry.isDirectory()){
unZippedFile.mkdirs();
}else{
new File(unZippedFile.getParent()).mkdirs();
unZippedFile.createNewFile();
FileOutputStream unZippedFileOutputStream = new FileOutputStream(unZippedFile);
int length;
while((length = inZip.read(byteBuffer)) > 0){
unZippedFileOutputStream.write(byteBuffer,0,length);
}
unZippedFileOutputStream.close();
}
inZipEntry = inZip.getNextEntry();
}
//inZipEntry.close();
inZip.close();
System.out.println("Finished Unzipping");
}catch(IOException e){
e.printStackTrace();
}
}
究竟应该在哪里?在fileoutputstream的初始化之前? – user1806716
我更新了我的答案 – Daniel
我尝试了更新,它以某种方式混淆了文件和目录。希望它能以同样的方式在Windows机器上运行。 – Daniel
看起来您正在将目录作为文件首先处理并创建一个空文件,以防止创建该目录。
Unzipping: D:\UnzipTest\aspell
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias
很难完全确定,但这就是它的样子。第一个“Unzipping:”行来自你的代码创建一个名为D:\UnzipTest\aspell
的空文件。在下一次迭代中,您试图创建一个名称相同的目录,并且失败了,可能是默默无闻,导致以后的失败。
请发布堆栈跟踪。 –
@SotiriosDelimanolis已添加到原文 – user1806716
我敢肯定,这是因为你没有检查if(inZipEntry.isDirectory()),如果它是一个目录,使它而不是写字节。 – tom