【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;
}
相关推荐
晨星shine7 小时前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户2986985301415 小时前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户36674625267416 小时前
接口文档汇总 - 2.设备状态管理
c#
用户36674625267416 小时前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530145 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools6 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的6 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#