title: Java GZIPOutputStream: Corrupted gzip with this method
body: <p>does someone have an idea why this code to create a gzipped string is not working? CLI gzip on a Mac can't open the resulting file: "Not in gz format".</p>
  
  <p>Please note: I need the string, not the file. Creating the gzipped file directly works, so does writing the JSON without zipping it. 
  The file writing in this example is just for testing purposes.</p>
  
  <pre><code>public someMethod {
              String gzippedString = this.gzippedString(finalJSONObject.toJSONString());
              OutputStream outputStream = new FileOutputStream(new File(this.jsonOutputPath + "/myfile.gz"));
              BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
              writer.append(gzippedString);
              writer.close();
          }
  
  private String gzippedString(String inputString) throws IOException {
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
          gzipOutputStream.write(inputString.getBytes());
          gzipOutputStream.close();
          outputStream.close();
          String gzippedString = outputStream.toString();
          return gzippedString;
      }
  </code></pre>
  
  <p>EDIT:
  chrylis showed me the way:</p>
  
  <pre><code>public void someMethod() {
          byte[] byteArray = this.gzippedByteArray(finalJSONObject.toJSONString());
          FileOutputStream out = new FileOutputStream(this.jsonOutputPath + "/myfile.gz");
          out.write(byteArray);
          out.close();
  }
  
  
  private byte[] gzippedByteArray(String inputString) throws IOException {
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
          gzipOutputStream.write(inputString.getBytes());
          gzipOutputStream.close();
          outputStream.close();
          byte[] gzippedByteArray = outputStream.toByteArray();
          return gzippedByteArray;
  }
  </code></pre>
  
  <p>This results in a working gzipped JSON.
  Thanks a lot!</p>