作者:科技、互联网行业优质创作者
专注领域:.Net技术、软件架构、人工智能、数字化转型、DeveloperSharp、微服务、工业互联网、智能制造
点击右上方“关注”,里面有很多高价值技术文章,是你刻苦努力也积累不到的经验,能助你快速成长。升职+涨薪!!
1、通过 System.IO.Compression 命名空间中新增的ZipArchive、ZipFile等类实现。
不需要安装第三方的组件包,微软官方的实现,推荐使用
//压缩
System.IO.Compression.ZipFile.CreateFromDirectory(@"C:\Users\Pride\Pictures\test\123",@"C:\Users\Pride\Pictures\test\123.zip");
//解压
System.IO.Compression.ZipFile.ExtractToDirectory(@"C:\Users\Pride\Pictures\test\123.zip",@"C:\Users\Pride\Pictures\test\1234");
2、第三方类库(DotNetZip的使用)
- • SharpZipLib[1]
- • DotNetZip[2]
SharpZipLib的简单使用
DotNetZip的简单使用
压缩文件
using(ZipFilezip=newZipFile())
{
zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
zip.AddFile("ReadMe.txt");
zip.Save("Archive.zip");
}
更新文件,无需解压
using(ZipFilezip=ZipFile.Read("ExistingArchive.zip"))
{
//1.removeanentry,giventhename
zip.RemoveEntry("README.txt");
//2.Updateanexistingentry,withcontentfromthefilesystem
zip.UpdateItem("Portfolio.doc");
//3.modifythefilenameofanexistingentry
//(renameitandmoveittoasubdirectory)
ZipEntrye=zip["Table1.jpg"];
e.FileName="images/Figure1.jpg";
//4.insertormodifythecommentontheziparchive
zip.Comment="Thisziparchivewasupdated"+System.DateTime.ToString("G");
//5.finally,savethemodifiedarchive
zip.Save();
}
提取文件
using(ZipFilezip=ZipFile.Read("ExistingZipFile.zip"))
{
foreach(ZipEntryeinzip)
{
e.Extract(TargetDirectory,true);//true=>overwriteexistingfiles
}
}
3、原生 System.IO.Compression 实现 zip 的压缩与解压
不需要安装第三方的组件包,微软官方的实现,需要添加命名空间 using System.IO.Compression;
将指定目录压缩为Zip文件
///<summary>
///将指定目录压缩为Zip文件
///</summary>
///<paramname="folderPath">文件夹地址D:/1/</param>
///<paramname="zipPath">zip地址D:/1.zip</param>
publicstaticvoidCompressDirectoryZip(stringfolderPath,stringzipPath)
{
DirectoryInfodirectoryInfo=new(zipPath);
if(directoryInfo.Parent!=null)
{
directoryInfo=directoryInfo.Parent;
}
if(!directoryInfo.Exists)
{
directoryInfo.Create();
}
ZipFile.CreateFromDirectory(folderPath,zipPath,CompressionLevel.Optimal,false);
}
将指定文件压缩为Zip文件
///<summary>
///将指定文件压缩为Zip文件
///</summary>
///<paramname="filePath">文件地址D:/1.txt</param>
///<paramname="zipPath">zip地址D:/1.zip</param>
publicstaticvoidCompressFileZip(stringfilePath,stringzipPath)
{
FileInfofileInfo=newFileInfo(filePath);
stringdirPath=fileInfo.DirectoryName?.Replace("\\","/")+"/";
stringtempPath=dirPath+Guid.NewGuid()+"_temp/";
if(!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
fileInfo.CopyTo(tempPath+fileInfo.Name);
CompressDirectoryZip(tempPath,zipPath);
DirectoryInfodirectory=new(path);
if(directory.Exists)
{
//将文件夹属性设置为普通,如:只读文件夹设置为普通
directory.Attributes=FileAttributes.Normal;
directory.Delete(true);
}
}
解压Zip文件到指定目录(压缩单个文件的逻辑其实就是先将我们要压缩的文件复制到一个临时目录,然后对临时目录执行了压缩动作,压缩完成之后又删除了临时目录)
///<summary>
///解压Zip文件到指定目录
///</summary>
///<paramname="zipPath">zip地址D:/1.zip</param>
///<paramname="folderPath">文件夹地址D:/1/</param>
publicstaticvoidDecompressZip(stringzipPath,stringfolderPath)
{
DirectoryInfodirectoryInfo=new(folderPath);
if(!directoryInfo.Exists)
{
directoryInfo.Create();
}
ZipFile.ExtractToDirectory(zipPath,folderPath);
}
复制
欢迎“关注”我,里面有很多高价值技术文章,是你刻苦努力也积累不到的经验,能助你快速成长。升职+涨薪!!
最后,再给你分享个全网最全.NET视频学习教程:
领取方式:在我的个人主页的第一篇置顶文章中领取