ZIP & UnZIP Folders in .NET

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 Directory

Compressing Directory Contents

Steps: Compress Directory Contents into ZIP Files using C#

  1. Define the directory that contains the files to be compressed.
  2. Open two file streams for the output ZIP files using FileMode.Create.
  3. Create an instance of the Archive class.
  4. Use the CreateEntries method to add all files and folders from the specified directory to the archive.
  5. Save the compressed directory contents into multiple ZIP files using the Save method.
 1    // For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
 2    string dataDir = RunExamples.GetDataDir_Data();
 3
 4    using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Create))
 5    {
 6        using (FileStream zipFile2 = File.Open(dataDir + "CompressDirectory2_out.zip", FileMode.Create))
 7        {
 8            using (Archive archive = new Archive())
 9            {
10                DirectoryInfo corpus = new DirectoryInfo(dataDir + "CanterburyCorpus");
11                archive.CreateEntries(corpus);
12                archive.Save(zipFile);
13                archive.Save(zipFile2);
14            }
15        }
16    }

Extracting Compressed Directory Archive

Steps: Extracting a Compressed Directory Archive

  1. Open the ZIP file stream using FileMode.Open.
  2. Create an instance of the Archive class by passing the ZIP file stream.
  3. Extract the contents of the compressed archive to a specified directory using ExtractToDirectory.
1    // For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2    using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Open))
3    {
4        using (var archive = new Archive(zipFile))
5        {
6            archive.ExtractToDirectory(dataDir + "DecompressFolder_out");
7        }
8    }

Compress Files by File Info

Steps: Compress Files by File Info

  1. Open a file stream for the output ZIP file using FileMode.Create.
  2. Create FileInfo objects for the files you want to compress.
  3. Create an instance of the Archive class.
  4. Use the CreateEntry method to add each file to the archive.
  5. Save the archive using the Save method, with optional save options such as setting the encoding.
 1    // For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
 2    using (FileStream zipFile = File.Open(dataDir + "CompressFilesByFileInfo_out.zip", FileMode.Create))
 3    {
 4        FileInfo fi1 = new FileInfo(dataDir + "alice29.txt");
 5        FileInfo fi2 = new FileInfo(dataDir + "fields.c");
 6
 7        using (var archive = new Archive())
 8        {
 9            archive.CreateEntry("alice29.txt", fi1);
10            archive.CreateEntry("fields.c", fi2);
11            archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
12        }
13    }

Extract Archives with Different Entry Archives

Steps: Extract Archives with Different Entry Passwords

  1. Open the ZIP file stream using FileMode.Open.
  2. Create an instance of the Archive class by passing the ZIP file stream.
  3. Extract the first entry from the archive, providing a specific password for that entry using the Extract method.
  4. Extract the second entry with a different password, saving it to a separate output file.
1    // For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2    using (FileStream zipFile = File.Open(dataDir + "\\different_password.zip", FileMode.Open))
3    {
4        using (Archive archive = new Archive(zipFile))
5        {
6            archive.Entries[0].Extract(dataDir + "alice29_extracted_pass_out.txt", "first_pass");
7            archive.Entries[1].Extract(dataDir + "asyoulik_extracted_pass_out.txt", "second_pass");
8        }
9    }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.