title: Can we store data in gzip format in postgresql
body: <p>Following is java code which is trying to store data in gzip format in a table "TESTBYTEA". I am using postgresql database. TESTBYTEA table has one column "data" of type BYTEA. I want to compress the data and store it. While reading from the DB I want uncompress it and read it. But I am getting an exception "Not in GZIP format". </p>
  
  <pre><code>public static void main(String[] args){
      insertBytes(connection);        
      readBytes(connection);
      connection.close();
  }
  
  public static void insertBytes(Connection connection) throws FileNotFoundException, IOException, SQLException{
      File file = new File("C:test.txt");
      FileReader fileReader = new FileReader(file);
      char[] cbuf = new char[2000];
      int read = fileReader.read(cbuf);
      String str = new String (cbuf);
      byte[] bytes = gzip(str);
      Statement statement = connection.createStatement();
      int result = statement.executeUpdate("INSERT INTO TESTBYTEA (data) VALUES ('\\\\x"+bytes+"')");
      System.out.println(result);
  }
  
  
  public static void readBytes(Connection connection) throws SQLException, IOException{
      Statement statement = connection.createStatement();
      ResultSet rs = statement.executeQuery("select data from testbytea");
      while(rs.next()){
          byte[] bs = rs.getBytes(1);
          String str = gunzip(bs);
          System.out.println(str);
      }
  }
  
  
  private static String gunzip(byte[] bytes) throws IOException {
      Reader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(bytes)), "US-ASCII");
      StringBuffer sbuf = new StringBuffer();
  
      char[] buffer = new char[32 * 1024];
      int nread;
      while ((nread = reader.read(buffer)) &gt;= 0) {
          sbuf.append(buffer, 0, nread);
      }
  
      String s = sbuf.toString();
  
      reader.close();
      return s;
  }
  
  private static byte[] gzip(String s) throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
      GZIPOutputStream gzos = new GZIPOutputStream(baos);
      Writer writer = new OutputStreamWriter(gzos, "US-ASCII");
  
      writer.write(s);
      writer.flush();
      gzos.finish();
  
      byte[] bytes = baos.toByteArray();
  
      writer.close();
      return bytes;
  }
  </code></pre>
  
  <p>But I am getting the following exception</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Not in GZIP format
      at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141)
      at java.util.zip.GZIPInputStream.&lt;init&gt;(GZIPInputStream.java:56)
      at java.util.zip.GZIPInputStream.&lt;init&gt;(GZIPInputStream.java:65)
      at postgresjdbc.PostgresJDBC.gunzip(PostgresJDBC.java:237)
      at postgresjdbc.PostgresJDBC.readBytes(PostgresJDBC.java:230)
      at postgresjdbc.PostgresJDBC.main(PostgresJDBC.java:208)
  Java Result: 1
  </code></pre>
  
  <p>Any help regarding this is appreciated.</p>