title: Util.zip. Corrupted data deflating in Java inflating in Android
body: <p>I have an issue with java.util.zip library, my requirement is to compress some data on a server, decompress and read them on an Android device. I got some corrupted data, so I tried a simplified test from a PC and I could see in the debugger where it failed, but I can't understand why. According to the documentation the Java VM and Android use the same compression algorithm, but the bytes as they were written by the Java deflater were not restored properly by the inflater in Android.
  The Java VM is:</p>
  <pre><code>~&gt; java -version
  openjdk version &quot;10.0.2&quot; 2018-07-17
  OpenJDK Runtime Environment (build 10.0.2+13-suse-lp150.7.1-x8664)
  OpenJDK 64-Bit Server VM (build 10.0.2+13-suse-lp150.7.1-x8664, mixed mode)
  </code></pre>
  <p>The Android environment the emulator: Nexus 5X API 29 x86 (cpu x86)]. In the comments in the code I'll add some details.
  Deflate on the JVM:</p>
  <pre><code>byte[] pArray = //Input data
  //Before compression, size = 20 bytes
  //Data: [-67, 77, 12, 20, -65, 85, -106, 39, -64, 34, 88, -68, 63, -56, -114, -39, -65, 76, 28, -81]
  try {
      Deflater def = new Deflater();
      def.setInput(pArray);
      def.finish();
  
      int compressedDataLength = def.deflate(pArray);
      def.end();
      //After compression data size is still 20 bytes 
      //Data: [120, -100, -85, -104, -61, 35, -78, 63, 116, -102, -6, 1, -91, -120, 61, -10, 39, -6, 110, -18]
  
      FileOutputStream fout = new FileOutputStream(filePath);
      fout.write(pArray, 0, compressedDataLength);
  
      fout.flush();
      fout.close();           
  } catch (IOException e) {
      log.error(&quot;IO Exception writing compressed data. {}&quot;, e);           
  }
  </code></pre>
  <p>Inflate on Android:</p>
  <pre><code>private byte[] expandCompressedData(byte[] cData, int expectedSize) {
      //Data coming from the files, 20 bytes equal to those written on the PC
      //Data: [120, -100, -85, -104, -61, 35, -78, 63, 116, -102, -6, 1, -91, -120, 61, -10, 39, -6, 110, -18]
      //expectedSize = 20
      byte expData[] = null;
      try {
          Inflater decompresser = new Inflater();
          decompresser.setInput(cData, 0, cData.length);
  
          expData = new byte[expectedSize];
          int resultLength = decompresser.inflate(expData);
          //First issue here resultLength = 16
          if (resultLength &lt;= 0) {
              throw new DataExtractionException(&quot;Something went wrong extracting file data. Extracted data size: {}&quot; + resultLength);
          }
          decompresser.end();
      } catch (java.util.zip.DataFormatException ex) {
          throw new DataExtractionException(&quot;DataFormatException extracting file data: {}&quot; + ex);
      }
      //The data returned is the same as the input, but truncated and padded by zeroes
      //Data: [120, -100, -85, -104, -61, 35, -78, 63, 116, -102, -6, 1, -91, -120, 61, -10, 0, 0, 0, 0 ]
      return expData;
  }
  </code></pre>
  <p>Does anybody have any idea about why the deflater doesn't like and doesn't extract those data?</p>
  <h1>_______________________________</h1>
  <p>Update:</p>
  <p>After further testing I realized that the issue doesn't have anything to do with JVM -&gt; Android.
  I don't know why but that particular sequence of bytes can't be compressed and recovered. For the sake of clarity I can add that the bytes come from an array of floats, but that's irrelevant, the error happens regardless of the conversions.
  Can it be a bug in the BZIP implementation? In my case I worked around the problem using the gzip format.</p>