- fileName (String)
- The filename to use for the new zip archive.
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?
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); }
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
Exception | Condition |
---|---|
ZipException |
Thrown if name refers to an existing file that is not a valid zip file.
|