【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;
}
相关推荐
ajassi200013 小时前
开源 C# 快速开发(十六)数据库--sqlserver增删改查
windows·开源·c#
大飞pkz16 小时前
【设计模式】观察者模式
开发语言·观察者模式·设计模式·c#
唐青枫16 小时前
深入掌握 FluentMigrator:C#.NET 数据库迁移框架详解
c#·.net
李宥小哥17 小时前
C#基础08-面向对象
开发语言·c#
李宥小哥17 小时前
C#基础07-类与对象
服务器·数据库·c#
包达叔19 小时前
仿NewLife的XmlConfig类实现Json配置文件
c#·json·newlife
大飞pkz21 小时前
【设计模式】解释器模式
开发语言·设计模式·c#·解释器模式
敲敲敲-敲代码1 天前
web系统(asp.net和C#)
前端·c#·asp.net
__XYZ1 天前
Vala编程语言高级特性-弱引用和所有权
c语言·开发语言·后端·c#
大飞pkz1 天前
【设计模式】责任链模式
开发语言·设计模式·c#·责任链模式