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!
source
share