GZip download monitoring in Java

I upload some files to my java application and implement the download monitor dialog. But lately I have been compressing all files using gzip, and now the download monitor is a little broken.

I open the file as I GZIPInputStreamupdate the download status after each downloaded kB. If the file is 1 MB in size, progress is increased to, for example, 4 MB, which is an uncompressed size. I want to track the compressed download process. Is it possible?

EDIT: To clarify: I'm reading bytes from GZipInputStream, which are uncompressed bytes. So this does not give me the desired file size at the end.

Here is my code:

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();
}

Thanks for any help!

+5
source share
1 answer

GZIPInputStream InflaterInputStream. InflaterInputStream protected Inflater inf, Inflater . Inflater.getBytesRead .

, GZIPInputStream inf, , , Inflater, .

public final class ExposedGZIPInputStream extends GZIPInputStream {

  public ExposedGZIPInputStream(final InputStream stream) {
    super(stream);
  }

  public ExposedGZIPInputStream(final InputStream stream, final int n) {
    super(stream, n);
  }

  public Inflater inflater() {
    return super.inf;
  }
}
...
final ExposedGZIPInputStream gzip = new ExposedGZIPInputStream(...);
...
final Inflater inflater = gzip.inflater();
final long read = inflater.getBytesRead();
+4

All Articles