1

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

1 に答える 1

0

ストリームの読み取り方法を変更する必要があります。最初に読み取り、後で展開 (解凍) します。これは、圧縮されたデータのサイズを探しているときに、GZIPInputStream から直接読み取ると圧縮されていないデータのサイズが得られるためです。

于 2013-03-05T08:40:52.323 に答える