2016-05-29 48 views
2

所以我正在做一個遊戲的下載器和解壓器,但我無法獲得解壓縮的進度。這是我的解壓縮方法:在java中解壓縮文件的進度

static public void unzip(cachePart name) throws ZipException, IOException { 
     System.out.println("Unzipping " + name.getFile()); 
     int BUFFER = 2048; 
     long current = 0; 
     File file = new File(name.getFilePath()); 

     ZipFile zip = new ZipFile(file); 
     //String newPath = zipFile.substring(0, zipFile.length() - 4); 

     int finalSize = zip.size(); 

     //new File(newPath).mkdir(); 
     Enumeration<?> zipFileEntries = zip.entries(); 

     // Process each entry 
     while (zipFileEntries.hasMoreElements()) { 

      // grab a zip file entry 
      ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); 
      current += entry.getCompressedSize(); 
      //long finalSize = entry.getSize(); 
      int percentage; 
      String currentEntry = entry.getName(); 
      File destFile = new File(Data.SAVE_DIR, currentEntry); 
      //destFile = new File(newPath, destFile.getName()); 
      File destinationParent = destFile.getParentFile(); 

      // create the parent directory structure if needed 
      destinationParent.mkdirs(); 

      if (!entry.isDirectory()) { 
       BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); 
       int currentByte; 
       // establish buffer for writing file 
       byte data[] = new byte[BUFFER]; 

       // write the current file to disk 
       FileOutputStream fos = new FileOutputStream(destFile); 
       BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); 

       // read and write until last byte is encountered 
       while ((currentByte = is.read(data, 0, BUFFER)) != -1) { 
        dest.write(data, 0, currentByte); 
       } 
       percentage = (int) (current/finalSize * 100); 
       System.out.println("finalSize " + finalSize); 
       System.out.println("Current " + current); 
       System.out.println("percentage " + percentage + "%"); 

       dest.flush(); 
       dest.close(); 
       is.close(); 
      } 
     } 
     zip.close(); 
     System.out.println("Done unzipping " + name.getFile()); 
    } 

所以我試圖做一些事情,但我不能得到我需要的2種不同的尺寸。 我認爲這是:未壓縮的zip文件的大小以及將要解壓縮的下一個文件的大小。 我試圖這樣做:

int finalSize = zip.size(); 

current += entry.getCompressedSize(); 
+0

沒有經過兩次這樣做的準確方法。壓縮文件甚至不能保證比原始文件小。只需拿出一個無限的請等待。 –

+0

好吧,所以通過它兩次是不是一個真正的選擇,我應該只給玩家留言:「解壓縮,請稍候。」? – xX4m4zingXx

+0

這是對的 –

回答

2

你可以嘗試使用Zip4j。它配備了進度監控器,過去對我來說效果很好。

下面是一個示例from their forums顯示如何使用它。該示例演示了壓縮,但進度監視器的工作方式與解壓縮一樣。

import java.io.File; 
import java.util.ArrayList; 

import net.lingala.zip4j.core.ZipFile; 
import net.lingala.zip4j.exception.ZipException; 
import net.lingala.zip4j.model.ZipParameters; 
import net.lingala.zip4j.progress.ProgressMonitor; 
import net.lingala.zip4j.util.Zip4jConstants; 

/** 
* This example demonstrates ProgressMonitor usage. Progress Monitor 
* can be used to get the current progress information of the task 
* being performed by Zip4j.<br><br> 
* 
* ProgressMonitor can be used to get information regarding any kind of task. 
* This example uses the adding files to Zip functionality. Information regarding 
* rest of the tasks is similar to this approach 
*/ 
public class ProgressInformation { 

