【问题标题】:Java unzip compressed archive with folders FileNotFound exceptionJava 解压缩带有文件夹 FileNotFound 异常的压缩档案
【发布时间】:2013-10-15 20:20:45
【问题描述】:

我正在尝试在 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:\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)

【问题讨论】:

  • 请发布堆栈跟踪。
  • @SotiriosDelimanolis 添加到原帖
  • 我很确定这是因为您没有检查 if (inZipEntry.isDirectory()),如果它是一个目录,则创建它而不是写入字节。

标签: java io filenotfoundexception


【解决方案1】:

试试这个代码,它可以在我的机器上运行(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();
       }
    }

【讨论】:

  • 那到底应该在哪里?文件输出流初始化之前?
  • 我尝试了更新,它以某种方式混淆了文件和目录。希望它在 Windows 机器上也能以同样的方式工作。
【解决方案2】:

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

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 的空文件。在下一次迭代中,您尝试创建一个具有相同名称的目录,但失败了,可能是默默地失败了,导致后来的失败。

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 2013-03-09
    • 1970-01-01
    • 2010-09-05
    • 2010-12-14
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多