SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

First timer: MemoryStream & ZipOutputStream/ZipInputStream

Last post 08-08-2008 5:07 PM by downatone. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 07-28-2008 11:16 PM

    First timer: MemoryStream & ZipOutputStream/ZipInputStream

    Been playing with this most of today ... how sad - and still no sucess after going through many posts.
    I have 2 main functions 1 to compress a byte [ and return it, and vice-versa to decompress a byte [ and return it.

    I think I've setup the compression correctly - but can't test fully without getting the decompression working. Here are the 2 functions:
    Please take a peak and let me know what I've missed, thanks in advance ...

            public byte[ Compress(byte[ data)
            {
                byte[ output = null;
                MemoryStream str = new MemoryStream();
                Crc32 crc = new Crc32();

                if (data != null)
                {
                    try
                    {

                        using (ZipOutputStream zipstr = new ZipOutputStream(str))
                        {
                           
                            string s = Encoding.ASCII.GetString(data);
                           
                            //ZipEntry entry = new ZipEntry(s);
                            ZipEntry entry = new ZipEntry("DC_Test");
                            entry.DateTime = DateTime.Now;
                            entry.Size = s.Length;
                            crc.Reset();
                            crc.Update(data);
                            entry.Crc = crc.Value;

                            zipstr.PutNextEntry(entry);
                            zipstr.Write(data, 0, data.Length);

                            s = Encoding.ASCII.GetString(str.ToArray());
                            output = str.ToArray();

                            zipstr.Finish();
                            zipstr.Close();

                        }

                    }
                    catch
                    {

                        output = null;
                    }
                }

                return output;
            }

            public byte[ Decompress(byte[ data)
            {
                byte[ output = null;
                //string st = new ASCIIEncoding().GetString(data);
                //output = Encoding.ASCII.GetBytes(st);

                ZipEntry entry;

                MemoryStream str = new MemoryStream(data);
                str.Seek(0, SeekOrigin.Begin);

                if (data != null)
                {
                    try
                    {

                        using (ZipInputStream s = new ZipInputStream(str))
                        {
                            entry = s.GetNextEntry();
                            long size = entry.Size;
                            byte[ buffer = new byte[size];

                            size = s.Read(buffer, 0, size);

                            output = buffer;
                        }

                    }
                    catch
                    {
                        output = null;
                    }
                }
               

                return output;
            }

  • 08-08-2008 10:11 AM In reply to

    Re: First timer: MemoryStream & ZipOutputStream/ZipInputStream

     Hi,

    You grab the output of the zipstream before its finished.  Grab the output after its all sone and it may well work....


                            s = Encoding.ASCII.GetString(str.ToArray());
                            output = str.ToArray();

                            zipstr.Finish();
                            zipstr.Close();
     

     When readong you assume the read will grab all the data in onw hit which is not true.  Keep reading until read returns 0 indicating its really done....

     

      entry = s.GetNextEntry();
                            long size = entry.Size;
                            byte[ buffer = new byte[size];

                            size = s.Read(buffer, 0, size);
     

     

    hth, -jr-

  • 08-08-2008 5:07 PM In reply to

    Re: First timer: MemoryStream & ZipOutputStream/ZipInputStream

    That did the trick, thanks jr

Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.