ZIP 存档格式

在数据为王、存储效率至关重要的数字时代,ZIP 存档仍然是压缩和组织文件的坚定且普遍的解决方案。

关于 Zip 存档信息

ZIP 是一种存档格式,可以保存一个或多个无损压缩文件和文件夹,加密或不加密。 ZIP 是三十多年来最流行的格式,几乎所有现代操作系统都支持。此格式还扩展到其他一些格式,例如 JAR 和 OpenDocument。

Zip 存档文件格式历史信息

ZIP 的第一个格式规范于 1989 年发布。ZIP 想法的作者是 Phil Katz 和 Gary Conway。它立即取得了成功,因为它可以有效地减少文件大小,使通过缓慢的互联网连接和那个时代有限的存储容量更容易存储和传输文件。自 1993 年以来,它支持最常见的 Deflate 压缩方法。强 AES 加密于 2003 年推出。尽管该标准已经相当古老,但它并没有成为化石 - 目前正在积极开发。因此在 2020 年,它通过 Zstandard、MP3 和 XZ 压缩方法进行了扩展。

ZIP 档案的结构

ZIP 存档采用这种分层结构设计,可有效存储和组织压缩文件,同时允许轻松访问存档中的各个文件。存档的每个条目都是单独压缩的,甚至可以采用自己的压缩和加密方法。存档内的条目前面有带有原始文件元数据的标头。目录位于文件末尾。这种方法允许编写自解压 (SFX) 存档,由于可执行部分驻留在 SFX 文件的开头,因此它仍然是有效的 ZIP 存档。

Zip 压缩方法

现代 ZIP 允许使用 Deflate、Deflate64™、BZIP2、LZMA、XZ、PPMd、Zstandard 算法压缩数据。文件也可以不压缩地存储。最常见的是 Deflate,它是任何归档工具的默认设置。还有一些用于特定文件的无损压缩的算法:MP3、JPEG、WAV。 Aspose.ZIP 完全支持 Deflate、Deflate64™、Bzip2、LZMA、XZ、PPMd 和 Zstandard 方法。它允许提取 WavPack 压缩音频。

Zip 存档支持的操作

使用Aspose.ZIP,您可以通过多种方式处理ZIP存档。您可以编写存档,将条目添加到现有存档而不重新打包,从现有存档中删除条目而不影响存档的其余部分,以及提取任意条目或整个存档。您可以使用传统或现代 AES 加密算法单独加密和解密每个条目。 Aspose.ZIP 能够创建自解压和多卷 ZIP 存档。

Zip 文件 - 内部结构

如上所述,中央目录(即目录)位于 ZIP 存档的末尾。该目录充当索引,列出存档中的所有文件条目及其在存档中的位置。条目标头可以包括创建和修改时间、文件系统属性、文件名和注释。条目标题可以使用自定义额外字段进行扩展以存储自定义元数据。可以包含 Zip64 标头以支持每个存档超过 65,535 个条目。具有 Zip64 扩展名的 ZIP 存档的最大大小为 264−1 字节。 ZIP 存档可以分为多个文件。在这种情况下,中央目录存储每个卷的偏移量,以便快速访问特定条目。

Zip 文件 - 内部结构

Zip Archive 的受欢迎程度和支持

ZIP 是排名第一的存档格式。 ZIP 存档得到了广泛的认可和支持,以至于各种软件应用程序,包括 Windows 资源管理器、macOS Finder 等流行的文件管理器,以及 [7-Zip](/zip/most-common-archives/what-is-7zip 等开源工具) 和 WinRAR ,为创建和解压 ZIP 文件提供本机支持。这种支持扩展到云存储服务、电子邮件客户端,甚至移动设备。

使用 Zip 文件的示例

Zip 存档是一种普遍存在的文件格式,用于压缩和组织数据,这使得它们在各种软件应用程序中至关重要。通过 .NET 进行档案操作使开发人员能够轻松地使用 Zip 文件。在下面的代码示例中,我们将深入研究 Zip 存档的操作功能,演示如何 创建新的 Zip 存档并高效地从中提取文件现有的。这些示例将帮助您利用该库的功能在 .NET 项目中无缝管理 Zip 档案

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);
    }
}

有关 Zip 档案的其他信息

人们一直在问

1. 压缩 zip 存档的最常见原因是什么?

压缩 Zip 存档的最常见原因是减小文件大小,以便有效存储、传输和组织数据。

2. 为什么 zip 称为存档?

Zip 被称为档案,因为它充当数字活页夹,将各种文件和目录捆绑到一个压缩实体中,类似于物理档案存储和组织文档的方式。此归档功能通过减少单个文件的数量和所需的总体存储空间来简化数据存储和传输。

3. 病毒会感染zip吗?

是的,如果文件本身被感染,病毒可能会感染 Zip 存档中的文件。虽然 Zip 格式本身并不有害,但它可以像任何其他文件格式一样存储和传输受感染的文件。