下面会提供两种解压的方案,代码仅供参考!!!
第一种方法:可以解压多种格式的压缩包;
例如:rar, unrar, zip, unzip, bzip2, gzip, tar, 7zip, lzip, xz
准备工作:装一个叫SharpCompress的Nuget包
/// <summary>
/// 解压压缩包
/// </summary>
/// <param name="ZipPackagePath">压缩包路径</param>
/// <param name="ForPath">解压到哪里</param>
public static void ExtractTheFile(string ZipPackagePath,string ForPath)
{
//设置解压文件名称格式,防止解压后的文件名称出现乱码情况
SharpCompress.Readers.ReaderOptions options = new SharpCompress.Readers.ReaderOptions();
options.ArchiveEncoding.Default = Encoding.GetEncoding("utf-8");
IArchive Archive = ArchiveFactory.Open(ZipPackagePath, options);
foreach (IArchiveEntry ArchiveEntry in Archive.Entries)
{
if (!ArchiveEntry.IsDirectory)
{
ArchiveEntry.WriteToDirectory(ForPath, new ExtractionOptions { ExtractFullPath = true, Overwrite = false });
}
}
}
第二种方法:
准备工作:装一个叫System.IO.Compression.ZipFile的Nuget包
/// <summary>
/// 解压压缩包
/// </summary>
/// <param name="ZipPackagePath">压缩包路径</param>
/// <param name="ForPath">解压到哪里</param>
public static void NetExtractTheFile(string ZipPackagePath, string ForPath)
{
ZipFile.ExtractToDirectory(ZipPackagePath, ForPath); //解压
}
随堂笔记,侵权请联系作者删除!!