安装
Install-package System.IO.Compression.ZipFile
使用
压缩
ZipFile.CreateFromDirectory(string pathName, string fileName);//压缩
参数 pathName 为要压缩的目录的路径,指定为相对或绝对路径。相对路径被解释为相对于当前工作目录。
参数 fileName 要创建的压缩文件路径,指定为相对或绝对路径。相对路径被解释为相对于当前工作目录。
解压缩
ZipFile.ExtractToDirectory(string fileName, string pathName, bool overwriteFiles);//解压缩
参数 fileName 为待解压文件的路径,指定为相对或绝对路径。相对路径被解释为相对于当前工作目录。
参数 pathName 为解压的目标目录路径,指定为相对或绝对路径。相对路径被解释为相对于当前工作目录。
参数 overwriteFiles 是否要覆盖文件,true 覆盖,false 否
示例
string startPath = @".\start";
string zipPath = @".\result.zip";
string extractPath = @".\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath,true);