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 格式本身並沒有本質上的危害,但它可以像任何其他文件格式一樣儲存和傳輸受感染的文件。