    public ProgressInformation() { 

     try { 
      // Initiate the ZipFile 
      ZipFile zipFile = new ZipFile("c:\\ZipTest\\ProgressInformation.zip"); 

      // Set runInThread variable of ZipFile to true. 
      // When this variable is set, Zip4j will run any task in a new thread 
      // If this variable is not set, Zip4j will run all tasks in the current 
      // thread. 
      zipFile.setRunInThread(true); 

      // Initialize files to add 
      ArrayList filesToAdd = new ArrayList(); 
      filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); 
      filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); 
      filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); 

      // Initialize Zip Parameters 
      ZipParameters parameters = new ZipParameters(); 
      parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); 
      parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 

      // Add files to Zip 
      zipFile.addFiles(filesToAdd, parameters); 

      // Get progress monitor from ZipFile 
      ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); 

      // PLEASE NOTE: Below code does a lot of Sysout's. 

      // ProgressMonitor has two states, READY and BUSY. READY indicates that 
      // Zip4j can now accept any new tasks. BUSY indicates that Zip4j is 
      // currently performing some task and cannot accept any new task at the moment 
      // Any attempt to perform any other task will throw an Exception 
      while (progressMonitor.getState() == ProgressMonitor.STATE_BUSY) { 
       // ProgressMonitor has a lot of useful information like, the current 
       // operation being performed by Zip4j, current file being processed, 
       // percentage done, etc. Once an operation is completed, ProgressMonitor 
       // also contains the result of the operation. If any exception is thrown 
       // during an operation, this is also stored in this object and can be retrieved 
       // as shown below 

       // To get the percentage done 
       System.out.println("Percent Done: " + progressMonitor.getPercentDone()); 

       // To get the current file being processed 
       System.out.println("File: " + progressMonitor.getFileName()); 

       // To get current operation 
       // Possible values are: 
       // ProgressMonitor.OPERATION_NONE - no operation being performed 
       // ProgressMonitor.OPERATION_ADD - files are being added to the zip file 
       // ProgressMonitor.OPERATION_EXTRACT - files are being extracted from the zip file 
       // ProgressMonitor.OPERATION_REMOVE - files are being removed from zip file 
       // ProgressMonitor.OPERATION_CALC_CRC - CRC of the file is being calculated 
       // ProgressMonitor.OPERATION_MERGE - Split zip files are being merged 
       switch (progressMonitor.getCurrentOperation()) { 
       case ProgressMonitor.OPERATION_NONE: 
        System.out.println("no operation being performed"); 
        break; 
       case ProgressMonitor.OPERATION_ADD: 
        System.out.println("Add operation"); 
        break; 
       case ProgressMonitor.OPERATION_EXTRACT: 
        System.out.println("Extract operation"); 
        break; 
       case ProgressMonitor.OPERATION_REMOVE: 
        System.out.println("Remove operation"); 
        break; 
       case ProgressMonitor.OPERATION_CALC_CRC: 
        System.out.println("Calcualting CRC"); 
        break; 
       case ProgressMonitor.OPERATION_MERGE: 
        System.out.println("Merge operation"); 
        break; 
       default: 
        System.out.println("invalid operation"); 
        break; 
       } 
      } 

      // Once Zip4j is done with its task, it changes the ProgressMonitor 
      // state from BUSY to READY, so the above loop breaks. 
      // To get the result of the operation: 
      // Possible values: 
      // ProgressMonitor.RESULT_SUCCESS - Operation was successful 
      // ProgressMonitor.RESULT_WORKING - Zip4j is still working and is not 
      //         yet done with the current operation 
      // ProgressMonitor.RESULT_ERROR - An error occurred during processing 
      System.out.println("Result: " + progressMonitor.getResult()); 

      if (progressMonitor.getResult() == ProgressMonitor.RESULT_ERROR) { 
       // Any exception can be retrieved as below: 
       if (progressMonitor.getException() != null) { 
        progressMonitor.getException().printStackTrace(); 
       } else { 
        System.err.println("An error occurred without any exception"); 
       } 
      } 

     } catch (ZipException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     new ProgressInformation(); 
    } 

} 
+1

我從來沒有聽說過這個,我會看看它,謝謝你回答這個老問題! – xX4m4zingXx