Reputation: 616
On the network where I have my web server there is a machine that has many zipped pdf files (zipped using java.util.zip) and I can access these files through HTTP. When a user wants to download a pdf file, I know how to unzip the file locally on the server first and then deliver the unzipped pdf to the user through a servlet. Is it possible to deliver the unzipped file to the user without unzipping it locally first?
Regards
Upvotes: 2
Views: 719
Reputation: 1109532
Is it possible to deliver the unzipped file to the user without unzipping it locally first?
That depends a little bit on what exactly you mean with "locally", the general answer is "no". To deliver unzipped content, you have to unzip the zip first.
If you actually mean that the zip file is located at some non-local machine and that you currently need to save and zip it locally first before streaming unzipped content, then the answer would be "yes", it is possible to unzip and stream it without saving the file locally. Just pass/decorate the streams without using FileInputStream
/FileOutputStream
.
Upvotes: 1
Reputation: 74800
In principle, if the client has said in his request that he accepts gzip-compressed data, you could send the PDF file in compressed form, and the client will decompress it. There is a gotcha, though: While the compression algorithm of zip files and the HTTP Content-Encoding: gzip
is the same, the Zip file format has some more things around it (since it can contain multiple files, and a directory structure), so it would be necessary to strip these things off before. I'm not sure this would be much easier than decompressing in your servlet and then let your Servlet-engine take care of compressing again, but try it.
Upvotes: 1
Reputation: 421230
You can send the response to a request, encoded in a compressed format. If the client does the request with the header
Accept-Encoding: gzip, deflate
you can for instance serve him the content compressed using gzip (as long as you declare this through a header:)
Content-Encoding: gzip
Source: Wikipedia: HTTP Compression
Upvotes: 1