SharpDevelop Community

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

Compress MemoryStream and what is best method

Last post 03-05-2007 4:19 PM by ariellevy. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 02-09-2007 4:10 PM

    Compress MemoryStream and what is best method

    I have just started using SharpZipLib after using a compression tool from exceed. I really like what options I have on this but I cannot seem to find a sample on compressing a standard MemoryStream. Can someone point me in the right direction.

     I was thinking of using Zip.Compression.Streams in order to accompolish this, would this be the right option. The MemoryStream is actually an arraylist filled with documents converted to byte[]'s.

     Any help would be appreciated.

     

    UnSkiLd

  • 02-12-2007 8:28 PM In reply to

    Re: Compress MemoryStream and what is best method

    I must add that the above was for Compression but also I cannot find a sample that fits my situation and needs to a decompress stream as well.

    the output of the decompression should be an Arraylist.

     So far on my Compression this is what I had done:

    //save to memoryStream
    MemoryStream msStream = new MemoryStream(arrbytes); //arrbytes is the Arraylist converted to a byte[]

    //try compressing arraylist
    ICS.ZipOutputStream zipOutput = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(msStream);

    zipOutput.SetLevel(8);

    ICS.ZipEntry entry = new ICS.ZipEntry(xmlPath);

    zipOutput.PutNextEntry(entry);
    zipOutput.Write(buffer, 0, buffer.Length);
    zipOutput.Finish();
    zipOutput.Close();

    zipOutput = null;

    buffer = msStream.ToArray();

     Still cant do the Decompression on this for some reason. Any help would be appreciated.

  • 03-05-2007 4:19 PM In reply to

    Re: Compress MemoryStream and what is best method

    /// <summary>

    /// Decompresion de zip file to file (create directory structure if necesary)

    /// </summary>

    /// <param name="zipFileName"></param>

    /// <param name="outputFilePath"></param>

    public static void Decompress(string zipFileName, string outputFilePath)

    {

        FileStream input =null;

        ZipInputStream zip = null;

     

        try

        {

            //read file to stream

            using (input = File.OpenRead(zipFileName))

            {

                //obtengo todas las entradas del zip

                using ( zip = new ZipInputStream(input))

                {

                    ZipEntry entry;

                    //recorro todos los archivos

                    while ((entry = zip.GetNextEntry()) != null)

                    {

                        string entryName = entry.Name;

                        string outFileName = outputFilePath + "\\" + entryName;

                        if(entryName.IndexOf('.')>0)

                        //is file

                        {

                            FileStream output = null;

                            try

                            {

                                //compress stream to file

                                using (output = File.Create(outFileName))

                                {

                                    //copy input to ouput

                                    //streams throw ZipInputStream

                                    byte[] buffer = new byte[BUFFER_LENGHT]; //128 kb

                                    while(true)

                                    {

                                        System.Threading.Thread.Sleep(SLEEP_UNCOMPRESS);

                                        int count = zip.Read(buffer, 0, BUFFER_LENGHT); //128 kb

                                        if(count==0) break;

                                        output.Write(buffer, 0, count);

                                    }                               

                                }

                            }

                            finally

                            {

                                if(output!=null)

                                    output.Close();

                            }

                        }

                        else

                        //is directory

                        {

                            Directory.CreateDirectory(outFileName);

                        }

                    }

                }

            }

        }

        finally

        {

            if(input!=null)

                input.Close();

     

            if(zip!=null)

                zip.Close();           

        }

    }

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.