C# | Visual Basic | Visual C++ |
- fileOrDirectoryName (String)
- the name of the file or directory to add.
- directoryPathInArchive (String)
- The name of the directory path to use within the zip archive. This path need not refer to an extant directory in the current filesystem. If the files within the zip are later extracted, this is the path used for the extracted file. Passing null (Nothing in VB) will use the path on the fileOrDirectoryName. Passing the empty string ("") will insert the item at the root path within the archive.
If adding a directory, the add is recursive on all files and subdirectories contained within it.
The name of the item may be a relative path or a fully-qualified path. The item added by this call to the ZipFile is not read from the disk nor written to the zip file archive until the application calls Save() on the ZipFile.
This version of the method allows the caller to explicitly specify the directory path to be used in the archive, which would override the "natural" path of the filesystem file.
Encryption will be used on the file data if the Password has been set on the ZipFile object, prior to calling this method.
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.
String[] itemnames= { "c:\\fixedContent\\Readme.txt", "MyProposal.docx", "c:\\SupportFiles", // a directory "images\\Image1.jpg" }; try { using (ZipFile zip = new ZipFile()) { for (int i = 1; i < itemnames.Length; i++) { // will add Files or Dirs, recurses and flattens subdirectories zip.AddItem(itemnames[i],"flat"); } zip.Save(ZipToCreate); } } catch (System.Exception ex1) { System.Console.Error.WriteLine("exception: {0}", ex1); }
Dim itemnames As String() = _ New String() { "c:\fixedContent\Readme.txt", _ "MyProposal.docx", _ "SupportFiles", _ "images\Image1.jpg" } Try Using zip As New ZipFile Dim i As Integer For i = 1 To itemnames.Length - 1 ' will add Files or Dirs, recursing and flattening subdirectories. zip.AddItem(itemnames(i), "flat") Next i zip.Save(ZipToCreate) End Using Catch ex1 As Exception Console.Error.WriteLine("exception: {0}", ex1.ToString()) End Try
Exception | Condition |
---|---|
FileNotFoundException |
Thrown if the file or directory passed in does not exist.
|