2015-03-25 85 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