0

I have this code that compresses a string and write to a streamwritter

FileOutputStream output = new FileOutputStream(outPutFilePath.toFile());
try {
    Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8");
    try {
        writer.write(String.valueOf(stringByteArray));
    } finally {
        writer.close();
    }
} finally {
    output.close();
}

How can I convert back my string?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Navigator
  • 2,871
  • 4
  • 17
  • 27
  • 1
    Does this answer your question? [How to read from GZIPInputstream](https://stackoverflow.com/questions/35789253/how-to-read-from-gzipinputstream) – Federico klez Culloca Sep 14 '22 at 06:57
  • 2
    If “`stringByteArray`” truly is a byte array, using `String.valueOf(stringByteArray)` will not do what you might expect. If this byte array is already UTF-8 encoded, you should simply write it directly to the `GZIPOutputStream`. Otherwise, if it has a different encoding, you must use an appropriate method to decode it specifying the exact encoding. Besides that, you should use [try-with-resources statements](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) instead of `try finally`. – Holger Sep 14 '22 at 08:42

0 Answers0