Class IronZipArchive
An IronZipArchive for ZIP-type ".zip" Archives
Implements
Inherited Members
Namespace: IronZip
Assembly: IronZip.dll
Syntax
public class IronZipArchive : IronBaseArchive
IronZipArchive creates, reads, edits, and extracts ZIP (.zip) archives from C#. Start an empty archive with new IronZipArchive(), or open an existing one by passing a file path, a Stream, or a byte[] to a constructor (each accepts an optional password). Static factories cover the common starting points: FromFile, FromFiles, FromDirectory, and FromArchive. The type derives from IronBaseArchive and is disposable, so wrap it in a using statement.
The members group into four jobs. Building an archive uses Add and AddArchiveEntry to put files in. Editing uses Delete, ReplaceEntry, Contains, the Count property, and Entries(), which returns a List<Entry> describing each file. Securing uses Encrypt and SetPassword with an EncryptionMethods value, RemoveEncryption, or the ZipSaveOptions property for password and compression in one object. Output uses Save, SaveAs, and the static ExtractArchiveToDirectory to unpack an archive to disk.
A typical run constructs an archive, adds files, and saves. Set Comment to label the archive, adjust Compression for the size and speed trade-off, and call Encrypt before saving when the contents need protection. Because each Add takes one path, call it once per file or build from a directory with FromDirectory. Opening an existing archive through a path constructor lets you add or remove entries and call Save to write the changes back in place, while SaveAs writes to a new path; pass a password to that constructor to open an encrypted archive.
using IronZip;
using var archive = new IronZipArchive();
archive.Add("report.pdf");
archive.Add("image.png");
archive.SaveAs("output.zip");The create ZIP example and extract ZIP example show the round trip, and the create, read, and extract tutorial walks through a complete project.
Constructors
IronZipArchive(Byte[], String)
Open an existing protected Archive. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public IronZipArchive(byte[] Bytes, string Password = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | Bytes | Byte[] containing the existing archive. |
| System.String | Password | Password for opening the protected archive. |
IronZipArchive(Int32)
Creating an empty archive. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public IronZipArchive(int Compression = 9)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | Compression | Optionally sets the compression level. 0 is the weakest, 9 is the highest compression, with 9 as the default value. |
IronZipArchive(Stream, String)
Open an existing protected Archive. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public IronZipArchive(Stream StreamPath, string Password = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | StreamPath | Stream containing the existing archive. |
| System.String | Password | Password for opening the protected archive. |
IronZipArchive(String, String)
Open an existing Archive. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public IronZipArchive(string ArchivePath, string Password = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | ArchivePath | File path to open the existing Archive. Example: You may use an absolute path.
|
| System.String | Password | Password for opening the protected archive. |
Properties
Comment
Sets/Gets the comment for the entire archive.
Declaration
public string Comment { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Count
Retrieves the total number of entries in the archive.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The count of entries within the archive. |
ZipSaveOptions
Set the security settings for Zip Archive
Declaration
public ZipSaveOptions ZipSaveOptions { get; set; }
Property Value
| Type | Description |
|---|---|
| ZipSaveOptions |
Methods
Add(String)
Adds an archive entry (file) to the Archive. These entries will appear as files when the Archive is extracted.
Declaration
public void Add(string EntryFilePath)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | EntryFilePath | Filepath to the archive entry to add |
AddArchiveEntry(String)
Adds an archive entry (file) to the Archive. These entries will appear as files when the Archive is extracted.
Declaration
public void AddArchiveEntry(string EntryFilePath)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | EntryFilePath | Filepath to the archive entry to add |
Compression(Int32)
Sets the compression level (default is 9).
Declaration
public void Compression(int Compression)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | Compression | 0 is weakest, 9 is highest compression. 9 is default. |
Contains(String)
Checks if an entry with the specified name exists in the archive.
Declaration
public bool Contains(string EntryName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | EntryName | The name of the entry to check for existence. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Delete(String)
Deletes an entry from the archive.
Declaration
public void Delete(string EntryName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | EntryName | The name of the entry to be deleted. |
Dispose()
Check the license before using IronZIP
Declaration
public override void Dispose()
Overrides
Exceptions
| Type | Condition |
|---|---|
| IronSoftware.Exceptions.LicensingException |
Encrypt(String, EncryptionMethods)
Sets the password for a specific encryption method.
Declaration
public void Encrypt(string Password, EncryptionMethods EncryptionMethods)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Password | The password to set. |
| EncryptionMethods | EncryptionMethods | The encryption method to use. Supported types are traditional, AES128, and AES256. |
Entries()
Load the entries of an existing Archive
Declaration
public List<Entry> Entries()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<Entry> |
ExtractArchiveToDirectory(String, String, String)
Will load an Archive and extract it to a Directory (System Folder)
Declaration
public static void ExtractArchiveToDirectory(string ArchiveSourcePath, string SaveDirectoryPath, string Password = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | ArchiveSourcePath | Path to the Archive to extract |
| System.String | SaveDirectoryPath | Directory to extract files to |
| System.String | Password | Optional password to open the archive |
FromArchive(String, String)
Creates an instance of IronZipArchive from an existing archive.
Declaration
public static IronZipArchive FromArchive(string ArchivePath, string Password = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | ArchivePath | The path to the existing archive. |
| System.String | Password | The password for decryption (null if not encrypted). |
Returns
| Type | Description |
|---|---|
| IronZipArchive | An instance of IronZipArchive representing the specified archive. |
FromDirectory(String, Int32)
Declaration
public static IronZipArchive FromDirectory(string DirectoryPath, int Compression = 9)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | DirectoryPath | |
| System.Int32 | Compression |
Returns
| Type | Description |
|---|---|
| IronZipArchive |
FromFile(String, Int32)
Creates an Archive with a specific file to add to it. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public static IronZipArchive FromFile(string FilePath, int Compression = 9)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | FilePath | File Entry to add to the Archive |
| System.Int32 | Compression | Optionally set the compression level. 0 is weakest, 9 is highest compression. 9 is default. |
Returns
| Type | Description |
|---|---|
| IronZipArchive | Returns the constructed IronZipArchive with the file entry added. |
FromFiles(String[], Int32)
Creates an Archive with some files to add to it. Please use in a using block. Use the SaveAs method to export the archive.
Declaration
public static IronZipArchive FromFiles(string[] Paths, int Compression = 9)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String[] | Paths | File Entries to add to the Archive |
| System.Int32 | Compression | Optionally set the compression level. 0 is weakest, 9 is highest compression. 9 is default. |
Returns
| Type | Description |
|---|---|
| IronZipArchive | Returns the constructed IronZipArchive with the file entries added. |
RemoveEncryption()
Remove the encryption password from the zip archive.
Declaration
public void RemoveEncryption()
ReplaceEntry(String, String)
Replaces an entry within the archive with a new one.
Declaration
public void ReplaceEntry(string OldEntry, string NewEntry)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | OldEntry | The name of the entry to be replaced. |
| System.String | NewEntry | The name of the new entry to replace the existing one. |
Save()
Save the archive
Declaration
public override void Save()
Overrides
Remarks
Users cannot add additional entries after saving
Save(ZipSaveOptions)
Save the archive
Declaration
public void Save(ZipSaveOptions ZipSaveOptions = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ZipSaveOptions | ZipSaveOptions | Options for IronZipArchive |
Remarks
Users cannot add additional entries after saving
SaveAs(String)
Save the archive to the specified path
Declaration
public override void SaveAs(string Path)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Path | Save path |
Overrides
Remarks
Users cannot add additional entries after saving
SaveAs(String, ZipSaveOptions)
Save the archive to the specified path
Declaration
public void SaveAs(string Path, ZipSaveOptions ZipSaveOptions = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Path | Save path |
| ZipSaveOptions | ZipSaveOptions | Options for IronZipArchive |
Remarks
Users cannot add additional entries after saving
SetPassword(String, EncryptionMethods)
Sets the password for a specific encryption method.
Declaration
public void SetPassword(string Password, EncryptionMethods EncryptionMethods)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Password | The password to set. |
| EncryptionMethods | EncryptionMethods | The encryption method to use. Supported types are traditional, AES128, and AES256. |