Zipfile objects in memory

Apr 27, 2010 at 11:43 AM

Hi guys, thanks for great software. I am having some problems, though (I am not the most experienced programmer...)

I wonder if it is possible to handle ZipFile objects in memory, without saving to disk? I have seen some examples using memoryStream, but they do not work the way I want them to work ;)



public ZipFile GenerateZipFile(SomeObject o)
          using (var zip = new ZipFile())
         // Some code that adds files to the zipFile
       return zip;


And the other way:


public SomeObject GetObjectsFromZipFile(ZipFile zip)
         var o = new SomeObject();
         // Some code that extracts the file from the zip file into o
       return o;

For the GenerateZipFile method, I have tried using a stream, but an empty document is added to the zip file:

      public ZipFile GenerateZipFile(SomeXmlObject o)
         using (var zipFile = new ZipFile())
            var serialiser = new Serialiser();
            MemoryStream xmlStream = serialiser.Serialise(o);
            zipFile.AddEntry("SomeXmlObject.xml", xmlStream);

            return zipFile;
I can see while debugging that the xmlStream has data, but the file inside the resulting zip file is 0 bytes...

I tried doing zip.Save(); before returning, but it will not let me save without having a filename or a stream. But I have an object, shouldn't that be enough?


Apr 27, 2010 at 11:56 AM

Ok, the last code snippet is now fixed - I had forgotten to reset the cursor before returning the stream from the serialiser...


But I still wonder about the other things =)


Apr 28, 2010 at 3:25 AM

I'm not clear on the question here.

Maybe you can rephrase it for me.

Apr 28, 2010 at 7:33 AM

Hi Cheeso, I'm not really sure what I'm asking anymore actually. I have some working code right now, so I'll work some more on that and see if I run into more problems.

I think I was generally frustrated because of streams, which was all new to me. So never mind me ;)

Apr 28, 2010 at 4:56 PM

ha!  ok, well, good luck!