C# 如何压缩和解压文件 - ZipArchive类的使用

ZipArchive 是 .NET 原生轻量 ZIP 处理方案,支持压缩、解压及流式读取;需注意目录创建、路径安全、ZIP64、密码保护、空文件夹模拟及中文编码等细节。

在 C# 中,ZipArchive 类(位于 System.IO.Compression 命名空间)是处理 ZIP 文件最常用、最轻量的方式,无需第三方库,.NET Framework 4.5+ 和 .NET Core/.NET 5+ 均原生支持。

压缩多个文件到 ZIP

ZipArchive 创建 ZIP 文件,本质是创建一个 FileStream,再用它初始化 ZipArchive(模式为 Create),然后逐个添加文件条目。

  • 目标 ZIP 文件路径需确保目录存在,否则会抛出异常;建议先调用 Directory.CreateDirectory(Path.GetDirectoryName(zipPath))
  • 添加文件时,zip.CreateEntry() 的参数是 ZIP 内部的相对路径(如 "docs/report.txt"),不是磁盘绝对路径
  • 推荐使用 using 确保流和归档正确释放,避免文件被占用
示例:
string zipPath = @"C:\output\archive.zip";
string[] files = { @"C:\data\file1.txt", @"C:\data\image.png" };

using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create)) { foreach (string file in files) { string entryName = Path.GetFileName(file); // 或自定义子目录结构 var entry = archive.CreateEntry(entryName); using (var entryStream = entry.Open()) using (var fileStream = File.OpenRead(file)) { fileStream.CopyTo(entryStream); } } }

解压 ZIP 到指定文件夹

解压更简单:用 ZipFile.OpenRead() 打开只读归档,遍历 Entries,对每个条目调用 ExtractToFile(),自动创建中间目录。

  • ExtractToFile() 第二个参数设为 true 可覆盖已存在的同名文件
  • 注意条目路径可能含 "../"(路径遍历风险),生产环境应校验 entry.FullName 是否安全(如不以 "..\" 开头且不含非法字符)
  • 若只需解压部分文件,可按 entry.Nameentry.FullName 过滤
示例:
string zipPath = @"C:\output\archive.zip";
string extractPath = @"C:\extracted";

Directory.CreateDirectory(extractPath);

using (var archive = ZipFile.OpenRead(zipPath)) { foreach (ZipArchiveEntry entry in archive.Entries) { string destinationPath = Path.Combine(extractPath, entry.FullName); // 安全校验(简化版) if (destinationPath.Contains("..\") || destinationPath.Contains("../")) throw new InvalidOperationException("Suspicious path detected");

    Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
    entry.ExtractToFile(destinationPath, overwrite: true);
}

}

读取 ZIP 内容而不解压

有时只需要检查 ZIP 里有什么文件或读取某个文本内容,不必落地到磁盘。这时直接打开条目的流即可。

  • entry.Open() 获取可读流,配合 StreamReader 读取文本内容
  • 注意:entry.Length 是原始未压缩大小,entry.CompressedLength 是 ZIP 中实际占用字节数
  • 如果 ZIP 很大但只读少量小文件,这种方式内存友好
示例(读取 ZIP 中的 config.json):
using (var archive = ZipFile.OpenRead(zipPath))
{
    var configEntry = archive.GetEntry("config.json");
    if (configEntry != null)
    {
        using (var stream = configEntry.Open())
        using (var reader = new StreamReader(stream))
        {
            string json = reader.ReadToEnd();
            // 解析 JSON...
        }
    }
}

常见注意事项

ZipArchive 灵活但有些细节容易踩坑:

  • 不支持 ZIP64(超 4GB 或超 65535 个文件)——老版本 .NET 不支持,.NET 6+ 已默认开启 ZIP64 支持
  • 密码保护 ZIP 不被原生 ZipArchive 支持,需用 SharpZipLibDotNetZip
  • 添加空文件夹?ZIP 标准本身不存“空目录”,但可通过添加路径以 "/" 结尾的条目模拟(如 archive.CreateEntry("logs/")
  • 中文文件名乱码?确保 ZIP 由 UTF-8 编码生成(.NET Core+ 默认 UTF-8;.NET Framework 需设置 ZipArchiveEntry.ExternalAttributes 或改用 SharpZipLib

基本上就这些。用好 ZipArchive,压缩解压逻辑清晰、性能稳定,适合绝大多数本地文件归档场景。