2010-10-20 19 views
14

ứng dụng của tôi tải xuống một tệp zip với khoảng 350 tệp. Kết hợp tệp JPG và HTML. Các chức năng tôi đã viết để làm điều đó hoạt động tốt nhưng giải nén mất bao giờ hết. Lúc đầu, tôi nghĩ lý do có thể là việc ghi vào thẻ sd chậm. nhưng khi tôi giải nén cùng một zip với một ứng dụng khác trên điện thoại của tôi, nó hoạt động nhanh hơn nhiều. có điều gì mà tôi có thể làm để tối ưu hóa nó không?Extrakting Zip vào SD-Card rất chậm. Làm thế nào tôi có thể tối ưu hóa hiệu suất?

đây là mã:

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(); 
    } 
} 

nhờ CommonsWare tôi sửa đổi mã của tôi như thế này:

    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(); 

lớn khác biệt hiệu suất. Cảm ơn rất nhiều.

Trả lời

14

Bạn đang đọc và viết một byte tại một thời điểm. Cân nhắc đọc và viết một khối lớn hơn tại một thời điểm.

+1

CẢM ƠN! Điều đó thực sự đã làm các trick. – notme

+1

Liên kết được cung cấp trong câu trả lời này không hoạt động. –

+0

@CommonsWare Liên kết bị lỗi, bạn có thể sửa lỗi đó không? – Scorchio

0

Chỉ cần sử dụng phương pháp này một lần và tin tôi là một quá trình siêu nhanh. Nó sẽ giải nén tất cả các tệp mà không bỏ qua bất kỳ tệp nào trong 1 giây.

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; 
} 

Hy vọng điều này sẽ giúp bạn .. Chúc mừng Mã hóa :)

Các vấn đề liên quan