ZIP Archive Format

In the digital age, where data is king and storage efficiency paramount, ZIP archives have remained a steadfast and ubiquitous solution for compressing and organizing files.

About Zip Archive Information

ZIP is an archive format that can hold one or several lossless compressed files and folders, with or without encryption. ZIP is the most popular format known for more than thirty years, and supported on virtually every modern operating system. This format also extended to some other formats like JAR and OpenDocument.

Zip-archive File Format History Info

The first format specification of ZIP was published in 1989. The authors of ZIP idea were Phil Katz and Gary Conway. It was an immediate success due to its efficiency in reducing file sizes, making it easier to store and transmit files over slow internet connections and limited storage capacities of that era. Since 1993 it supports the most common Deflate compression method. Strong AES encryption was introduced in 2003. Despite being quite old, the standard has not become fossil - is being actively developed in nowadays. So in 2020 it was expanded with Zstandard, MP3 and XZ compression methods.

Structure of ZIP Archive

ZIP archives are designed in this hierarchical structure to efficiently store and organize compressed files while allowing for easy access to individual files within the archive. Each entry of archive are compressed separately, may be even with its own compression and encryption method. The entry inside archive has preceding header with original file metadata. The table of content resides in the end of file. Such approach allows composing self-extracting (SFX) archive, which also remains a valid ZIP archive due to executable part resides at the very beginning of SFX file.

Zip Compression Methods

Modern ZIP allows compressing data with Deflate, Deflate64™, BZIP2, LZMA, XZ, PPMd, Zstandard algorithms. File also can be stored without compression. The most common is Deflate, which is default in any archiver tool. There are also algorithms for lossless compression of specific files: MP3, JPEG, WAV. Aspose.ZIP fully supports Deflate, Deflate64™, Bzip2, LZMA, XZ, PPMd and Zstandard methods. It allows extraction WavPack compressed audio.

Zip Archive Supported Operations

Using Aspose.ZIP, you can handle ZIP archive in various ways. You can compose archive, add entries to existing archive without repacking, delete entries from existing archive without affecting the rest of the archive, and extract arbitrary entry or whole archive. You can encrypt and decrypt each entry separately with either legacy or modern AES encryption algorithm. Aspose.ZIP is able to create self-extracting and multi-volume ZIP archives.

Zip-file - Internal Structure

As being said, the central directory, which is the table of contents, is located toward the end of the ZIP archive. This directory acts as an index, listing all the file entries in the archive along with their positions within the archive. Entry headers can include creation and modification times, file system attributes, file name and comment. Entry header can be expanded with custom extra field to store custom metadata. It is possible to include Zip64 headers to support more than 65,535 entries per archive. The maximum size of ZIP archive with Zip64 extension is 264−1 bytes. ZIP archive can be split into several files. In such case the central directory stores offsets of each volume for quick access to particular entry.

Zip-file - Internal Structure

Popularity of the Zip Archive and Support

ZIP is the number one archive format. ZIP archives are so widely recognized and supported that various software applications, including popular file managers like Windows Explorer, macOS Finder, and open-source tools like 7-Zip and WinRAR , provide native support for creating and extracting ZIP files. This support extends to cloud storage services, email clients, and even mobile devices.

Examples of Using Zip-files

Zip archives are a ubiquitous file format for compressing and organizing data, making them essential in various software applications. Operations with archives via .NET empowers developers to work with Zip-files effortlessly. In the following code examples, we’ll delve into the capabilities of operations with Zip archive, demonstrating how to create new Zip archives and efficiently extract files from existing ones. These examples will help you harness the capabilities of this library to manage Zip archives seamlessly in your .NET projects

Create Zip file via .NET

Compose ZIP archive with two entries added by their paths.:

using (var archive = new Archive())
{
    archive.CreateEntry("entry_name1.dat", "input_file1.dat");
    archive.CreateEntry("entry_name2.dat", "input_file2.dat");
    archive.Save("result_archive.zip");
}

How to UnZIP files in C#

Steps: Unzip File to Folder in C#

  • Create an instance of Archive class based on your zip file.
  • Unzip the zip file using Archive.ExtractToDirectory method to your folder.
using (var archive = new Archive("input_archive.zip"))
{
    archive.ExtractToDirectory("outputDirectory");
}

Compressing Single File ZIP File

Steps: Compressing Single File in C#

  • Create a file stream with the desired name of your output zip file.
  • Create file stream of the data file to be compressed and encrypted.
  • Create an instance of Archive class and pass to it an instance of ArchiveEntrySettings class with AesEcryptionSettings instance, specifying the password.
  • Add data file created in step 2 using Archive.CreateEntry method.
  • Compress and encrypt the data file using Archive.Save method and pass it the file stream created in step 1.
using (var zipFile = File.Open("EncrypedWithAES256.zip", FileMode.Create))
{
    using (var source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
     {
           using (var archive = new Archive(new ArchiveEntrySettings(null, new  AesEcryptionSettings("p@s$", EncryptionMethod.AES256))))
           {
                  archive.CreateEntry("alice29.txt", source);
                  archive.Save(zipFile);
           }
      }
}

Deleting entries from existing archive

You do not have to repack whole archive when you only need to remove one entry from it. Steps:

  • Create a file stream with the desired name of your output zip file.
  • Create an instance of Archive class based on your zip file.
  • Delete the first file - the entry with zero index – from the archive.
  • Save the archive without excluded entry to output stream from step 1
using (FileStream outputZipFile = File.Open(withoutAnEntry.zip, FileMode.Create))
{
    using (Archive archive = new Archive(archive.zip))
    {
        archive.DeleteEntry(archive.Entries[0]);
        archive.Save(outputZipFile);
    }
}

Aspose.Zip offers individual archive processing APIs for popular development environments, listed below:

Aspose.Zip for .NETAspose.Zip via JavaAspose.Zip via Python.NET

Additional information about Zip-archives

People have been asking

1. Which encryption method should I choose?

ZIP format supports traditional (ZipCrypto) and modern AES encryption techniques. The former is a way weaker than the latter and easily breakable; ZipCrypto is supported by Aspose.ZIP for legacy. Please use only AES256 encryption when composing an archive.

2. Is there a way to take advantage of multi-core processors for compression?

Aspose.ZIP allows you to compose entries of ZIP archive by different CPU cores. This can significantly reduce total compression time. See an article with explanation and usage sample.

3. Can virus infect zip?

Yes, viruses can potentially infect files within a Zip archive if the files themselves are infected. While the Zip format itself is not inherently harmful, it can store and transport infected files, just like any other file format.