2015-03-25 92 views
1

解壓縮文件,我想從SD卡使用下面的代碼從SD卡

public void unzip(String zipFilePath, String destDirectory, String filename) throws IOException { 

    File destDir = new File(destDirectory); 
     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(filename); 
         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(); 
      } 

上面的代碼是給我的錯誤解壓縮的文件。下面是日誌

java.io.FileNotFoundException: /mnt/sdcard/unZipedFiles/myfile/tt/images.jpg: open failed: ENOENT (No such file or directory) 

在這裏我ziped目錄,其中包含圖像/子目錄,然後我試圖解壓縮。

誰能告訴我原因

感謝

+1

您是否創建了'/ mnt/sdcard/unZipedFiles/myfile/tt /'目錄? – CommonsWare 2015-03-25 11:04:06

+1

ENOENT(沒有這樣的文件或目錄):看看你的文件是否存在 – 2015-03-25 11:05:45

+0

你在清單文件中提到過嗎? Yogendra 2015-03-25 11:08:07

回答

1

您試圖將文件寫入到一個不存在的目錄。這不起作用。在解壓縮時,不僅需要創建文件,還需要創建目錄

以下添加到extractPath()其開行:

filePath.getParentFile().mkdirs(); 

這得到應該包含您需要的文件(filePath.getParentFile())的目錄,然後創建所有必要的子目錄到那裏(mkdirs())。

+0

謝謝。我會嘗試 – Prasad 2015-03-25 12:17:18