AddFile Method (fileName, directoryPathInArchive)

DotNetZip

Ionic Zip Library v1.9.1.6 AddFile Method (fileName, directoryPathInArchive)
ReferenceIonic.ZipZipFileAddFile(String, String)
Adds a File to a Zip file archive, potentially overriding the path to be used within the zip archive.
Declaration Syntax
C# Visual Basic Visual C++
public ZipEntry AddFile(
	string fileName,
	string directoryPathInArchive
)
Public Function AddFile ( _
	fileName As String, _
	directoryPathInArchive As String _
) As ZipEntry
public:
ZipEntry^ AddFile(
	String^ fileName, 
	String^ directoryPathInArchive
)
Parameters
fileName (String)
The name of the file to add. The name of the file may be a relative path or a fully-qualified path.
directoryPathInArchive (String)
Specifies a directory path to use to override any path in the fileName. This path may, or may not, correspond to a real 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 fileName, if any. Passing the empty string ("") will insert the item at the root path within the archive.
Return Value
The ZipEntry corresponding to the file added.
Remarks

The file added by this call to the ZipFile is not written to the zip file archive until the application calls Save() on the ZipFile.

This method will throw an exception if an entry with the same name already exists in the ZipFile.

This version of the method allows the caller to explicitly specify the directory path to be used in the archive.

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

In this example, three files are added to a Zip archive. The ReadMe.txt file will be placed in the root of the archive. The .png file will be placed in a folder within the zip called images. The pdf file will be included into a folder within the zip called files\docs, and will be encrypted with the given password.

CopyC#
try
{
  using (ZipFile zip = new ZipFile())
  {
    // the following entry will be inserted at the root in the archive.
    zip.AddFile("c:\\datafiles\\ReadMe.txt", "");
    // this image file will be inserted into the "images" directory in the archive.
    zip.AddFile("c:\\photos\\personal\\7440-N49th.png", "images");
    // the following will result in a password-protected file called
    // files\\docs\\2008-Regional-Sales-Report.pdf  in the archive.
    zip.Password = "EncryptMe!";
    zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf", "files\\docs");
    zip.Save("Archive.zip");
  }
}
catch (System.Exception ex1)
{
  System.Console.Error.WriteLine("exception: {0}", ex1);
}
CopyVB.NET
Try
    Using zip As ZipFile = New ZipFile
        ' the following entry will be inserted at the root in the archive.
        zip.AddFile("c:\datafiles\ReadMe.txt", "")
        ' this image file will be inserted into the "images" directory in the archive.
        zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
        ' the following will result in a password-protected file called
        ' files\\docs\\2008-Regional-Sales-Report.pdf  in the archive.
        zip.Password = "EncryptMe!"
        zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf", "files\documents")
        zip.Save("Archive.zip")
    End Using
Catch ex1 As Exception
    Console.Error.WriteLine("exception: {0}", ex1)
End Try

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