Dyapa Srikanth
Dyapa Srikanth

Reputation: 1241

Calculating Compressed Filesize from GZIPOutputStream

I am compressing File and writing it into outputstream, How to get the compressed file size to put in response header

    os = response.getOutputStream();
    gzos = new GZIPOutputStream(os);
    fin = new FileInputStream(file);
    in = new BufferedInputStream(fin);

    byte[] buffer = new byte[1024];
    int i;
    while ((i = in.read(buffer)) >= 0) {
           gzos.write(buffer, 0, i);
     }
    gzos.flush();

Upvotes: 0

Views: 548

Answers (3)

user207421
user207421

Reputation: 310874

You don't. Reason: you don't have to. The servlet container takes care of it for you.

Upvotes: 0

Ted Shaw
Ted Shaw

Reputation: 2306

you don't. Reason:

response will be committed once response buffer is full, that is to say, if you are gzipping a 20MB large file, the header was already sent to client before gzip completes, and you can not modify the committed header when gzip finishes.

Upvotes: 1

MichaelT
MichaelT

Reputation: 7934

You should Look at fin.Position

Upvotes: 0

Related Questions