jaime-olivares / zipstorer

A Pure C# Class to Store Files in Zip
MIT License
183 stars 63 forks source link

Stream from outside should not be closed #1

Closed dguder closed 7 years ago

dguder commented 9 years ago

I'm trying to use this class in a small WCF project where I don't want to add another 3rd party lib. I want to use the stream mode for packaging and then send the zip stream to another machine. Unfortunately the internal zipfilestream is closed and disposed within the Close() method. If I don't close the zip, then the EndRecord is not written. But when I close the zip then the underlying stream is closed. My suggestion is to set a flag when a filename is provided for Open() or Create(). When this is set, then ZipFileStream.Dispose(); and ZipFileStream = null; in function Close() should be omitted. I have a working solution here and can provide a pull request later.

jaime-olivares commented 8 years ago

Hi, sounds reasonable, please provide the pull request.

guillaumejay commented 7 years ago

Really, this is a problem. This class should NOT close a stream which it didn't create. I'm using it on a application, to create a zipfile (in a memory stream) which I'm going to send to my enduser. Right now,I can't send him a valid file, because I can't use Close to save the central directory information. If you don't want to change this comportment, can you at least add a method to save the central directory information without closing the stream ?

guillaumejay commented 7 years ago

I fixed it right now creating an optional parameter on Close. Maybe it would have been better to create a CloseWithoutClosingExternalStream methode ? (not sure..)

jaime-olivares commented 7 years ago

If you are using a memory stream, you can always recreate another memory stream based on your underlying byte array. Something like:

memoryStream = new MemoryStream(memoryStream.ToArray());