
Introduction to ZIP Archives
ZIP archives are essential for compressing and consolidating one or more files or folders into a single container. This format not only reduces file size for storage and transmission but also retains metadata information, making file management easier. ZIP archives are widely used in both desktop and web applications for tasks such as uploading, downloading, sharing, and securing files through encryption.
In this article, we’ll explore various methods for programmatically creating ZIP archives in C#. You will learn how to:
- Create a ZIP archive using C#
- Add multiple files to a ZIP archive
- Add folders to a ZIP archive
- Create a password-protected ZIP archive using ZipCrypto
- Encrypt ZIP archive with AES encryption
- Set parallel compression mode
C# ZIP Library
The Aspose.ZIP for .NET is a robust API designed for zipping and unzipping files and folders within .NET applications. It offers features such as AES encryption to secure your files within ZIP archives. You can easily install the API from NuGet or download the binaries from the Downloads section.
Create a ZIP Archive in C#
To compress a file into a ZIP archive, follow these steps:
- Create a FileStream object for the output ZIP archive.
- Open the source file with a FileStream object.
- Instantiate the Archive class.
- Add the file to the archive using the Archive.CreateEntry(string, FileStream) method.
- Save the ZIP archive with the Archive.Save(FileStream) method.
Here’s a code sample demonstrating how to add a file to a ZIP archive using C#:
Add Multiple Files to a ZIP Archive in C#
To add multiple files to a ZIP archive, you can choose one of the following methods:
Using FileStream
Utilize the FileStream class to add multiple files to the ZIP archive with the Archive.CreateEntry(String, FileStream) method. Here’s how:
Using FileInfo
Alternatively, you can use the FileInfo class to add files. This method loads the files using the FileInfo class and adds them to the ZIP archive with the Archive.CreateEntry(String, FileInfo) method. See the example below:
Using File Path
You can also add files directly by providing their paths to the Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. Here’s how:
Add Folders to a ZIP Archive in C#
Adding a folder to a ZIP archive can be a convenient way to include multiple files. To ZIP a folder in C#, follow these steps:
- Create a FileStream object for the output ZIP archive.
- Instantiate the Archive class.
- Use the DirectoryInfo class to specify the folder to be zipped.
- Add the folder to the ZIP using the Archive.CreateEntries(DirectoryInfo) method.
- Save the ZIP archive with the Archive.Save(FileStream) method.
Here’s a code sample that demonstrates how to add a folder to a ZIP archive in C#:
Create a Password Protected ZIP using ZipCrypto in C#
To enhance security, you can create password-protected ZIP archives using ZipCrypto encryption. This is achieved by using the ArchiveEntrySettings class in the constructor of the Archive, which allows you to specify the encryption type.
Here’s an example of how to create a password-protected ZIP archive using ZipCrypto in C#:
Create Password Protected ZIP with AES Encryption
The Aspose.ZIP for .NET library also supports AES encryption for securing ZIP archives. You can choose from the following AES encryption methods:
- AES128
- AES192
- AES256
To apply AES encryption, use the AesEcryptionSettings class. Here’s how to create a password-protected ZIP with AES encryption in C#:
Set Parallel Compression Mode
For cases involving multiple entries, you can configure the API for parallel compression using the ParallelOptions class. Aspose.ZIP for .NET provides several parallel compression modes:
- Never - Do not compress in parallel.
- Always - Always compress in parallel (be cautious of out-of-memory issues).
- Auto - Automatically decide whether to use parallel compression based on the entries.
Here’s an example demonstrating how to set the parallel compression mode while zipping multiple files:
Learn More About C# .NET ZIP Library
Dive deeper into our C# .NET ZIP API with the following resources:
Try ZIP Archives Online
Explore our free online application, based on Aspose.ZIP for .NET, to compress files into ZIP archives easily.
Conclusion
In this article, you have learned how to programmatically create ZIP archives in C#. The provided code samples demonstrate how to add files and folders to ZIP archives. Additionally, we covered how to create password-protected ZIP archives using both ZipCrypto and AES encryption methods. We also discussed parallel compression for efficiently handling large files. If you have any questions or need further assistance, feel free to reach out via our forum.