0

I have a C# function that receives a compressed ByteArray as a parameter. I need to EXTRACT this byteArray and send the resulting uncompressed byteArray to another function. I need help extracting zipBytes to unzippedBytes please see below PSEUDO CODE:

SOLUTION using Zlib.net!

byte[] receiveZipByte (byte[] zipBytes)

{

    MemoryStream oInStream = new MemoryStream(pZFileData);
    ZInputStream oZInstream = new ZInputStream(oInStream);
    MemoryStream oOutStream = new MemoryStream();

    byte[] buffer = new byte[2000];
    int len;
    while ((len = oZInstream.read(buffer, 0, 2000)) > 0)
    {
        oOutStream.Write(buffer, 0, len);
    }

    byte[] pFileData = oOutStream.ToArray();
    oZInstream.Close();
    oOutStream.Close();
   return unzippedBytes;
}

1 Answers1

0

If what you're trying to do is decompress data that has been compressed using zlib, I would suggest using the ZLib.NET library: http://www.componentace.com/zlib_.NET.htm

It would depend on what you want to do with the data; but, here's an example of using a Stream with DotNetZip:

using (var input = new ZipInputStream(new MemoryStream(zipBytes)))
{
    ZipEntry e;
    while ((e = input.GetNextEntry()) != null)
    {
        if (e.IsDirectory) continue;
        using (var output = File.Open(e.FileName, FileMode.Create, FileAccess.ReadWrite))
        {
            while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, n);
            }
        }
    }
}

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • *****"It would depend on what you want to do with the data"***** I'm actually trying to pass the data to another byte Array variable in memory (no intention to save the data on a file. – user2690511 Aug 19 '13 at 15:16
  • DotNetZip provides zip-file functionality. This means it managed multiple compresses files in one archive--which generally means writing to disk. If you just want compressed data, I wouldn't recommend DotNetZip (overhead could easily be more than what the compression would offer). If you provide more detail about what you're hoping to accomplish, someone might be able to offer some suggestions. – Peter Ritchie Aug 19 '13 at 16:26
  • Ok this is the scenario. FLEX 3 application trough AS3 class file sends a variable type byteArray compressed (trough compress() )method) to .NET - a C# webservice method type [WebMethod] RECEIVES the byteArray variable; I should unzip or decompress the variable here in memory in order for me to send it UNZIPPED to another class inside my .NET logic. – user2690511 Aug 19 '13 at 16:47
  • or.....I have a C# function that receives a compressed ByteArray as a parameter. I need to EXTRACT this byteArray in Memory and send the resulting uncompressed byteArray to another function. – user2690511 Aug 19 '13 at 17:03
  • FLEX 3 compress() takes a parameter for the compression algorithm, doesn't it? Without knowing that, it would be hard to detail code that would reliably decompress it. – Peter Ritchie Aug 19 '13 at 17:18
  • Actually AIR SDK uses compression parameters..but I'm not using AIR and NON AIR sdk will not take any parameter..not sure exactly what compression algorithm use by default. – user2690511 Aug 19 '13 at 17:27
  • Ok...seems that the default algorithm I'm using is zlib. – user2690511 Aug 19 '13 at 17:32
  • Peter, using Zlib.net as you suggested worked just fine, thank you for your advice! I'm posting the code above..just in case helps some other folks. Thanks! – user2690511 Aug 19 '13 at 19:08