lanczlot
lanczlot

Reputation: 41

compressing with GZIPOutputStream to upload without creating a gzip file locally

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!

Upvotes: 4

Views: 1355

Answers (3)

user207421
user207421

Reputation: 310840

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

Upvotes: 2

lanczlot
lanczlot

Reputation: 41

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!

Upvotes: 0

ok2c
ok2c

Reputation: 27518

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();
}

Upvotes: 0

Related Questions