The ZIP archives are used to compress and keep one or more files or folders into a single container. A ZIP archive encapsulates the files and folders as well as holds their metadata information. The most common usage of archiving is reducing the size of the files for storage or transmission and applying encryption for security. Apart from the file compression tools, the automated compression/extraction features are also used within various desktop and web applications for uploading, downloading, sharing, or encrypting the files. This article also targets similar scenarios and presents some easy ways of compressing files or folders and creating ZIP archivesprogrammatically using C#.
In this article, you will learn how to perform the following ZIP archiving operations:
Prerequisite – C# ZIP Library
Aspose.ZIP for .NET is a powerful and easy to use API for zipping or unzipping files and folders within .NET applications. It also provides AES encryption techniques to encrypt the files in ZIP archives. You can install the API from NuGet or download its binaries from the Downloads section.
Create a ZIP Archive in C#
With 7-Zip you have access to compressing in different formats, including 7z and zip. The 7z format is one of the better archive methods and can compress file size down a notable amount. How to Compress a File to Zip or 7z. 7-Zip allows for you to archive files in four different formats; including Zip and 7z. A – highlight the file(s) that you would like to archive. (CTRL+left click for multiple files) Then you can right click the files to bring up the menu. Select '7-Zip' then 'Add to Archive.' A – Select '7-Zip' from menu B – Then Select 'Add to archive' 7Zip will then pull up a window with several drop down menu options. Create a folder. The quickest way to create a zip file is to place all of the files that you want to archive into one folder. You can place multiple files and folders into the folder that you are creating the ZIP file from. Rename the folder to whatever you want the ZIP file to be named.
The following are the steps to compress a file by adding it into a ZIP archive:
- Create a FileStream object for the output ZIP archive.
- Open the source file into a FileStream object.
- Create an object of Archive class.
- Add the file into the archive using Archive.CreateEntry(string, FileStream) method.
- Create the ZIP archive using Archive.Save(FileStream) method.
The following code sample shows how to add a file into a ZIP archive using C#.
Add Multiple Files into ZIP Archive in C#
In case you want to add multiple files into a ZIP archive, you can do it using one of the following ways.
ZIP Multiple Files using FileStream
In this method, the FileStream class is used to add files to the ZIP archive using Archive.CreateEntry(String, FileStream) method. The following code sample shows how to add multiple files into a ZIP in C#.
ZIP Multiple Files using FileInfo
You can also use the FileInfo class for adding multiple files into a ZIP archive. In this method, the files will be loaded using the FileInfo class and added to the ZIP archive using Archive.CreateEntry(String, FileInfo) method. The following code sample shows how to ZIP multiple files using the FileInfo class in C#.
ZIP Files using Path
Instead of using FileInfo or FileStream classes for the ZIP entries, you can provide the file's path directly to Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. The following code sample shows how to ZIP files using their path.
Add Folders to a ZIP Archive in C#
You can ZIP a folder as well which could be another alternative of adding multiple files to a ZIP archive. Just put the source files into a folder and add that folder to the ZIP archive. The following are the steps to ZIP a folder:
- Create an object of FileStream class for the output ZIP archive.
- Create an instance of the Archive class.
- Use the DirectoryInfo class to specify the folder to be zipped.
- Use Archive.CreateEntries(DirectoryInfo) method to add folder into ZIP.
- Create the ZIP archive using Archive.Save(FileStream) method.
The following code sample shows how to add a folder to ZIP in C#.
Linux Make Zip Archive
Create a Password-Protected ZIP Archive using ZipCrypto in C#
You can protect the ZIP archives using passwords and apply ZipCrypto encryption. For this, the ArchiveEntrySettings class is used in the constructor of the Archive that accepts the encryption type as the second parameter.
The following code sample shows how to create a password-protected ZIP archive using ZipCrypto in C#.
Create AES Encrypted ZIP Archives in C#
Aspose.ZIP for .NET also lets you apply AES encryption to the ZIP archives. You can use the following AES encryption methods:
- AES128
- AES192
- AES256
In order to apply AES encryption, API offers the AesEcryptionSettings class. The following code sample shows how to create an AES encrypted ZIP archive in C#.
Set Parallel Compression Mode
You can also configure the API for parallel compression in case of multiple entries. For this, you can specify the parallel compression mode using ParallelOptions class. Aspose.ZIP for .NET provides the following parallel compression modes.
- Never – Do not compress in parallel.
- Always – Do compress in parallel (beware out of memory).
- Auto – Decide whether to use parallel compression or not upon the entries. This option may compress some entries in parallel only.
The following code sample shows how to set parallel compression mode while zipping multiple files.
Learn more about Aspose.ZIP for .NET
Explore more about our C# ZIP API using the following resources:
See also
The following are the steps to compress a file by adding it into a ZIP archive:
- Create a FileStream object for the output ZIP archive.
- Open the source file into a FileStream object.
- Create an object of Archive class.
- Add the file into the archive using Archive.CreateEntry(string, FileStream) method.
- Create the ZIP archive using Archive.Save(FileStream) method.
The following code sample shows how to add a file into a ZIP archive using C#.
Add Multiple Files into ZIP Archive in C#
In case you want to add multiple files into a ZIP archive, you can do it using one of the following ways.
ZIP Multiple Files using FileStream
In this method, the FileStream class is used to add files to the ZIP archive using Archive.CreateEntry(String, FileStream) method. The following code sample shows how to add multiple files into a ZIP in C#.
ZIP Multiple Files using FileInfo
You can also use the FileInfo class for adding multiple files into a ZIP archive. In this method, the files will be loaded using the FileInfo class and added to the ZIP archive using Archive.CreateEntry(String, FileInfo) method. The following code sample shows how to ZIP multiple files using the FileInfo class in C#.
ZIP Files using Path
Instead of using FileInfo or FileStream classes for the ZIP entries, you can provide the file's path directly to Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. The following code sample shows how to ZIP files using their path.
Add Folders to a ZIP Archive in C#
You can ZIP a folder as well which could be another alternative of adding multiple files to a ZIP archive. Just put the source files into a folder and add that folder to the ZIP archive. The following are the steps to ZIP a folder:
- Create an object of FileStream class for the output ZIP archive.
- Create an instance of the Archive class.
- Use the DirectoryInfo class to specify the folder to be zipped.
- Use Archive.CreateEntries(DirectoryInfo) method to add folder into ZIP.
- Create the ZIP archive using Archive.Save(FileStream) method.
The following code sample shows how to add a folder to ZIP in C#.
Linux Make Zip Archive
Create a Password-Protected ZIP Archive using ZipCrypto in C#
You can protect the ZIP archives using passwords and apply ZipCrypto encryption. For this, the ArchiveEntrySettings class is used in the constructor of the Archive that accepts the encryption type as the second parameter.
The following code sample shows how to create a password-protected ZIP archive using ZipCrypto in C#.
Create AES Encrypted ZIP Archives in C#
Aspose.ZIP for .NET also lets you apply AES encryption to the ZIP archives. You can use the following AES encryption methods:
- AES128
- AES192
- AES256
In order to apply AES encryption, API offers the AesEcryptionSettings class. The following code sample shows how to create an AES encrypted ZIP archive in C#.
Set Parallel Compression Mode
You can also configure the API for parallel compression in case of multiple entries. For this, you can specify the parallel compression mode using ParallelOptions class. Aspose.ZIP for .NET provides the following parallel compression modes.
- Never – Do not compress in parallel.
- Always – Do compress in parallel (beware out of memory).
- Auto – Decide whether to use parallel compression or not upon the entries. This option may compress some entries in parallel only.
The following code sample shows how to set parallel compression mode while zipping multiple files.
Learn more about Aspose.ZIP for .NET
Explore more about our C# ZIP API using the following resources:
See also
7-Zip Macro sur excel 2007. is a file archiver with a high compression ratio.
Download 7-Zip 19.00 (2019-02-21) for Windows:
Link | Type | Windows | Size |
---|---|---|---|
Download | .exe | 32-bit x86 | 1.2 MB |
Download | .exe | 64-bit x64 | 1.4 MB |
Download 7-Zip 21.01 alpha (2021-03-09) for Windows:
Link | Type | Windows | Size |
---|---|---|---|
Download | .exe | 32-bit x86 | 1.2 MB |
Download | .exe | 64-bit x64 | 1.4 MB |
Download | .exe | 64-bit ARM64 | 1.5 MB |
License
7-Zip is free software with open source. The most of the code is under the GNU LGPL license. Some parts of the code are under the BSD 3-clause License. Also there is unRAR license restriction for some parts of the code. Read 7-Zip License information.
You can use 7-Zip on any computer, including a computer in a commercial organization. You don't need to register or pay for 7-Zip.
The main features of 7-Zip
- High compression ratio in 7z format with LZMA and LZMA2 compression
- Supported formats:
- Packing / unpacking: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM
- Unpacking only: AR, ARJ, CAB, CHM, CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, UDF, UEFI, VDI, VHD, VMDK, WIM, XAR and Z.
- For ZIP and GZIP formats, 7-Zip provides a compression ratio that is 2-10 % better than the ratio provided by PKZip and WinZip
- Strong AES-256 encryption in 7z and ZIP formats
- Self-extracting capability for 7z format
- Integration with Windows Shell
- Powerful File Manager
- Powerful command line version
- Plugin for FAR Manager
- Localizations for 87 languages
7-Zip works in Windows 10 / 8 / 7 / Vista / XP / 2019 / 2016 / 2012 / 2008 / 2003 / 2000.
p7zip - the port of the command line version of 7-Zip to Linux/Posix.
On 7-Zip's SourceForge Page you can find a forum, bug reports, and feature request systems.
Compression ratio
Linux Make Zip Archive
We compared 7-Zip with WinRAR 5.20.
FILE SETS: Mozilla Firefox 34.0.5 for Windows and Google Earth 6.2.2.6613 for Windows.
Making Zip Archive
Archiver | Mozilla Firefox | Google Earth | ||
---|---|---|---|---|
65 files 85 280 391 bytes | 483 files 110 700 519 bytes | |||
Compressed size | Ratio | Compressed size | Ratio | |
7-Zip 9.35 -mx | 39 357 375 | 100% | 15 964 369 | 100% |
WinRAR 5.20 -m5 -s -ma5 -md128m | 41 789 543 | 106% | 17 035 432 | 107% |
Compression ratio results are very dependent upon the data used for the tests. Usually, 7-Zip compresses to 7z format 30-70% better than to zip format. And 7-Zip compresses to zip format 2-10% better than most of other zip compatible programs.