0

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();
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Dyapa Srikanth
  • 1,231
  • 2
  • 22
  • 60

3 Answers3

1

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.

Ted Shaw
  • 2,298
  • 14
  • 8
0

You should Look at fin.Position

MichaelT
  • 7,574
  • 8
  • 34
  • 47
0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • i didn't find any content length in response headers. I want to display progress bar in android so that i need content length while downloading. Is it possible without content length. – Dyapa Srikanth Sep 13 '12 at 07:11