参考学习网址:
https://www.cnblogs.com/Joetao/articles/7089472.html
选择方案:
使用SharpCompress框架
1.下载SharpCompress.dll并引用
2.编写代码:
using SharpCompress.Reader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestUnzip
{
class Program
{
static void Main(string[] args)
{
using (Stream stream = File.OpenRead(@"F:\TestUnZip\testUnzip.rar"))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Console.WriteLine(reader.Entry.FilePath);
reader.WriteEntryToDirectory(@"F:\TestUnZip");
}
}
}
}
}
}
测试:
image.png
补充:
.net 4.5以后集成到框架内的内容
/// <summary>
/// 解压缩文件到指定目录,将在指定目录下解压出一个压缩文件名字的最终的目录
/// </summary>
/// <param name="ZipPath">ZIP文件路径</param>
/// <param name="ExtractPath">要解压缩的目录</param>
private void UnZipFile(string ZipPath,string ExtractPath)
{
//string NewFile = @"c:\users\exampleuser\NewFile.txt";
if (System.IO.File.Exists(ZipPath))
{
using (ZipArchive Archive = ZipFile.Open(ZipPath, ZipArchiveMode.Update))
{
//Archive.CreateEntryFromFile(NewFile, "NewEntry.txt");
//如果目录下面有文件,将解压缩失败,所以之前先备份目录
Archive.ExtractToDirectory(ExtractPath);
}
}
}
网友评论