Create ZIP File via C#

Efficient Compression with C#: Reducing File Sizes within any .NET based application.

How to Create a Compressed Zip folder

It is a common practice to compress the contents of a file or folder into a ZIP archive for utilizing the space, keeping backup, carrying data and saving data in more organized and managed way. Moreover, later on extracts that content to a new folder or drive. So for How to Compress and Extract Files, C# ZIP API has made it simple. C# Programmerts can easily integrate API and write code for compression of images, PDF files, Word documents, Excel Spreadsheets even whole folders. API supports ZipCrypto for generating password protected archives as well as using AES encryption to encrypt archive. Even developers can set parallel compression mode or use BZip2, LZMA or PPMd Compression within archive. Below are few sample code snippets for creating C# based zip archives.

C# Create ZIP Image Files Archive

Creating archives having images including BMP, JPG, GIF, TIFF and more is simple using the API. Process of adding an image to archive is, Create a FileStream object for the output Images ZIP archive. Open the source image into FileStream object. Create API’s Archives class object. Add the image using CreateEntry method into the archive. Create the archive containing images using Save method .

C# Code for Creating ZIP Archive Containing Image Files.

  // For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
  using (FileStream zipFile = File.Open(dataDir + "CompressSingleFile_out.zip", FileMode.Create))
  {
      //File to be added to archive
      using (FileStream source1 = File.Open(dataDir + "picture123.png", FileMode.Open, FileAccess.Read))
      {
          using (var archive = new Archive(new ArchiveEntrySettings()))
          {
              archive.CreateEntry("picture123.png", source1);
              
              archive.Save(zipFile);
          }
      }
  }

C# Convert PDF to ZIP file

For creating ZIP archive of PDF documents, process is same as of images. Create Archive object, using its object, call the CreateEntry method having PDF file as parameter and finally invoke Save function for zip creation. API also provides some easy ways of compressing PDF files such as adding multiple files to a archive, storing without compression, adding folders and much more.

C# Code for Creating ZIP Archive of PDF Files

  var dir = "full directory path";
  using (var arch = new Archive())
  {
    arch.CreateEntry("filename.pdf", dir+"file.pdf");
    arch.Save(dir+"pdf-files-archived_result.zip");
  }

ZIP Multiple Files With Password

ForAPI supports archiving multiple files and folders with different encryption such as Traditional Encryption, AES128, AES192 and AES256 encryption. Process is same as of images and PDF archiving other than related encryption object.

C# Code for Archiving Multiple Files with Encryption

  using (FileStream zipFile = File.Open(dataDir + "CompressWithTraditionalEncryption_out.zip", FileMode.Create))
  {
      using (FileStream file1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
      {
      using (FileStream file2 = File.Open(dataDir + "asyoulike.txt", FileMode.Open, FileAccess.Read))
        {
          using (var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$"))))
          {
            archive.CreateEntry("alice29.txt", file1);
            archive.CreateEntry("asyoulik3.txt", file2);
            archive.Save(zipFile);
          }
                
        }
      }
  }

Compression functions in the documentation Aspose.ZIP for .NET

Aspose.Zip .NET API is recognized for its robust capability in managing intricate archive compression, enabling effortless transformation between diverse archive formats with exceptional efficiency and dependability. With the capacity to compress ZIP files into other prominent archive formats and vice versa, developers can ensure data integrity and optimal performance across a range of platforms and applications. This fundamental compression feature streamlines the data transfer, storage, and sharing processes, facilitating smooth compatibility with various software environments. By harnessing the potential of Aspose.Zip .NET API, users can easily manipulate archive files and seamlessly integrate them with their desired applications, thereby enhancing their overall productivity and workflow efficiency. The ZIP namespace incorporates classes representing zip archives and other common archive-related entities.