4

I'm trying to compress the file in java before I upload the file with GZIPOutputStream. Is there a way to just store the gzipped file in memory for the upload and not have it generate a gzipped file on the local computer?

Thanks!

lanczlot
  • 41
  • 2

3 Answers3

2

Just connect the GZipOutputStream directly to the output stream and write. No file necessary.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • GzipCompressingEntity is the recommended way of doing that, as it also adjusts content-length / content-type / content-encoding request headers and ensures correct message delineation. – ok2c Sep 23 '14 at 09:23
  • I was trying to look up how to use GzipCompressingEntity wasn't able to find much. Would I simply just create a entity of class GzipCompressingEntity and set that as the entity in my put request? When I do that i get a SocketTimepoutException. – lanczlot Sep 23 '14 at 22:23
  • Yes, you just would use GzipCompressingEntity to decorate another entity. SocketTimepoutException has nothing to do with GZIP encoding. – ok2c Sep 24 '14 at 09:24
0

Decorate content entity with GzipCompressingEntity

HttpPost httpPost = new HttpPost("https://host/stuff");
httpPost.setEntity(new GzipCompressingEntity(new FileEntity(new File("my.stuff"))));
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
    EntityUtils.consume(response.getEntity());
} finally {
    response.close();
}
ok2c
  • 26,450
  • 5
  • 63
  • 71
0

Solved this problem by writing to a temp file and doing a .deleteonexit on the temp file once the gzip uploading was done.

Thanks for the help!

lanczlot
  • 41
  • 2
  • using some stream will be the best..what if this code runs on server and never exit? or some error causing your JVM to crash? – Shawn Sep 22 '14 at 21:48
  • you mean like streaming it directly to server? how does streaming handle JVM crashes? Im currently streaming to temp file using the `GZIPOutputStream` , then uploading it to the server. – lanczlot Sep 22 '14 at 22:02