I'm trying to decompress buffer compressed by php deflate implementation. Here's the code:
public static void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt, i = 0;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0 )
{
dest.Write(bytes, 0, cnt);
}
dest.Flush();
}
public static byte[] Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new DeflateStream(msi, CompressionMode.Decompress))
{
msi.ReadByte();
msi.ReadByte();
CopyTo(gs, mso);
}
return mso.ToArray();
}
}
As you notice, I'm reading first 2 bytes from source stream, otherwise DeflateStream throws exception saying invalid block size. However, my problem is that, for some files, this code works like a charm, but for others, it gives corrupted result (a file with only some part of the file. Gives impression that it didn't decompress whole file). Anyone has any idea what's wrong?
Thanks
UPDATE
I found out PHP function used to compress the data. It's gzcompress.