Uncategorized

Add Files or Folders to ZIP Archives Programmatically in C

Create ZIP in C#

The ZIP archives are Used to compress and store one or more files or folders in a single container. A ZIP archive encapsulates the files and folders and their metadata information. The most common use of archiving is to reduce the size of files for storage or transmission and to use encryption for security. Apart from the file compression tools, the automated compression/extraction features are also used in various desktop and web applications for uploading, downloading, sharing or encrypting the files. This article also aims at similar scenarios and presents some easy ways to compress files or folders and create ZIP archives programmatically using C# ahead>.

This article will show you how to perform the following ZIP archiving operations:

Reading: How to create a zip file in c#

  • Creating a ZIP archive using C#
  • Add multiple files to a ZIP archive
  • Add folders to a ZIP archive
  • Create a password-protected ZIP archive with ZipCrypto
  • ZIP Encrypt archive with AES encryption
  • Set parallel compression mode

C# ZIP library#

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

Create ZIP archive in C##

The steps to compress a file by adding it are described below a ZIP archive:

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

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

Add multiple files to a ZIP archive in C##

In If you have multiple If you want to add files to a ZIP archive, you can do so in one of the following ways .

C# ZIP Multiple files with FileStream#

See also: How to Make a Website With Wix: Wix Tutorial for Beginners

This method uses the FileStream class to zip files using the Archive.CreateEntry(String, FileStream) method Add archive. The following code sample shows how to add multiple files to a ZIP file in C#.

C# ZIP Multiple Files with FileInfo#

You can also use the FileInfo class to add multiple Files to a ZIP archive. In this method, the files are loaded using the FileInfo class and added to the ZIP archive 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 FileInfo or FileStream classes for the ZIP To use entries, you can specify the path of the file directly to the Archive.CreateEntry(String name, String path, Boolean openImmediately, ArchiveEntrySettings newEntrySettings) method. The following code example shows how to zip files using their path.

Add folders to a ZIP archive in C##

You can compress a folder using a C# ZIP library, which could also 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 archive. Below are the steps to ZIP a folder in C#:

  • 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 compress.
  • Use the Archive.CreateEntries(DirectoryInfo) method to create folders ZIP.
  • Create the ZIP archive using the Archive.Save(FileStream) method.

The following code example shows how to ZIP a folder into C# added.

Create a password protected ZIP with ZipCrypto in C##

You can protect the ZIP archives with passwords and apply ZipCrypto encryption. To do this, the ArchiveEntrySettings class is used in the archive’s constructor, which accepts the encryption type as the second parameter.

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

C# Create Password Protected ZIP with AES Encryption#

See also: Stay at Home Mom Resume Example & Job Description Tips

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

  • AES128
  • AES192
  • AES256

To use AES Apply encryption API provides AesEcryptionSettings class. The following code example shows how to create a password-protected ZIP file with AES encryption in C#.

Set parallel compression mode#

You can also configure the API for parallel compression in case of multiple entries. To do this, you can specify the parallel compression mode using the ParallelOptions class. Aspose.ZIP for .NET offers the following parallel compression modes.

  • Never – Do not compress in parallel.
  • Always – Compress in parallel (caution: insufficient memory).
  • Auto – Decide whether the entries should be compressed in parallel or not. This option can only compress some entries in parallel.

The following code sample shows how to set the parallel compression mode while compressing multiple files using Aspose’s C# ZIP library.

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

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

  • Documentation
  • Source code examples

Create ZIP archives online#

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

Conclusion#

In this article you learned how to programmatically create ZIP archives in C#. The code examples showed how to add files and folders in the ZIP archives. In addition, we also covered how to create password-protected ZIP archives with ZipCrypto and AES encryption methods using C# Aspose’s ZIP library. In addition, the parallel compression of several entries is also discussed at the end. If you have any questions or suggestions, you can send them to us via our forum.

See also: How To Create an App: The Ultimate Guide for 2023

See also#

Unpack or unpack files with C# Unpack files in ZIP archives with C# Create 7z ( 7- Zip) Archive in C# .NETOpen or Extract 7z (7zip) File in C# .NETCreate and Extract GZip Archives using C#Convert RAR Files to ZIP Archive in C#Convert ZIP Archives to TAR in C#Create a Flat ZIP Archive in C# Executable Create self-extracting archive in C# Create TAR.GZ and TAR.XZ files in C# Delete files in a ZIP archive in C# Extract nested ZIP archives in C# Merge multiple ZIP or TAR archives in C#.

See also  How to Set up a G Suite Email Alias and Save Money

Related Articles

Leave a Reply

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

Back to top button