Create a ZIP File Programmatically in .NET
Aspose.ZIP for .NET lets a C# application create a new ZIP archive without starting 7-Zip, WinRAR, or another desktop utility. Instantiate Archive, add source files as named entries, and call Save to write the finished archive to a file or stream.
This page focuses on creating the ZIP container and defining its contents. Compression is one part of that operation, but the primary result is a new, organized archive that can contain one file, many files, or a complete directory tree.
C# Example: Create a ZIP Archive with Multiple Files
The following example creates package.zip, adds a PDF and a CSV file, and assigns a clear name to each archive entry.Create package.zip and Add Two Files
using Aspose.Zip;
using (var archive = new Archive())
{
archive.CreateEntry("report.pdf", "report.pdf");
archive.CreateEntry("data.csv", "data.csv");
archive.Save("package.zip");
}
Install Aspose.ZIP for .NET
Install the Aspose.ZIP NuGet package and reference it from your .NET project. Archive creation runs in your application process and does not require a separate ZIP program.Package Manager Console
PM> Install-Package Aspose.Zip
Create ZIP from One File
Create an Archive, call CreateEntry with the desired entry name and source path, then save the archive. This is useful for packaging a report, export, or generated document for delivery.
Add Multiple Files
Call CreateEntry for every source file before saving. You control the entry name independently of the source path, so files from different directories can be arranged consistently inside the ZIP.
Create ZIP from a Folder
Add the files beneath a source directory while retaining safe relative paths. This produces a portable directory tree instead of exposing machine-specific absolute paths in the archive.
Write ZIP to a Stream
Save the completed archive to a writable stream when returning a ZIP from ASP.NET, uploading it to cloud storage, or keeping the workflow in memory without creating a temporary output file.
How ZIP Archive Creation Works in C#
- Install and reference Aspose.ZIP for .NET.
- Create an
Archiveinstance for the new ZIP container. - Add each source file with
CreateEntry, or add the contents of a directory. - Set compression or encryption options when the workflow requires them.
- Save the archive to a file path or writable stream.
- Dispose the archive and any streams opened by the application.
An archive is finalized when it is saved. Add all planned entries first so the central directory accurately describes the ZIP contents.
Control File Names and Folder Structure Inside the ZIP
The **source path** tells the application where to read a file; the **entry name** defines how that file appears inside the ZIP. Keeping those values separate lets you rename entries, group them under virtual folders, and avoid leaking local directory names.Use forward slashes for nested entry names such as documents/2026/report.pdf. Reject absolute paths, parent-directory segments such as ../, empty names, and unintended duplicates when entry names come from users or external systems.
Create a Password-Protected ZIP Archive
Aspose.ZIP supports traditional ZipCrypto and AES encryption with 128-, 192-, or 256-bit keys. Prefer AES when the software that will open the archive supports it; use ZipCrypto only when compatibility with older ZIP tools is the priority.Treat passwords as secrets: obtain them from secure configuration, avoid writing them to source code or logs, and communicate them through a channel separate from the archive. A lost password cannot be recovered from the API.
Choose How ZIP Entries Are Compressed
Deflate is the practical default for broadly compatible ZIP archives. Depending on the receiving software and data, entries may also use BZip2, LZMA, PPMd, or store mode without compression. Store mode can be appropriate for already compressed data when packaging speed matters more than a small size reduction.Formats such as JPEG, MP4, and many office documents are already compressed and may shrink very little. They can still benefit from being combined into one ZIP file with consistent names, folders, and access protection.
Create ZIP vs. Compress, Extract, or View
| Goal | Use this workflow | Expected result |
|---|---|---|
| Build a new ZIP and add files or folders | Create ZIP — this page | A new ZIP containing the selected entries |
| Reduce the size of a file or create another archive type | Compress files in C# | Compressed data or a different archive container |
| Restore files from an existing archive | Extract archives in C# | Original files written to a path or stream |
| Inspect archive entries without extracting them | View archive contents in C# | A list of entry names and available metadata |
| Add or change archive protection | Password-protect archives in C# | An encrypted archive that requires valid credentials |
Adding a PDF, DOCX, PNG, or another file to ZIP does not convert its internal format. The original file becomes an entry in the newly created archive.
Production Guidance for Creating ZIP Files
- Dispose archives and streams deterministically, especially in services that create many packages. - Write to a stream when returning a ZIP from an ASP.NET endpoint or storing it in cloud storage. - Validate source paths, entry names, file counts, and total input size before creating archives from untrusted requests. - Write to a temporary destination and replace the target only after a successful save when partial output would be harmful. - Avoid overwriting an existing archive unless the application explicitly allows it. - Normalize entry ordering, names, and timestamps when reproducible ZIP output is required by a build or signing process.Create ZIP Archives from Common File Types
Use the format-specific guides for examples built around the files your application packages most often. The source format remains unchanged inside the ZIP; each guide focuses on creating and organizing the archive.ZIP Creation Guides by Source File Type
Add documents, images, spreadsheets, media, and other source files to a new ZIP archive.
Create ZIP Files Online
Need a ZIP without writing code? Use the free online ZIP maker in a browser.
Aspose.ZIP for .NET Resources
- Product overview
- Developer documentation
ArchiveAPI referenceArchiveEntryAPI reference- C# examples on GitHub
- Aspose.ZIP package on NuGet
- Create an archive with one or more files
Frequently Asked Questions
1. How do I create a ZIP file in C#?Create an Archive, add a source file with CreateEntry, and call Save with the output ZIP path or stream. The first code example on this page shows the complete workflow.
2. Can I add multiple files to one ZIP archive?
Yes. Call CreateEntry once for each file and save the archive after all entries have been added.
3. Can I create a ZIP from an entire folder?
Yes. Add the files beneath the folder and use safe relative entry names to preserve the required directory structure inside the ZIP.
4. Can I create an AES-encrypted ZIP in .NET?
Yes. Configure entry settings with AES-128, AES-192, or AES-256 encryption and supply the password through secure application configuration.
5. Does ZIP creation require 7-Zip or WinRAR?
No. Aspose.ZIP creates the archive directly inside the .NET application without invoking external archive software.