AddFile Method (fileName)

DotNetZip

Ionic Zip Library v1.9.1.6 AddFile Method (fileName)
ReferenceIonic.ZipZipFileAddFile(String)
Adds a File to a Zip file archive.
Declaration Syntax
C# Visual Basic Visual C++
public ZipEntry AddFile(
	string fileName
)
Public Function AddFile ( _
	fileName As String _
) As ZipEntry
public:
ZipEntry^ AddFile(
	String^ fileName
)
Parameters
fileName (String)
The name of the file to add. It should refer to a file in the filesystem. The name of the file may be a relative path or a fully-qualified path.
Return Value
The ZipEntry corresponding to the File added.
Remarks

This call collects metadata for the named file in the filesystem, including the file attributes and the timestamp, and inserts that metadata into the resulting ZipEntry. Only when the application calls Save() on the ZipFile, does DotNetZip read the file from the filesystem and then write the content to the zip file archive.

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

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 photos\personal. The pdf file will be included into a folder within the zip called Desktop.

CopyC#
try
{
  using (ZipFile zip = new ZipFile())
  {
    zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
    zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf");
    zip.AddFile("ReadMe.txt");

    zip.Save("Package.zip");
  }
}
catch (System.Exception ex1)
{
  System.Console.Error.WriteLine("exception: " + ex1);
}
CopyVB.NET
Try
     Using zip As ZipFile = New ZipFile
         zip.AddFile("c:\photos\personal\7440-N49th.png")
         zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf")
         zip.AddFile("ReadMe.txt")
         zip.Save("Package.zip")
     End Using
 Catch ex1 As Exception
     Console.Error.WriteLine("exception: {0}", ex1.ToString)
 End Try

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