title: Are zlib.compress on Python and Deflater.deflate on Java (Android) compatible?
body: <p>I am porting a Python application to Android and, at some point, this application has to communicate with a Web Service, sending it compressed data.</p>
  
  <p>In order to do that it uses the next method:</p>
  
  <pre><code>def stuff(self, data):
      "Convert into UTF-8 and compress."
      return zlib.compress(simplejson.dumps(data))
  </code></pre>
  
  <p>I am using the next method to try to emulate this behavior in Android:</p>
  
  <pre><code>private String compressString(String stringToCompress)
  {
      Log.i(TAG, "Compressing String " + stringToCompress);
      byte[] input = stringToCompress.getBytes(); 
      // Create the compressor with highest level of compression 
      Deflater compressor = new Deflater(); 
      //compressor.setLevel(Deflater.BEST_COMPRESSION); 
      // Give the compressor the data to compress 
      compressor.setInput(input); 
      compressor.finish(); 
      // Create an expandable byte array to hold the compressed data. 
      // You cannot use an array that's the same size as the orginal because 
      // there is no guarantee that the compressed data will be smaller than 
      // the uncompressed data. 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); 
      // Compress the data 
      byte[] buf = new byte[1024]; 
      while (!compressor.finished()) 
      { 
          int count = compressor.deflate(buf); 
          bos.write(buf, 0, count); 
      } 
  
      try { 
          bos.close(); 
      } catch (IOException e) 
      { 
  
      } 
      // Get the compressed data 
      byte[] compressedData = bos.toByteArray(); 
  
      Log.i(TAG, "Finished to compress string " + stringToCompress);
  
      return new String(compressedData);
  }
  </code></pre>
  
  <p>But the HTTP response from the server is not correct and I guess it is because the result of the compression in Java is not the same as the one in Python.</p>
  
  <p>I ran a little test compressing "a" both with zlib.compress and deflate.</p>
  
  <p>Python, zlib.compress() -> x%9CSJT%02%00%01M%00%A6</p>
  
  <p>Android, Deflater.deflate -> H%EF%BF%BDK%04%00%00b%00b</p>
  
  <p>How should I compress the data in Android to obtain the same value of zlib.compress() in Python?</p>
  
  <p>Any help, guidance or pointer is greatly appreciated!</p>