2015-03-25 5 views
1

Я пытаюсь распаковать файл из SDCard используя ниже кодРазархивируйте файл с SDcard

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

+1

ENOENT (Нет такого файла или каталога): есть ли файл в вашем файле или нет –

+0

Вы упомянули в своем файле манифеста. Yogendra

ответ

1

Вы пытаетесь записать файлы в каталог, который не существует. Это не будет работать. Вам нужно не только создать файлы , когда вы открываете файл unZIPping, вам необходимо создать каталоги.

Добавьте следующее extractPath() в качестве вводной линии:

filePath.getParentFile().mkdirs(); 

Это получает каталог, который должен содержать нужный вам файл (filePath.getParentFile()), а затем создает все необходимые подкаталоги, чтобы получить там (mkdirs()).

+0

Спасибо. я попробую – Prasad

Смежные вопросы