Add Files or Folders to ZIP Archives Programmatically in C

Create ZIP in C#

The ZIP files are It is used to compress and save one or more files or folders in a single container. A ZIP file encapsulates the files and folders and also contains their metadata information. The most common use of archiving is to reduce file sizes for storage or transmission and to apply encryption for security. In addition to file compression tools, automated compression/extraction features are also used in various web and desktop applications to upload, download, share, or encrypt files. This article also focuses on similar scenarios and introduces some easy ways to compress files or folders and create ZIP archives on a schedule using C#.

In this article, you will learn how to perform the following ZIP file operations:

  • Create a ZIP file using C#
  • Add multiple files to a ZIP archive
  • Adding folders to a ZIP archive
  • Create a password protected ZIP archive using ZipCrypto
  • Encrypt ZIP archive with AES encryption</li
  • Set Parallel Compression Mode

C# ZIP Library#

Aspose.ZIP for .NET is a powerful and easy-to-use API to zip or unzip files and folders within .NET applications. It also provides AES encryption techniques to encrypt ZIP files in C#. You can install the API from NuGet or download its binaries from the Downloads section.

Create a C## ZIP file

The following are the steps to compress a file by adding it to an archive ZIP:

  • Create a FileStream object for the output ZIP file.
  • Open the source file in a FileStream object.
  • Create an object from the Archive class.
  • Add the file to the archive using the Archive.CreateEntry(string, FileStream) method.
  • Create the ZIP file using the Archive.Save(FileStream) method .

The following code example shows how to add a file to a ZIP archive using C#.

Adding multiple files to a ZIP archive in C##

In case you want to add multiple files to a ZIP file, you can do it in one of the following ways.

C# ZIP Multiple files using FileStream#

In this method, The FileStream class is used to add files to the ZIP archive using the Arch method ive.CreateEntry(String, FileStream). The following code example shows how to add multiple files into a ZIP in C#.

C# ZIP Multiple Files Using FileInfo#

You can also use the FileInfo class to add multiple files into one Archive ZIP. In this method, the files will be uploaded using the FileInfo class and added to the ZIP file using the Archive.CreateEntry(String, FileInfo) method. The following code example shows how to compress multiple files using the FileInfo class in C#.

ZIP files using Path#

Instead of using the FileInfo or FileStream classes for ZIP entries, You can provide the file path directly to the Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. The following code sample shows how to zip files using their path.

Adding Folders to a ZIP Archive in C##

You can zip a folder with the C# ZIP library, which could be another alternative to adding multiple files to a ZIP archive. Just put the source files in a folder and add that folder to the ZIP file. The following are the steps to zip a folder in C#:

  • Create an object of the FileStream class for the output ZIP file.
  • Create an instance of the Archive class .
  • Use the DirectoryInfo class to specify the folder to compress.
  • Use the Archive.CreateEntries(DirectoryInfo) method to add a folder to the ZIP.
  • Create the ZIP file using the Archive.Save(FileStream) method.

The following code example shows how to add a folder to the ZIP in C#.

Create a password protected ZIP using ZipCrypto in C##

You can protect ZIP files with passwords and apply ZipCrypto encryption. This is done by using the ArchiveEntrySettings class in the archive’s constructor that accepts the encryption type as the second parameter.

The following code example shows how to create a password-protected ZIP archive using ZipCrypto in C#.

C# Create Password Protected ZIP with AES# Encryption

Aspose.ZIP for .NET also allows you to apply AES encryption to protect ZIP files. You can use the following AES encryption methods:

  • AES128
  • AES192
  • AES256

To apply the AES encryption, the API provides the AesEcryptionSettings class. The following code example shows how to create a password protected ZIP with AES encryption 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 the ParallelOptions class. Aspose.ZIP for .NET offers the following compression modes in parallel.

  • Never: do not compress in parallel.
  • Always: compress in parallel (careful with memory).
  • Automatic: decides whether or not to use parallel compression on the inputs. This option can compress some entries only in parallel.

The following code example shows how to set compression mode in parallel when compressing multiple files with Aspose’s C# ZIP library.

Learn More Learn more about the C# .NET ZIP Library#

Explore more about our C# ZIP API using the following resources:

  • Documentation
  • Source Code Examples

Create ZIP Files Online#

You can also try our free online application, which is based on Aspose.ZIP for .NET, to compress files into ZIP archives.

Conclusion#

In this article, you have learned how to create ZIP archives programmatically in C#. The code samples have demonstrated how to add files and folders to the ZIP archives. In addition, we have also covered how to create password protected ZIP archives with ZipCrypto and AES encryption methods using the C# Aspose ZIP library. Also, parallel compression of multiple inputs is also discussed at the end. In case you have any questions or queries, you can ask us through our forum.

See also#

Unzip or extract files using C#Unzip files into ZIP files using C#Create 7z ( 7-Zip) Archives in C# .NETOpen or Extract 7z (7zip) Archive in C# .NETCreate and Extract GZip Archives Using C#Convert RAR Files to ZIP Archive in C#Convert ZIP Files to TAR in C#Create a Flat ZIP Archive in C# Create self-extracting executable archive in C# Create TAR.GZ and TAR.XZ archives in C# Delete files in a ZIP archive in C# Extract nested ZIP archives in C# Combine multiple ZIP or TAR archives in C#.

See Also:  How to Make a Forum Website Like Reddit in 8 Simple Steps

Leave a Reply

Your email address will not be published. Required fields are marked *