Zip or UnZip Files in .NET

Overview

This article will teach you how to programmatically compress Zip files using a variety of methods utilising C# or the.NET API and sample code. You will learn how to zip or compress one or more files, as well as how to zip files simultaneously. You will also learn how to decompress or unzip files as well.

Zip or Compress files in C# and Decompress or Unzip files in C#

Aspose.ZIP API lets you compress and decompress files without worrying about the underlying file structure. This article shows working with single as well as multiple file compression.

Compressing Files

Compressing Single File

Steps: Compressing Single File in C#

  1. Create a file stream with the desired name of your output zip file.
  2. Create file stream of the data file to be compressed.
  3. Create an instance of Archive class and pass it instance of ArchiveEntrySettings class.
  4. Add data file created in step 2 using Archive.CreateEntry method.
  5. Zip the data file using Archive.Save method and pass it the file stream created in step 1.

Compressing Multiple Files

Steps: Compressing Multiple Files in C#

  1. Create a file stream with the desired name of your output zip file.
  2. Create file stream of the first data file to be compressed.
  3. Create file stream of the second data file to be compressed.
  4. Create an instance of Archive class.
  5. Add data files created in step 2 and step 3 using Archive.CreateEntry method.
  6. Create an instance of ArchiveSaveOptions class.
  7. Zip the data files using Archive.Save method and pass it the file stream created in step 1 and instance of ArchiveSaveOptions created in above step.

Compress Files by File Info

Steps: Compress Files by File Info in C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Create FileInfo object of your first data file to be compressed.
  3. Create FileInfo object of your second data file to be compressed.
  4. Create an instance of Archive class.
  5. Add data files created in step 2 and step 3 using Archive.CreateEntry method.
  6. Create an instance of ArchiveSaveOptions class.
  7. Zip the data files using Archive.Save method and pass it the file stream created in step 1 and instance of ArchiveSaveOptions created in above step.

Storing Files to Archives without Compression

Steps: Storing Files to Archives without Compression using C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Create FileInfo objects for your data files to be stored in archive.
  3. Create an instance of ArchiveEntrySettings class and pass it instance of StoreCompressionSettings class.
  4. Create an instance of Archive class and pass it instance of ArchiveEntrySettings class created in above step.
  5. Add FileInfo objects created in step 2 using Archive.CreateEntry method.
  6. Create instance of ArchiveSaveOptions and set Encoding = Encoding.ASCII.
  7. Zip the data files using Archive.Save method and pass it the file stream created in step 1 and instance of ArchiveSaveOptions created in above step.

Using Parallelism to Compress Files

Steps: Using Parallelism to Compress Files using C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Open file streams with FileMode.Open and FileAccess.Read for your first and second data files to be compressed.
  3. Create an instance of Archive class.
  4. Add data files created in step 2 using Archive.CreateEntry method.
  5. Create an instance of ParallelOptions and set ParallelCompressInMemory = ParallelCompressionMode.Always.
  6. Create an instance of ArchiveSaveOptions and set its property ParallelOptions with the above instance.
  7. Zip the data files using Archive.Save method and pass it the file stream created in step 1 and instance of ArchiveSaveOptions created in above step.

LZMA Compression within ZIP Archive

The Lempel–Ziv–Markov chain algorithm (LZMA) is an algorithm used to perform lossless data compression. LZMA uses a dictionary compression algorithm, the compressed stream is a stream of bits. LZMA compression within the ZIP archive allows ZIP containers to contain LZMA compressed entries. The following code example shows the implementation of LZMA compression using Aspose.ZIP API.

Steps: LZMA Compression within ZIP Archive using C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Create an instance of ArchiveEntrySettings and pass it the instance of LzmaCompressionSettings class.
  3. Create an instance of Archive class and pass it the instance of ArchiveEntrySettings created above.
  4. Add data files to be compressed via file paths using Archive.CreateEntry method.
  5. Zip the data files using Archive.Save method.

BZip2 Compression within ZIP Archive

BZip2 compression settings allow ZIP container to contain BZip2 compressed entries. The following code example shows the implementation of BZip2 compression using Aspose.ZIP API.

Steps: BZip2 Compression within ZIP Archive using C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Create an instance of ArchiveEntrySettings and pass it the instance of Bzip2CompressionSettings class.
  3. Create an instance of Archive class and pass it the instance of ArchiveEntrySettings created above.
  4. Add data files to be compressed via file paths using Archive.CreateEntry method.
  5. Zip the data files using Archive.Save method.

PPMd Compression within ZIP Archive

PPMd compression settings allow ZIP container to contain PPMd compressed entries. The following code example demonstrated the implementation of PPMd compression using Aspose.ZIP API.

Steps: PPMd Compression within ZIP Archive using C#

  1. Open a file stream with FileMode.Create with the desired name of your output zip file.
  2. Create an instance of ArchiveEntrySettings and pass it the instance of PPMdCompressionSettings class.
  3. Create an instance of Archive class and pass it the instance of ArchiveEntrySettings created above.
  4. Add data files to be compressed via file paths using Archive.CreateEntry method.
  5. Zip the data files using Archive.Save method.

Decompressing Archives

Decompress Archive having Single File

Decompress Archive having Multiple Files

Extract Stored Archive without Compression

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.