GZIPInputStream を使用して PDF ファイルをダウンロードしています。ファイルのダウンロードの進行状況を UI ボタンに表示したいと考えています。しかし、ファイルの実際のサイズを取得していません。取得しているのは圧縮されたサイズであるため、正しいダウンロードの進行状況を表示できません。実際のファイル サイズがファイルの圧縮サイズよりも大きいため、このダウンロードの進行状況は 100 を超えています。サーバーからのファイルのヘッダー コンテンツ: - サーバーから受け取った次の情報。圧縮されたファイル サイズを指定する content-length を使用しています。
1.接続 2.Content-Encoding 3.Content-length 4.Content-Type 5.Keep-Alive 6.Server 7.Date
これが私のコードです。
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;
}
}