Streamed creation of large zip file with low memory cost

Im developing an application that exports a lot of data from a database server and write this into a binary file added into a zip file.

I will open a cursor in database, read one record at a time and add the row into a filestream inside a zip file.

Im not able to read every record because the large amount of data will not fit inside the memory. The simliest way may be to create a file and add the file to the zip later by using ZipFile class but i would like to skip tha roundtrip to the disk.

One way is to use the ZipFile class, and the
AddEntry method on it, specifying a WriteDelegate. The writeDelegate is invoked when you call ZipFile.Save(), and within the WriteDelegate, you can write whatever you like into the stream given to you by the DotNetZip library. The data that you write
into the stream is then stored as an entry in the zip file. Example code:

If you have a readable stream that supplies the data for the zipentry, you can use
the AddEntry overload that accepts a stream. In this case, when you call ZipFile.Save(), the stream you passed will be read to the end, and the data placed into the zip file. If you'd like to defer the opening of the stream until
ZipFile.Save is called, then
there is another overload of AddEntry for that. In this one, you pass a delegate to open the stream, and another delegate to close the stream. DotNetZip will invoke your delegates on a just-in-time basis. example:

The final option I think, is the ZipOutputStream class. This is a different programming model, one where you have to do a little more work. In this one the zip archive is modeled as a stream, and you can just do the read from one stream (presumably your
database) and write into the ZipOutputStream.

In the first method you have a list of tables that you'd like to save. For each one you add an entry to the zip file. The second method is the delegate. It gets called once for each entry you added - once for each table to be saved. In the MyWriteDelegate
code, you open the DataReader (or is it BinaryReader?) for that particular table, then write that data directly into the zip entry.

That write delegate gets invoked N times before the zip.Save() completes.