Search code examples
c#pythonzipgzipzlib

How to compress data in C# to be decompressed in zlib python


I have a python zlib decompressor that takes default parameters as follows, where data is string:

  import zlib
  data_decompressed = zlib.decompress(data)

But, I don't know how I can compress a string in c# to be decompressed in python. I've tray the next piece of code but when I trie to decompresse 'incorrect header check' exception is trown.

    static byte[] ZipContent(string entryName)
    {
        // remove whitespace from xml and convert to byte array
        byte[] normalBytes;
        using (StringWriter writer = new StringWriter())
        {
            //xml.Save(writer, SaveOptions.DisableFormatting);
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            normalBytes = encoding.GetBytes(writer.ToString());
        }

        // zip into new, zipped, byte array
        using (Stream memOutput = new MemoryStream())
        using (ZipOutputStream zipOutput = new ZipOutputStream(memOutput))
        {
            zipOutput.SetLevel(6);

            ZipEntry entry = new ZipEntry(entryName);
            entry.CompressionMethod = CompressionMethod.Deflated;
            entry.DateTime = DateTime.Now;
            zipOutput.PutNextEntry(entry);

            zipOutput.Write(normalBytes, 0, normalBytes.Length);
            zipOutput.Finish();

            byte[] newBytes = new byte[memOutput.Length];
            memOutput.Seek(0, SeekOrigin.Begin);
            memOutput.Read(newBytes, 0, newBytes.Length);

            zipOutput.Close();

            return newBytes;
        }
    }

Anyone could help me please? Thank you.

UPDATE 1:

I've tried with defalte function as Shiraz Bhaiji has posted:

    public static byte[] Deflate(byte[] data)
    {
        if (null == data || data.Length < 1) return null;
        byte[] compressedBytes;

        //write into a new memory stream wrapped by a deflate stream
        using (MemoryStream ms = new MemoryStream())
        {
            using (DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, true))
            {
                //write byte buffer into memorystream
                deflateStream.Write(data, 0, data.Length);
                deflateStream.Close();

                //rewind memory stream and write to base 64 string
                compressedBytes = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(compressedBytes, 0, (int)ms.Length);

            }
        }
        return compressedBytes;
    }

The problem is that to work properly in python code I've to add the "-zlib.MAX_WBITS" argument to decompress as follows:

    data_decompressed = zlib.decompress(data, -zlib.MAX_WBITS)

So, my new question is: is it possible to code a deflate method in C# which compression result could be decompressed with zlib.decompress(data) as defaults?


Solution

  • In C# the DeflateStream class supports zlib. See:

    https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.deflatestream?view=netframework-4.8