Trying to add memorystream to zip

Apr 6, 2010 at 4:22 AM

Thanks for your efforts in creating this class. Just what I was looking for.  I do however have a slight problem trying to add a memorystream to a zip file.

I am using v2.35.

What I am trying to do is create a zip file which contains a file attachment from a document library in SharePoint and an xml file that contains some of the metdata fields for the item that contains the attachent.

I have got the file being added to the zip ok, and I can create the xml document ans save it as a file ok (it contains values in the correct format)

When I try saving the xmlDocument as a MemoryStream and use that in ZipStorer to add a file, I get an entry created in the zip file for it, but it is empty, and I cant open it without getting an error message, so I assume it hasn't been created properly)

It could be something simple I am not doing, or doing wrong, as I am a relative new comer to c#. Below is the code I am using which is just after the creation of the xmlDocument. (This is inside a using statemet for ZipStorer)

			   XmlText xmltext;
                                XmlDocument xmlDoc = new XmlDocument();
                                //let's add the XML declaration section
                               // all the xml creation stuff goes here

                                MemoryStream xmlMemoryStream = new MemoryStream();

                                Console.WriteLine(string.Format("Stream Can write: {0}, size: {1}", xmlMemoryStream.CanWrite.ToString(), xmlMemoryStream.Capacity.ToString()));
                                zipFile.AddStream(ZipStorer.Compression.Deflate, "FileDetails.xml", xmlMemoryStream, DateTime.Now, "");
                                theStage = "Adding xml attachment to zip file stream...";

                                MemoryStream readme = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(string.Format("{0}\r\nThis file has been {1} using the ZipStorer class, by Jaime Olivares.",
                                                                        DateTime.Now, "created")));
                                zipFile.AddStream(ZipStorer.Compression.Store, "readme.txt", readme, DateTime.Now, "Please read");

Not sure exaclty what it means, bute the CanWrite flag is true, and the Capacity is over 600, so I am assuming that the stream, has some content.

Any ideas?




Apr 6, 2010 at 4:24 AM

Forgot to mention, that I added your example memoryStream code, and that works perfect.

Apr 6, 2010 at 3:18 PM

Hi Jeff,

MemoryStream is a bit picky with buffers. Try to use xmlMemoryStream.Flush() before adding to zip.

Also, please do not forget to close the zip storage.

Apr 7, 2010 at 8:32 AM

Thanks for your response Jamie, but the flush() didnt seem to do anything.

I did however manage to get it working. I had a look at the Position property of the stream before adding it to the zip , and noticed that it was set to the length of the string. So, I replaced the Flush() with a Position = 0 and it now works.

                                Console.WriteLine(string.Format("Stream Can write: {0}, size: {1}, Position: {2}", 
                                                  xmlMemoryStream.CanWrite.ToString(), xmlMemoryStream.Capacity.ToString(), xmlMemoryStream.Position.ToString()));
                                xmlMemoryStream.Position = 0;
                                zipFile.AddStream(ZipStorer.Compression.Deflate, "FileDetails.xml", xmlMemoryStream, DateTime.Now, "");

 All working now. Onto the next challenge!!