AddEntry Method (entryName, opener, closer)

DotNetZip

Ionic Zip Library v1.9.1.6 AddEntry Method (entryName, opener, closer)
ReferenceIonic.ZipZipFileAddEntry(String, OpenDelegate, CloseDelegate)
Add an entry, for which the application will provide a stream, just-in-time.
Declaration Syntax
C# Visual Basic Visual C++
public ZipEntry AddEntry(
	string entryName,
	OpenDelegate opener,
	CloseDelegate closer
)
Public Function AddEntry ( _
	entryName As String, _
	opener As OpenDelegate, _
	closer As CloseDelegate _
) As ZipEntry
public:
ZipEntry^ AddEntry(
	String^ entryName, 
	OpenDelegate^ opener, 
	CloseDelegate^ closer
)
Parameters
entryName (String)
the name of the entry to add
opener (OpenDelegate)
the delegate that will be invoked to open the stream
closer (CloseDelegate)
the delegate that will be invoked to close the stream
Return Value
the ZipEntry added
Remarks

In cases where the application wishes to open the stream that holds the content for the ZipEntry, on a just-in-time basis, the application can use this method and provide delegates to open and close the stream.

For ZipFile properties including Encryption, Password, SetCompression, ProvisionalAlternateEncoding, ExtractExistingFile, ZipErrorAction, and CompressionLevel, their respective values at the time of this call will be applied to the ZipEntry added.

Examples
This example uses anonymous methods in C# to open and close the source stream for the content for a zip entry. In a real application, the logic for the OpenDelegate would probably be more involved.
CopyC#
using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
    zip.AddEntry(zipEntryName,
                 (name) =>  File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ),
                 (name, stream) =>  stream.Close()
                 );

    zip.Save(zipFileName);
}
Examples
This example uses delegates in VB.NET to open and close the the source stream for the content for a zip entry. VB 9.0 lacks support for "Sub" lambda expressions, and so the CloseDelegate must be an actual, named Sub.
CopyVB.NET
Function MyStreamOpener(ByVal entryName As String) As Stream
    '' This simply opens a file.  You probably want to do somethinig
    '' more involved here: open a stream to read from a database,
    '' open a stream on an HTTP connection, and so on.
    Return File.OpenRead(entryName)
End Function

Sub MyStreamCloser(entryName As String, stream As Stream)
    stream.Close()
End Sub

Public Sub Run()
    Dim dirToZip As String = "fodder"
    Dim zipFileToCreate As String = "Archive.zip"
    Dim opener As OpenDelegate = AddressOf MyStreamOpener
    Dim closer As CloseDelegate = AddressOf MyStreamCloser
    Dim numFilestoAdd As Int32 = 4
    Using zip As ZipFile = New ZipFile
        Dim i As Integer
        For i = 0 To numFilesToAdd - 1
            zip.AddEntry(String.Format("content-{0:000}.txt"), opener, closer)
        Next i
        zip.Save(zipFileToCreate)
    End Using
End Sub

Assembly: Ionic.Zip (Module: Ionic.Zip) Version: 1.9.1.8 (1.9.1.8)