ZipFile Constructor (fileName)

DotNetZip

Ionic Zip Library v1.9.1.6 ZipFile Constructor (fileName)
ReferenceIonic.ZipZipFileZipFile(String)
Creates a new ZipFile instance, using the specified filename.
Declaration Syntax
C# Visual Basic Visual C++
public ZipFile(
	string fileName
)
Public Sub New ( _
	fileName As String _
)
public:
ZipFile(
	String^ fileName
)
Parameters
fileName (String)
The filename to use for the new zip archive.
Remarks

Applications can use this constructor to create a new ZipFile for writing, or to slurp in an existing zip archive for read and update purposes.

To create a new zip archive, an application can call this constructor, passing the name of a file that does not exist. The name may be a fully qualified path. Then the application can add directories or files to the ZipFile via AddDirectory(), AddFile(), AddItem() and then write the zip archive to the disk by calling Save(). The zip file is not actually opened and written to the disk until the application calls ZipFile.Save(). At that point the new zip file with the given name is created.

If you won't know the name of the Zipfile until the time you call ZipFile.Save(), or if you plan to save to a stream (which has no name), then you should use the no-argument constructor.

The application can also call this constructor to read an existing zip archive. passing the name of a valid zip file that does exist. But, it's better form to use the static Read(String) method, passing the name of the zip file, because using ZipFile.Read() in your code communicates very clearly what you are doing. In either case, the file is then read into the ZipFile instance. The app can then enumerate the entries or can modify the zip file, for example adding entries, removing entries, changing comments, and so on.

One advantage to this parameterized constructor: it allows applications to use the same code to add items to a zip archive, regardless of whether the zip file exists.

Instances of the ZipFile class are not multi-thread safe. You may not party on a single instance with multiple threads. You may have multiple threads that each use a distinct ZipFile instance, or you can synchronize multi-thread access to a single instance.

By the way, since DotNetZip is so easy to use, don't you think you should donate $5 or $10?

Examples
This example shows how to create a zipfile, and add a few files into it.
CopyC#
String ZipFileToCreate = "archive1.zip";
String DirectoryToZip  = "c:\\reports";
using (ZipFile zip = new ZipFile())
{
  // Store all files found in the top level directory, into the zip archive.
  String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip);
  zip.AddFiles(filenames, "files");
  zip.Save(ZipFileToCreate);
}
CopyVB.NET
Dim ZipFileToCreate As String = "archive1.zip"
Dim DirectoryToZip As String = "c:\reports"
Using zip As ZipFile = New ZipFile()
    Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
    zip.AddFiles(filenames, "files")
    zip.Save(ZipFileToCreate)
End Using
Exceptions
Exception Condition
ZipException Thrown if name refers to an existing file that is not a valid zip file.

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