2017-09-14 111 views
0

在我的應用程序之一,我需要提取一個zip文件,裏面有文件夾,該文件夾包含圖像它的意思abc.zip => adb(folder)=> abc png格式 我想提取圖像文件android解壓縮文件夾從zip文件並從該文件夾中讀取內容

我用下面的方法

private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException 
    { 
     int BUFFER = 8192; 
     File file = zipFile; 
     //This can throw ZipException if file is not valid zip archive 
     ZipFile zip = new ZipFile(file); 
//  String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName()); 
     String newPath = destination.getAbsolutePath() + File.separator + zipFile.getName(); 
     //Create destination directory 
     new File(newPath).mkdir(); 
     Enumeration zipFileEntries = zip.entries(); 

     //Iterate overall zip file entries 
     while (zipFileEntries.hasMoreElements()) 
     { 
      ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); 
      String currentEntry = entry.getName(); 
      File destFile = new File(newPath, currentEntry); 
      File destinationParent = destFile.getParentFile(); 
      //If entry is directory create sub directory on file system 
      destinationParent.mkdirs(); 

      if (!entry.isDirectory()) 
      { 
       //Copy over data into destination file 
       BufferedInputStream is = new BufferedInputStream(zip 
         .getInputStream(entry)); 
       int currentByte; 
       byte data[] = new byte[BUFFER]; 
       //orthodox way of copying file data using streams 
       FileOutputStream fos = new FileOutputStream(destFile); 
       BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); 
       while ((currentByte = is.read(data, 0, BUFFER)) != -1) { 
        dest.write(data, 0, currentByte); 
       } 
       dest.flush(); 
       dest.close(); 
       is.close(); 
      } 
     } 
     return true;//some error codes etc. 
    } 

但得到zipfolder/foldername/ (Is a directory)

回答

3
private void unzip(String src, String dest){ 

     final int BUFFER_SIZE = 4096; 

     BufferedOutputStream bufferedOutputStream = null; 
     FileInputStream fileInputStream; 
     try { 
      fileInputStream = new FileInputStream(src); 
      ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream)); 
      ZipEntry zipEntry; 

      while ((zipEntry = zipInputStream.getNextEntry()) != null){ 

       String zipEntryName = zipEntry.getName(); 

       String name = dest.substring(dest.lastIndexOf("/")-1); 

       File FileName = new File(FolderName); 
       if (!FileName.isDirectory()) { 
        try { 
         if (FileName.mkdir()) { 
         } else { 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 

       File file = new File(FolderName+"/" +zipEntryName); 

       if (file.exists()){ 

       } else { 
        if(zipEntry.isDirectory()){ 
         file.mkdirs(); 
        }else{ 
         byte buffer[] = new byte[BUFFER_SIZE]; 
         FileOutputStream fileOutputStream = new FileOutputStream(file); 
         bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE); 
         int count; 

         while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) { 
          bufferedOutputStream.write(buffer, 0, count); 
         } 

         bufferedOutputStream.flush(); 
         bufferedOutputStream.close(); 
        } 
       } 
      } 
      zipInputStream.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

嘗試這個代碼,它是爲我工作。

+0

謝謝你爲我工作也 –

相關問題