Introduction

Aspose.ZIP for .NET API lets work with creating and managing archives in your applications without the need of any other 3rd party applications. Its detailed implementation lets manage ZIP archives easily.

This article descibes manipulation with _zip_ format only. Aspose.Zip support a lot of archive formats, they are listed [here](https://docs.aspose.com/zip/net/supported-file-formats/).

Basic Concepts of the API

Aspose.ZIP API conceptualizes the functionality of each item in an archive as Entry. Entry(ies) can be added, updated as well as removed from an archive. The API makes it simplified to work with an archive and its entries. This section gives an idea about different classes and methods exposed by the API and their usage.

Working with Archives

Creating Archive

An archive can be created using the Archive class exposed by the API. A basic archive can be created using this class as shown in the following code sample.

1var archive = new Archive();
Such archive is prepared for compression.

Creating Archive with Custom Compression

Archives can be created with additional settings specifying custom Compression techniques.

1 var archive = new Archive(new ArchiveEntrySettings(new CompressionSettings(CompressionMethod.Store), new AesEcryptionSettings("p@s$", EncryptionMethod.AES256)));

Saving Archives

Archives can be saved to the file system on the disc as well as to streams.

1archive.Save(stream, ArchiveSaveOptions saveOptions = null)
2
3archive.Save(filePath, ArchiveSaveOptions saveOptions = null)

Archive Extraction

Archives can be extracted by passing the source stream or path to the Archive extracting constructor.

1FileStream zipFileStream = File.Open("myarchive.zip", FileMode.Open);
2Archive archive = new Archive(zipFileStream);
3
4or 
5
6Archive archive = new Archive("myarchive.zip");

Extracting Encrypted Archives

Password-protected archives can be extracted by specifying the ArchiveLoadOptions

1 Archive archive = new Archive(sourceStream, new ArchiveLoadOptions() { DecryptiptionPassword = "p@s$" });
2
3or
4
5Archive archive = new Archive("myarchive.zip", new ArchiveLoadOptions() { DecryptiptionPassword = "p@s$" });

Working with Archive Entries

Adding Entries to Archive

Entries can be added to an archive using the CreateEntry method of Archive. The overloads offered by this method allow you to create entries in an archive from file path or stream along with specification Archive Entry settings. Files from a directory can also be added to an archive specifying the recursive search through the directory.

1CreateEntry(string name, Stream source, ArchiveEntrySettings newEntriesSettings) - Creates single entry with given name, data source, optional compression and encryption settings for the entry.
2
3CreateEntry(string name, string path, ArchiveEntrySettings newEntriesSettings) - Creates single entry with given name and file source, optional compression and encryption settings for the entry.
4
5CreateEntry(string name, FileInfo fileInfo, ArchiveEntrySettings newEntriesSettings) - Creates single entry with given name, file source, optional compression and encryption settings for the entry. Keeps attributes of NTFS file.
6
7CreateEntry(string name, Stream source, ArchiveEntrySettings newEntriesSettings, FileSystemInfo fileInfo) - Creates single entry with given name, data source, compression and encryption settings for the entry. Keeps attributes of NTFS file and respects optional parameters.
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.