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();
沒有經過兩次這樣做的準確方法。壓縮文件甚至不能保證比原始文件小。只需拿出一個無限的請等待。 –
好吧,所以通過它兩次是不是一個真正的選擇,我應該只給玩家留言:「解壓縮,請稍候。」? – xX4m4zingXx
這是對的 –