2016-05-13 94 views
1

我试图用java.util.zip包解压压缩文件夹:错误而提取zip文件

现在我的压缩文件夹结构为: 我的压缩文件夹的名字是classes.zip 里面这个zip文件夹我有一个类文件夹中,我有子文件夹以及文件: enter image description here

如果你的WWW文件夹中走的更远然后再它的子文件夹,这是一个Java包,我有包装结构文件夹内。类文件。

现在我想解压缩这个压缩文件夹,我的代码是: package www.eor.com;

/** 

* A console application that tests the UnzipUtility class 
* 
*/ 
public class UnzipUtilityTest { 
    public static void main(String[] args) { 
     String zipFilePath = "D:/classes.zip"; 
     String destDirectory = "D:/Dojo"; 
     UnzipUtility unzipper = new UnzipUtility(); 
     try { 
      unzipper.unzip(zipFilePath, destDirectory); 
     } catch (Exception ex) { 
      // some errors occurred 
      ex.printStackTrace(); 
     } 
    } 
} 

和支撑类:

package www.eor.com; 

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

/** 
* This utility extracts files and directories of a standard zip file to 
* a destination directory. 
*/ 
public class UnzipUtility { 
    /** 
    * Size of the buffer to read/write data 
    */ 
    private static final int BUFFER_SIZE = 4096; 
    /** 
    * Extracts a zip file specified by the zipFilePath to a directory specified by 
    * destDirectory (will be created if does not exists) 
    * @param zipFilePath 
    * @param destDirectory 
    * @throws IOException 
    */ 
    public void unzip(String zipFilePath, String destDirectory) throws IOException { 
     File destDir = new File(destDirectory); 
     if (!destDir.exists()) { 
      destDir.mkdir(); 
     } 
     ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); 
     ZipEntry entry = zipIn.getNextEntry(); 
     // iterates over entries in the zip file 
     while (entry != null) { 
      String filePath = destDirectory + File.separator + entry.getName(); 
      if (!entry.isDirectory()) { 
       // if the entry is a file, extracts it 
       extractFile(zipIn, filePath); 
      } else { 
       // if the entry is a directory, make the directory 
       File dir = new File(filePath); 
       dir.mkdir(); 
      } 
      zipIn.closeEntry(); 
      entry = zipIn.getNextEntry(); 
     } 
     zipIn.close(); 
    } 
    /** 
    * Extracts a zip entry (file entry) 
    * @param zipIn 
    * @param filePath 
    * @throws IOException 
    */ 
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { 
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); 
     byte[] bytesIn = new byte[BUFFER_SIZE]; 
     int read = 0; 
     while ((read = zipIn.read(bytesIn)) != -1) { 
      bos.write(bytesIn, 0, read); 
     } 
     bos.close(); 
    } 
} 

现在,当我运行UnzipUtilityTest类它给我的异常如:

java.io.FileNotFoundException: D:\Dojo\classes\camel-config-xml.xml (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:110) 
    at www.cognizant.com.UnzipUtility.extractFile(UnzipUtility.java:59) 
    at www.cognizant.com.UnzipUtility.unzip(UnzipUtility.java:41) 
    at www.cognizant.com.UnzipUtilityTest.main(UnzipUtilityTest.java:16) 

为什么它给这个异常以及如何纠正这个问题?

+0

错误消息显然会告诉你有一个文件无法找到。检查文件是否存在于给定位置。看看你的第一张截图,它看起来像是名为'D:\ classes \ classes'而不是'D:\ Dojo \ classes'。 – Jesper

+0

尝试更换 ZipInputStream zipIn =新ZipInputStream(新的FileInputStream(zipFilePath)) 到 ** ZipInputStream zipIn =新ZipInputStream(新的FileInputStream(新文件(zipFilePath))); ** – emotionlessbananas

+0

感谢您的快速回复加斯帕和HolidayCoder。 :)你们真棒 – Roy

回答

5

这可能是由于文件的父母classes/不存在,所以无法在其中创建文件。

当您解压缩zip的条目时,必须为该文件创建父文件夹文件夹。一个zip文件不一定包含每个文件夹的条目。但是,zip中的每个条目的格式都是path/to/entry/filename.ext,因此您可以派生条目的父路径并相应地创建父文件夹。

所以提取文件之前,做

new File(filePath).getParent().mkdirs() 
+0

感谢洛特杰拉尔德..你是一个真正的救星..是的,这是确切的问题,你的方向解决了我的问题.. – Roy