今天梳理一下项目中用到的压缩、解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件。今天就好好整理记录下,别下次遇到需求又重头开始了。
DotNetZip
DotNetZip是一个开源的免费类库,主要提供了快速操作zip文件的工具集,VB、C#任何.Net语言都可以通过它创建、解压缩zip文件。我使用该类库最主要的目的还是因为它可以创建带密码保护的压缩文件。
只有设置了
zip.Password = "password"
之后,被压缩的文件才会有密码保护
/// <summary>
/// 压缩文件/文件夹
/// </summary>
/// <param name="filePath">需要压缩的文件/文件夹路径</param>
/// <param name="zipPath">压缩文件路径(zip后缀)</param>
/// <param name="password">密码</param>
/// <param name="filterExtenList">需要过滤的文件后缀名</param>
public static void CompressionFile(string filePath, string zipPath, string password = "", List<string> filterExtenList = null)
{
try
{
using (ZipFile zip = new ZipFile(Encoding.UTF8))
{
if (!string.IsNullOrWhiteSpace(password))
{
zip.Password = password;
}
if (Directory.Exists(filePath))
{
if (filterExtenList == null)
zip.AddDirectory(filePath);
else
AddDirectory(zip, filePath, filePath, filterExtenList);
}
else if (File.Exists(filePath))
{
zip.AddFile(filePath,"");
}
zip.Save(zipPath);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 添加文件夹
/// </summary>
/// <param name="zip">ZipFile对象</param>
/// <param name="dirPath">需要压缩的文件夹路径</param>
/// <param name="rootPath">根目录路径</param>
/// <param name="filterExtenList">需要过滤的文件后缀名</param>
public static void AddDirectory(ZipFile zip, string dirPath, string rootPath, List<string> filterExtenList)
{
var files = Directory.GetFiles(dirPath);
for (int i = 0; i < files.Length; i++)
{
//如果Contains不支持第二个参数,就用.ToLower()
if (filterExtenList == null || (filterExtenList != null && !filterExtenList.Any(d => Path.GetExtension(files[i]).Contains(d, StringComparison.OrdinalIgnoreCase))))
{
//获取相对路径作为zip文件中目录路径
zip.AddFile(files[i], Path.GetRelativePath(rootPath, dirPath));
//如果没有Path.GetRelativePath方法,可以用下面代码替换
//string relativePath = Path.GetFullPath(dirPath).Replace(Path.GetFullPath(rootPath), "");
//zip.AddFile(files[i], relativePath);
}
}
var dirs = Directory.GetDirectories(dirPath);
for (int i = 0; i < dirs.Length; i++)
{
AddDirectory(zip, dirs[i], rootPath, filterExtenList);
}
}
SharpCompress
SharpCompress是用到现在,感觉功能最强大的压缩、解压开源插件。它支持处理zip、rar、7z等多种格式的压缩文件,使用方式也很简单。当然,最让我难受的是创建压缩文件的时候没法设置密码~所以才有了上面DotnetZip的代码。
SharpCompress版本不同,设置ArchiveEncoding的方式也不同,默认设置了UTF8防止解压乱码。
通过设置ArchiveType切换生成不同格式压缩文件
/// <summary>
/// 压缩文件/文件夹
/// </summary>
/// <param name="filePath">需要压缩的文件/文件夹路径</param>
/// <param name="zipPath">压缩文件路径(zip后缀)</param>
/// <param name="filterExtenList">需要过滤的文件后缀名</param>
public static void CompressionFile(string filePath, string zipPath, List<string> filterExtenList = null)
{
try
{
using (var zip = File.Create(zipPath))
{
var option = new WriterOptions(CompressionType.Deflate)
{
ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding()
{
Default = Encoding.UTF8
}
};
using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, option))
{
if (Directory.Exists(filePath))
{
//添加文件夹
zipWriter.WriteAll(filePath, "*",
(path) => filterExtenList == null ? true : !filterExtenList.Any(d => Path.GetExtension(path).Contains(d, StringComparison.OrdinalIgnoreCase)), SearchOption.AllDirectories);
}
else if (File.Exists(filePath))
{
zipWriter.Write(Path.GetFileName(filePath), filePath);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <param name="dirPath">解压到文件夹路径</param>
/// <param name="password">密码</param>
public static void DeCompressionFile(string zipPath, string dirPath, string password = "")
{
if (!File.Exists(zipPath))
{
throw new ArgumentNullException("zipPath压缩文件不存在");
}
Directory.CreateDirectory(dirPath);
try
{
using (Stream stream = File.OpenRead(zipPath))
{
var option = new ReaderOptions()
{
ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding()
{
Default = Encoding.UTF8
}
};
if (!string.IsNullOrWhiteSpace(password))
{
option.Password = password;
}
var reader = ReaderFactory.Open(stream, option);
while (reader.MoveToNextEntry())
{
if (reader.Entry.IsDirectory)
{
Directory.CreateDirectory(Path.Combine(dirPath, reader.Entry.Key));
}
else
{
//创建父级目录,防止Entry文件,解压时由于目录不存在报异常
var file = Path.Combine(dirPath, reader.Entry.Key);
Directory.CreateDirectory(Path.GetDirectoryName(file));
reader.WriteEntryToFile(file);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
总结
相似的插件还有SharpZipLib(支持更多的压缩格式)、SevenZipSharp(专注处理7z格式压缩文件)等,它们也都有各自的优缺点。但总的来说,上面的两个组件已经满足日常工作中的大部分需求,遇到相同问题的朋友可以参考下~