C#对文件的压缩+解压
https://mp.weixin.qq.com/s/tjAIIpXAS-bvARBRj-rMCA
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 (ZipFile zip = new ZipFile())
{
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 (ZipFile zip = ZipFile.Read("ExistingArchive.zip"))
{
// 1. remove an entry, given the name
zip.RemoveEntry("README.txt");
// 2. Update an existing entry, with content from the filesystem
zip.UpdateItem("Portfolio.doc");
// 3. modify the filename of an existing entry
// (rename it and move it to a sub directory)
ZipEntry e = zip["Table1.jpg"];
e.FileName ="images/Figure1.jpg";
// 4. insert or modify the comment on the zip archive
zip.Comment ="This zip archive was updated" + System.DateTime.ToString("G");
// 5. finally, save the modified archive
zip.Save();
}
提取文件
using (ZipFile zip = ZipFile.Read("ExistingZipFile.zip"))
{
foreach (ZipEntry e in zip)
{
e.Extract(TargetDirectory, true); // true => overwrite existing files
}
}
3、原生 System.IO.Compression 实现 zip 的压缩与解压
不需要安装第三方的组件包,微软官方的实现,需要添加命名空间using System.IO.Compression;
将指定目录压缩为Zip文件
/// <summary>
/// 将指定目录压缩为Zip文件
/// </summary>
/// <param name="folderPath">文件夹地址 D:/1/ </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressDirectoryZip(string folderPath, string zipPath)
{
DirectoryInfo directoryInfo = 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>
/// <param name="filePath">文件地址 D:/1.txt </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressFileZip(string filePath, string zipPath)
{
FileInfo fileInfo = new FileInfo(filePath);
string dirPath = fileInfo.DirectoryName?.Replace("\\", "/") + "/";
string tempPath = dirPath + Guid.NewGuid() + "_temp/";
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
fileInfo.CopyTo(tempPath + fileInfo.Name);
CompressDirectoryZip(tempPath, zipPath);
DirectoryInfo directory = new(path);
if (directory.Exists)
{
//将文件夹属性设置为普通,如:只读文件夹设置为普通
directory.Attributes = FileAttributes.Normal;
directory.Delete(true);
}
}
解压Zip文件到指定目录(压缩单个文件的逻辑其实就是先将我们要压缩的文件复制到一个临时目录,然后对临时目录执行了压缩动作,压缩完成之后又删除了临时目录)
/// <summary>
/// 解压Zip文件到指定目录
/// </summary>
/// <param name="zipPath">zip地址 D:/1.zip</param>
/// <param name="folderPath">文件夹地址 D:/1/</param>
public static void DecompressZip(string zipPath, string folderPath)
{
DirectoryInfo directoryInfo = new(folderPath);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
ZipFile.ExtractToDirectory(zipPath, folderPath);
}
文档更新时间: 2023-11-18 13:19 作者:admin