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.Name或entry.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); }}
95Shop仿醉品商城下载95Shop可以免费下载使用,是一款仿醉品商城网店系统,内置SEO优化,具有模块丰富、管理简洁直观,操作易用等特点,系统功能完整,运行速度较快,采用ASP.NET(C#)技术开发,配合SQL Serve2000数据库存储数据,运行环境为微软ASP.NET 2.0。95Shop官方网站定期开发新功能和维护升级。可以放心使用! 安装运行方法 1、下载软件压缩包; 2、将下载的软件压缩包解压缩,得到we
读取 ZIP 内容而不解压
有时只需要检查 ZIP 里有什么文件或读取某个文本内容,不必落地到磁盘。这时直接打开条目的流即可。
- 用
entry.Open()获取可读流,配合StreamReader读取文本内容 - 注意:
entry.Length是原始未压缩大小,entry.CompressedLength是 ZIP 中实际占用字节数 - 如果 ZIP 很大但只读少量小文件,这种方式内存友好
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支持,需用SharpZipLib或DotNetZip - 添加空文件夹?ZIP 标准本身不存“空目录”,但可通过添加路径以
"/"结尾的条目模拟(如archive.CreateEntry("logs/")) - 中文文件名乱码?确保 ZIP 由 UTF-8 编码生成(.NET Core+ 默认 UTF-8;.NET Framework 需设置
ZipArchiveEntry.ExternalAttributes或改用SharpZipLib)
基本上就这些。用好 ZipArchive,压缩解压逻辑清晰、性能稳定,适合绝大多数本地文件归档场景。









