I'm java beginner, I need something like this:
String2GzipFile (String file_content, String file_name)
String2GzipFile("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "lorem.txt.gz")
I cant figure out how to do that.
I'm java beginner, I need something like this:
String2GzipFile (String file_content, String file_name)
String2GzipFile("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "lorem.txt.gz")
I cant figure out how to do that.
Same solution as Jon, just used try with resources
Have a look at GZIPOutputStream - you should be able to use this in exactly the same way as any other outputstream to write to a file - it'll just automatically write it in the gzip compressed format.
There are two orthogonal concepts here:
OutputStreamWriter
GZIPOutputStream
So in the end you'll want to:
OutputStream
which writes to wherever you want the result (e.g. a file or in memory via aByteArrayOutputStream
OutputStream
in aGZIPOutputStream
GZIPOutputStream
in anOutputStreamWriter
using an appropriate charset (e.g. UTF-8)OutputStreamWriter
For example:
Note that I'm closing
output
even if we fail to create the writer, but we still need to closewriter
if everything is successful, in order to flush everything and finish writing the data.