2010-10-20 44 views
14

我的應用程序下載約350個文件的zip。 JPG和HTML文件的混合。我寫的功能做得很好,但解壓縮需要永遠。 起初我認爲原因可能是寫入SD卡的速度很慢。但是當我解壓縮與我的手機上的其他應用程序相同的壓縮文件時,它的運行速度更快。有什麼我可以做的優化它嗎?對SD卡的Extrakting Zip速度非常慢。我如何優化性能?

這裏是代碼:

private void extract() { 

    try { 
     FileInputStream inStream = new FileInputStream(targetFilePath); 
     ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(inStream)); 
     ZipEntry entry; 
     ZipFile zip = new ZipFile(targetFilePath); 

        //i know the contents for the zip so i create the dirs i need in advance 
     new File(targetFolder).mkdirs(); 
     new File(targetFolder + "META-INF").mkdir(); 
     new File(targetFolder + "content").mkdir(); 

     int extracted = 0; 

     while((entry = zipStream.getNextEntry()) != null) { 
      if (entry.isDirectory()) { 
       new File(targetFolder + entry.getName()).mkdirs(); 
      } else { 
       FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName()); 
       for (int c = zipStream.read(); c != -1; c = zipStream.read()) { 
        outStream.write(c); 
       } 
       zipStream.closeEntry(); 
       outStream.close(); 

       extracted ++; 
      } 

      publishProgress(""+(int)extracted*100/zip.size()); 
     } 

     zipStream.close(); 
     inStream.close(); 
     // 
     new File(targetFilePath).delete(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

感謝CommonsWare我修改了代碼:

    int size; 
       byte[] buffer = new byte[2048]; 

       FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName()); 
       BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length); 

       while((size = zipStream.read(buffer, 0, buffer.length)) != -1) { 
        bufferOut.write(buffer, 0, size); 
       } 

       bufferOut.flush(); 
       bufferOut.close(); 

很大的性能差異。 非常感謝。

回答

14

您正在讀取和寫入一個字節。考慮一次讀取和寫入更大的塊。

+1

謝謝!那實際上做了把戲。 – notme 2010-10-20 13:02:04

+1

此答案中提供的鏈接無效。 – 2012-10-30 08:47:08

+0

@CommonsWare鏈接已關閉,您能否修復該問題? – Scorchio 2013-08-01 10:51:25

0

只要使用這種方法一次,並相信我一個超快的過程..它將解壓所有的文件,而不會在1秒內跳過任何文件。

public boolean rajDhaniSuperFastUnzip(String inputZipFile, String destinationDirectory) 
     { 
    try { 
     int BUFFER = 2048; 
     List<String> zipFiles = new ArrayList<String>(); 
     File sourceZipFile = new File(inputZipFile); 
     File unzipDestinationDirectory = new File(destinationDirectory); 
     unzipDestinationDirectory.mkdir(); 
     ZipFile zipFile; 
     zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); 
     Enumeration<?> zipFileEntries = zipFile.entries(); 
     while (zipFileEntries.hasMoreElements()) { 
      ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); 
      String currentEntry = entry.getName(); 
      File destFile = new File(unzipDestinationDirectory, currentEntry); 
      if (currentEntry.endsWith(".zip")) { 
       zipFiles.add(destFile.getAbsolutePath()); 
      } 

      File destinationParent = destFile.getParentFile(); 

      destinationParent.mkdirs(); 

      try { 
       if (!entry.isDirectory()) { 
        BufferedInputStream is = 
          new BufferedInputStream(zipFile.getInputStream(entry)); 
        int currentByte; 
        byte data[] = new byte[BUFFER]; 

        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(); 
       } 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } 
     zipFile.close(); 

     for (Iterator<String> iter = zipFiles.iterator(); iter.hasNext();) { 
      String zipName = (String)iter.next(); 
      doUnzip(
       zipName, 
       destinationDirectory + 
        File.separatorChar + 
        zipName.substring(0,zipName.lastIndexOf(".zip")) 
      ); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false ; 
    } 
    return true; 
} 

希望這將幫助你..快樂編碼:)