【C#】基于SharpCompress实现压缩包解压功能

1.SharpCompress安装

在vs的nuget下搜索安装SharpCompress,如图所示

2.解压缩包功能实现

csharp 复制代码
/// <summary>
/// 解压压缩包
/// </summary>
/// <param name="filePath">压缩包文件路径</param>
/// <param name="directoryPath">解压路径</param>
public static bool UnTar(string filePath,string directoryPath)
{
    bool bRet = false;
    try
    {
        string folderName = "";
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }
        ReaderOptions options = new ReaderOptions();
        //解决中文乱码问题
        options.ArchiveEncoding.Default = Encoding.GetEncoding("utf-8");
        ExtractionOptions extract = new ExtractionOptions { ExtractFullPath = true, Overwrite = true };

        using (Stream stream = File.OpenRead(filePath))
        {
            var reader = ReaderFactory.Open(stream);
            while (reader.MoveToNextEntry())
            {
                if (!reader.Entry.IsDirectory)
                {
                    if (!string.IsNullOrEmpty(reader.Entry.Key) && reader.Entry.Size == 0 && (reader.Entry.Key.EndsWith("/") || reader.Entry.Key.EndsWith("\\")))
                    {
                        //过滤文件夹
                        continue;
                    }
                    reader.WriteEntryToDirectory(directoryPath, extract);
                }
            }
            bRet = true;
        }
    }
    catch (Exception ex)
    {
    }
    return bRet;
}
相关推荐
谷宇.9 小时前
【Unity3D实例-功能-拔枪】角色拔枪(三)IK的使用-紧握武器
游戏·unity·c#·unity3d·游戏开发·游戏编程·steam
用户83562907805110 小时前
C# 从 PDF 提取图片教程
后端·c#
格林威12 小时前
Baumer高防护相机如何通过YoloV8深度学习模型实现网球运动员和网球速度的检测分析(C#代码UI界面版)
人工智能·深度学习·数码相机·yolo·ui·c#·视觉检测
hixiong12313 小时前
用OpencvSharp编写视频录制工具
opencv·c#·音视频
张飞洪14 小时前
C# 13 与 .NET 9 跨平台开发实战:基于.NET 9 与 EF Core 9 的现代网站与服务开发
开发语言·c#·.net
hixiong12316 小时前
C# OpencvSharp获取Astra Pro奥比中光深度相机深度图
数码相机·opencv·计算机视觉·c#
ggtc17 小时前
为workflow-core扩展外抛事件
c#·webapi·workflow-core
咕白m62517 小时前
C# 使用 Spire.Doc 实现企业级 Word 文档打印方案
c#
此wei浩亦18 小时前
WPF中使用 using prism.region 报错
c#·wpf·prism