ZIP64压缩扩展的兼容性问题

发布时间 2023-03-22 21:09:30作者: 无风听海

一、ZIP压缩的两种规范

zip64 格式是标准 zip 格式的扩展,实际上消除了 zip 存档中文件大小和数量的限制。

每种格式允许的最大值总结如下:

Standard Format Zip64 Format
Number of Files Inside an Archive 65,535 2^64 - 1
Size of a File Inside an Archive [bytes] 4,294,967,295 2^64 - 1
Size of an Archive [bytes] 4,294,967,295 2^64 - 1
Number of Segments in a Segmented Archive 999 (spanning) 65,535 (splitting) 4,294,967,295 - 1
Central Directory Size [bytes] 4,294,967,295 2^64 - 1

二、.NET提供的ZIP压缩能力

.NET提供了以下几个类来完成压缩功能,但是没有找到ZIP64的选项;
ZipFile
ZipArchive
ZipArchiveEntry
DeflateStream
GZipStream

三、DotNetZip提供的压缩能力

DotNetZip 是一个快速、免费的类库和工具集,用于处理 zip 文件。使用 VB、C# 或任何 .NET 语言轻松创建、提取或更新 zip 文件。

DotNetZip提供了Zip64Option来控制压缩的格式;默认情况下使用Never,即不使用ZIP64扩展;

    public enum Zip64Option
    {
        //
        // Summary:
        //     The default behavior, which is "Never". (For COM clients, this is a 0 (zero).)
        Default = 0,
        //
        // Summary:
        //     Do not use ZIP64 extensions when writing zip archives. (For COM clients, this
        //     is a 0 (zero).)
        Never = 0,
        //
        // Summary:
        //     Use ZIP64 extensions when writing zip archives, as necessary. For example, when
        //     a single entry exceeds 0xFFFFFFFF in size, or when the archive as a whole exceeds
        //     0xFFFFFFFF in size, or when there are more than 65535 entries in an archive.
        //     (For COM clients, this is a 1.)
        AsNecessary = 1,
        //
        // Summary:
        //     Always use ZIP64 extensions when writing zip archives, even when unnecessary.
        //     (For COM clients, this is a 2.)
        Always = 2
    }

我们可以通过以下方式来生成zip压缩文件

        public void ZIPContents(IDictionary<string, string> entrys, string zipFileName)
        {
            ZipFile zipfile = new ZipFile(zipFileName);
            zipfile.UseZip64WhenSaving = Zip64Option.AsNecessary;
            foreach (var entry in entrys)
            {
                zipfile.AddEntry(entry.Key, entry.Value);
            }

            zipfile.Save();
        }

四、ZIP64的兼容问题

ZIP64作为一个后来的扩展,虽然有很长的时间了,但是还是有一些操作系统或者开发语言的类库都不支持,从而会造成兼容性问题,最好是Zip64Option.AsNecessary选项,只有在必要的时候自动使用ZIP64位扩展;