Unzipping to memorystream

May 8, 2009 at 6:46 AM

I have seen the examples on how to compress memorysteam (string variable) items into a zipfile and was able to successfully do this.

I now want to decompress them back into the memorystream (string variables) from the zip file. Is this possible? Can someone show me an example of how to do this please. I couldn't find any information regarding this.

Thanks

May 8, 2009 at 1:44 PM

I was able to decompress the files into memory like so:

1      MemoryStream ms = new MemoryStream();
2 using (ZipFile zip = ZipFile.Read(ZipFileToRead))
3 {
4 zip["DataFile.csv"].Extract(ms);
5 }
6

but I want to tweak my question a little bit.

Say I have a folder in the zip file containing many different files (unknown number), how can I decompress the file into a List<> of say string with each index having each different zip file?
Coordinator
May 8, 2009 at 1:45 PM

I think you are mixing things.

There are a couple of related methods.  One is ZipFile.AddFileFromString() and the other, AddFileFromStream().  They are two distinct methods.  If you want to create an entry in a zip file, using the contents of a string, then use the former.  If you want to use the contents of a stream (including a MemoryStream), use the latter.   In each case the result is a zipfile with an entry.

I think you are asking about extracting an entry to a stream, or perhaps to a string.  In the extraction case, there is no ExtractToString() method, so you must extract to a stream, using the ZipEntry.Extract(Stream) method, and a MemoryStream.  Example: 

            using (ZipFile zip = ZipFile.Read(_zipFileName))
            {
                var r = zip["files/Readme.txt"];
                if (r!= null)
                {
                    using (var ms = new MemoryStream())
                    {
                        r.Extract(ms);

                        byte[] block = ms.ToArray();
                        string readmeString = System.Text.Encoding.Default.GetString(block, 0, block.Length);
                        Console.WriteLine("README: {0}\n\nPress <ENTER> to continue...\n", readmeString);
                        Console.ReadLine();
                    }
                }
            }

The code above retrieves an entry by name.  The entry name is "files/Readme.txt" - in other words, in the zip file, there is a Readme.txt file, stored in the "files" directory.  That entry is extracted to a MemoryStream, which is then decoded into a string.  The text encoding used here, for extraction, is System.Text.Encoding.Default.  But you can use any encoding, just ensure that you use the same encoding for extraction that was used for storage.  [FYI: When you use AddFileFromString, DotNetZip encodes the string using the Default encoding]

 

Coordinator
May 8, 2009 at 1:53 PM
Edited May 8, 2009 at 1:54 PM

our replies crossed.

To answer your 2nd Q, something like this might work:

            var list = new System.Collections.Generic.List<String>();
            using (ZipFile zip = ZipFile.Read(_zipFileName))
            {
                foreach (ZipEntry e in zip.Entries)
                {
                    using (var ms = new MemoryStream())
                    {
                        e.Extract(ms);

                        byte[] block = ms.ToArray();
                        string contentString = System.Text.Encoding.Default.GetString(block, 0, block.Length);
                        list.Add(contentString);
                    }
                }
            }

 

But that assumes that all files were encoded with the default text encoding, which may or may not be a valid assumption. If there is a file in the zip, for example, that was UTF-8 encoded, then the string resulting from decoding it with the default encoding will not have the desired content.

May 8, 2009 at 1:55 PM

HI Cheeso, I think you replied just while I was replying as well. I was able to do something very similar to what you suggested and get a single file out of the zipfile into memorystream and then convert that to a string

Where im stuck now is decompressing an unknown number of files in a folder within the zip file back into some form (either memorystream or something else) so that I can manipulate and retrieve them.

Thoughts?

Thanks

 

 

 

May 8, 2009 at 1:56 PM

Once again our replies crossed over. I will try your second example out.

Thanks