Java アプリでいくつかのファイルをダウンロードし、ダウンロード モニター ダイアログを実装しました。しかし最近、すべてのファイルを gzip で圧縮したところ、ダウンロード モニターが壊れてしまいました。
ファイルを として開き、GZIPInputStream
kB がダウンロードされるたびにダウンロード ステータスを更新します。ファイルのサイズが 1MB の場合、進行状況は圧縮されていないサイズの 4MB まで進みます。圧縮されたダウンロードの進行状況を監視したい。これは可能ですか?
編集:明確にするために:圧縮されていないバイトである GZipInputStream からバイトを読み取っています。そのため、最終的に適切なファイルサイズが得られません。
これが私のコードです:
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
...
File file = new File("bibles/" + name + ".xml");
if(!file.exists())
file.createNewFile();
out = new FileOutputStream(file);
in = new BufferedInputStream(new GZIPInputStream(con.getInputStream()));
byte[] buffer = new byte[1024];
int count;
while((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
downloaded += count;
this.stateChanged();
}
...
private void stateChanged() {
this.setChanged();
this.notifyObservers();
}
助けてくれてありがとう!