Java 解压缩压缩存档与文件夹 FileNotFound 异常



我正在尝试在java中解压缩一个存档,其中包含文件夹以及存档中的文件。问题是每当它到达文件夹并尝试解压缩它们时,它都会引发 FNF 异常。我的解压缩代码如下:

 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:UnzipTestaspell
java.io.FileNotFoundException: D:UnzipTestaspellamerican-w-accents.alias (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
Unzipping: D:UnzipTestaspellamerican-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:UnzipTestaspell
Unzipping: D:UnzipTestaspellamerican-w-accents.alias
java.io.FileNotFoundException: D:UnzipTestaspellamerican-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();
       }
    }

您似乎首先将目录作为文件处理,并创建一个阻止创建目录的空文件。

Unzipping: D:UnzipTestaspell
Unzipping: D:UnzipTestaspellamerican-w-accents.alias
java.io.FileNotFoundException: D:UnzipTestaspellamerican-w-accents.alias

很难完全确定,但这就是它的样子。第一行"解压缩:"来自代码创建名为 D:UnzipTestaspell 的空文件时。在下一次迭代中,您尝试创建一个具有相同名称的目录,但失败了,可能是静默的,导致后来的失败。

最新更新