Search code examples
c#filestreamsharpziplibzipoutputstream

When should I call Finish on the ZipOutputStream (SharpZipLib)


SharpZipLib is a .Net implementation for Java library for working with archives. It provides ZipOutputStream class, which derives from DeflaterOutputStream and has Finish() method. In the docs here it is stated that the Finish() method "will write the central directory at the end of the zip file and flush the stream" and that it "is automatically called when the stream is closed". So I wonder if I should call Finish() method at all if I call Close() anyway.

UPDATE: the question should have been stated another way: why should I use Finish() method if I can call Dispose() or Close() that will do the job (including calling Finish() method)?


Solution

  • From the documentation of ZipOutputStream - Finish():

    This is automatically called when the stream is closed.

    As for the closing, the ZipOutputStream needs to be properly disposed. There is no need to call for Close method.

    using (var zip = new ZipOutputStream(new FileStream("some path", FileMode.OpenOrCreate)))
    {
        // add some files.
       
    }