[英]Get actual size of a gzip file in android
我正在使用GZIPInputStream下載pdf文件,我想在UI按鈕上顯示文件的下載進度。 但是,我沒有得到文件的實際大小,我得到的是壓縮大小,因此無法顯示正確的下載進度。 由於實際文件大小大於文件的壓縮大小,因此下載進度超過100。 來自服務器的文件的標題內容:-從服務器接收到以下信息,我正在使用它從content-length獲得壓縮的文件大小。
1.連接2.內容編碼3.內容長度4.內容類型5.保持活動6.服務器7.日期
這是我的代碼。
long fileLength = httpResponse.getEntity().getContentLength();//
GZIPInputStream input = new GZIPInputStream(new BufferedInputStream(httpResponse.getEntity().getContent()));
FileOutputStream output = new FileOutputStream(destinationFilePath);
byte data[] = new byte[1024];
long total = 0;
float percentage = 0;
int count;
currentDownloadingPercentage=0;
while ((count = input.read(data)) != -1)
{
total += count;
output.write(data, 0, count);
// publishing the progress....
percentage = (float)total/(float)fileLength;
percentage *= 100;
if((int)percentage > (int)currentDownloadingPercentage)
{
currentDownloadingPercentage = percentage;
Bundle resultData = new Bundle();
resultData.putBoolean(DOWNLOAD_FAILED, false);
resultData.putInt(DOWNLOAD_PROGRESS ,(int)percentage);
receiver.send(processID, resultData);
resultData = null;
}
}
您需要更改讀取流的方式:先閱讀,然后再充氣(解壓縮)。 這是因為直接從GZIPInputStream讀給你的未壓縮數據的大小,而你正在尋找的壓縮數據的大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.