【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 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
刘欣的博客12 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
Yorlen_Zhang14 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝19114 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话14 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc114007566316 小时前
通信 28
c#
bugcome_com19 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中20 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
bugcome_com1 天前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
懒人咖1 天前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空