0

For some reason I cannot find any examples for doing so with DotNetZip.

I receive a Gziped content from my WebAPI.

All I want to do is to read that memory stream from the response and unzip the content(text) into a string.

My compression working fine, I just can't figure out the decompress:

public static byte[] CompressData(byte[] str)
{
    using (var output = new MemoryStream())
    {
        using (
            var compressor = new Ionic.Zlib.DeflateStream(
            output, Ionic.Zlib.CompressionMode.Compress,
            Ionic.Zlib.CompressionLevel.BestSpeed))
        {
            compressor.Write(str, 0, str.Length);
        }

        return output.ToArray();
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
omriman12
  • 1,644
  • 7
  • 25
  • 48

2 Answers2

-1

The following code shows how the program extracts the files from the archive.

private void ExtractArchive(object sender, EventArgs e)
{
    try
    {
        using (ZipFile zip = ZipFile.Read(txtArchiveName.zip))
        {
            // Loop through the archive's files.
            foreach (ZipEntry zip_entry in zip)
            {
                zip_entry.Extract(txtExtractTo);
            }
        }

        MessageBox.Show("Done");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error extracting archive.\n" +
            ex.Message);
    }
}

The simplest way to Extract all the entries from a zipfile looks like this:

using (ZipFile zip = ZipFile.Read(NameOfExistingZipFile))
  {
    zip.ExtractAll(args[1]);
  }
Kasun
  • 7,161
  • 2
  • 15
  • 9
-1

Ok, I found the solution. Pretty simple actually, but very poorly documented online.

 public static byte[] Decompress(Stream ms)
{
    byte[] outData = null;
    using (MemoryStream msInner = new MemoryStream())
    {
        using (DeflateStream defStream = new DeflateStream(ms, CompressionMode.Decompress))
        {
            defStream.CopyTo(msInner);
            outData = msInner.ToArray();
        }
    }

    return outData;
}
omriman12
  • 1,644
  • 7
  • 25
  • 48