This project has moved and is read-only. For the latest updates, please go here.

Using this to create and read a zip in memory

Jul 11, 2011 at 12:42 AM

Below I'm attempting to create a zip file, and then read it. However it's throwing an InvalidDataException.

            var someFileToStore = new MemoryStream(Encoding.UTF8.GetBytes("Hello World!")); ;
            var zipStream = new MemoryStream();
            ZipStorer zip = ZipStorer.Create(zipStream, "test");
            zip.AddStream(ZipStorer.Compression.Store, "/Hello.txt", someFileToStore, DateTime.Now.AddDays(-1), "Test");
            byte[] bytes = zipStream.ToArray();

            var fileAsStream = new MemoryStream(bytes);

            // Throws exception here...
            ZipStorer.Open(fileAsStream, FileAccess.Read);

Any thoughts?

Oct 31, 2011 at 10:38 AM

You need to call the close method before calling ToArray

byte[] bytes = zipStream.ToArray();

should be

[] bytes = zipStream.ToArray();
Nov 3, 2011 at 6:47 PM

Thanks CodeSmoke,

That's the right answer.