AddItem Method (fileOrDirectoryName, directoryPathInArchive)

DotNetZip

Ionic Zip Library v1.9.1.6 AddItem Method (fileOrDirectoryName, directoryPathInArchive)
ReferenceIonic.ZipZipFileAddItem(String, String)
Adds an item, either a file or a directory, to a zip file archive, explicitly specifying the directory path to be used in the archive.
Declaration Syntax
C# Visual Basic Visual C++
public ZipEntry AddItem(
	string fileOrDirectoryName,
	string directoryPathInArchive
)
Public Function AddItem ( _
	fileOrDirectoryName As String, _
	directoryPathInArchive As String _
) As ZipEntry
public:
ZipEntry^ AddItem(
	String^ fileOrDirectoryName, 
	String^ directoryPathInArchive
)
Parameters
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.
Return Value
The ZipEntry added.
Remarks

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.

Examples
This example shows how to zip up a set of files into a flat hierarchy, regardless of where in the filesystem the files originated. The resulting zip archive will contain a toplevel directory named "flat", which itself will contain files Readme.txt, MyProposal.docx, and Image1.jpg. A subdirectory under "flat" called SupportFiles will contain all the files in the "c:\SupportFiles" directory on disk.
CopyC#
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);
}
CopyVB.NET
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
Exceptions
Exception Condition
FileNotFoundException Thrown if the file or directory passed in does not exist.

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