【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;
}
相关推荐
玖玥拾2 小时前
Unity 3D 笔记(十二)Unity/C# Socket 网络笔记1
网络·unity·c#
EIP低代码平台3 小时前
EIP低代码平台 - 系统日志
低代码·c#·权限·工作流·netcore
玖玥拾3 小时前
Unity 3D 笔记(十五)Unity/C# Socket 网络笔记4
服务器·网络·unity·c#
EIP低代码平台11 小时前
EIP 低代码平台 - 角色维护
低代码·c#·权限·工作流·netcore
心平气和量大福大16 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
-银雾鸢尾-20 小时前
C#中HashTable相关方法
开发语言·c#
geovindu1 天前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
吴可可1231 天前
C# CAD二次开发中如何退出窗口
c#
影寂ldy1 天前
C# WinForm 三种自定义控件 + 完全自定义重绘控件知识点
开发语言·c#
ellis19701 天前
C#异常相关关键字:Exceptions,throw,try,catch,finally
unity·c#