title: Compressing\decompressing binary files
body: <p>I need to decompress the *.bin file into a certain temp-file, perform certain actions with it in the my program and then compress it. There is a function that unpacks the *.bin file. I.e., temp-file for the correct operation of the program was obtained. Function:</p>
  
  <pre><code>public void Unzlib_bin(string initial_location, string target_location)
  {
      byte[] data, new_data;
      Inflater inflater;
  
      inflater = new Inflater(false);
  
      data = File.ReadAllBytes(initial_location);
      inflater.SetInput(data, 16, BitConverter.ToInt32(data, 8));
      new_data = new byte[BitConverter.ToInt32(data, 12)];
      inflater.Inflate(new_data);
  
      File.WriteAllBytes(target_location, new_data);
  }
  </code></pre>
  
  <p>The question is how to pack the temp file to its original state? I'm trying to do something like the following, but the result is wrong:</p>
  
  <pre><code>public void Zlib_bin(byte[] data, int length, string target_location)
  {
      byte[] new_data;
      Deflater deflater;
      List&lt;byte&gt; compress_data;
  
      compress_data = new List&lt;byte&gt;();
      new_data = new byte[length];
      deflater = new Deflater();
  
      compress_data.AddRange(new byte[] { ... }); //8 bytes from header, it does not matter
      deflater.SetLevel(Deflater.BEST_COMPRESSION);
      deflater.SetInput(data, 0, data.Length);
      deflater.Finish();
      deflater.Deflate(new_data);
      compress_data.AddRange(BitConverter.GetBytes(new_data.Length));
      compress_data.AddRange(BitConverter.GetBytes(data.Length));
      compress_data.AddRange(new_data);
  
      File.WriteAllBytes(target_location, compress_data.ToArray());
  }
  </code></pre>
  
  <p>Any ideas?</p>