推荐一个C#开发的、跨平台的解压缩的开源项目,值得收藏

简介: 一个纯C#压缩库,用于.NET Standard 2.0、2.1、.NET Core 3.1和.NET 5.0

解压缩对于我们日常工作太经常用到了,毕竟它有诸多好处,比如节省空间、方便管理传输、保密作用等等。

而对于我们开发人员来说,项目开发也是经常需要用到,今天就给大家推荐用一个可以用于压缩、解压、解密,并支持多种压缩类型和格式的开源项目。

项目简介

这是一个纯C#压缩库,用于.NET Standard 2.0、2.1、.NET Core 3.1和.NET 5.0,支持格式有zip/tar/bzip2/gzip/lzip,功能实现有解压缩rar, 解压缩7zip, 解压缩zip, 解压缩tar解压缩bzip2, 解压缩gzip, 解压缩lzip。

项目结构

图片

使用方法

**压缩核心代码
**

protected void Write(CompressionType compressionType, string archive, string archiveToVerifyAgainst, Encoding encoding = null)
        {
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                WriterOptions writerOptions = new WriterOptions(compressionType)
                {
                    LeaveStreamOpen = true,
                };

                writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;

using (var writer = WriterFactory.Open(stream, type, writerOptions))
                {
                    writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
                }
            }
            CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
               Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));

using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                ReaderOptions readerOptions = new ReaderOptions();

                readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;

using (var reader = ReaderFactory.Open(NonDisposingStream.Create(stream), readerOptions))
                {
                    reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
                    {
                        ExtractFullPath = true
                    });
                }
            }
            VerifyFiles();
        }

解压核心代码

public abstract class ReaderTests : TestBase
    {
protected void Read(string testArchive, CompressionType expectedCompression, ReaderOptions options = null)
        {
            testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);

            options = options ?? new ReaderOptions();

            options.LeaveStreamOpen = true;
            ReadImpl(testArchive, expectedCompression, options);

            options.LeaveStreamOpen = false;
            ReadImpl(testArchive, expectedCompression, options);
            VerifyFiles();
        }

private void ReadImpl(string testArchive, CompressionType expectedCompression, ReaderOptions options)
        {
using (var file = File.OpenRead(testArchive))
            {
using (var protectedStream = NonDisposingStream.Create(new ForwardOnlyStream(file), throwOnDispose: true))
                {
using (var testStream = new TestStream(protectedStream))
                    {
using (var reader = ReaderFactory.Open(testStream, options))
                        {
                            UseReader(reader, expectedCompression);
                            protectedStream.ThrowOnDispose = false;
                            Assert.False(testStream.IsDisposed, "{nameof(testStream)} prematurely closed");
                        }

// Boolean XOR -- If the stream should be left open (true), then the stream should not be diposed (false)
// and if the stream should be closed (false), then the stream should be disposed (true)
var message = $"{nameof(options.LeaveStreamOpen)} is set to '{options.LeaveStreamOpen}', so {nameof(testStream.IsDisposed)} should be set to '{!testStream.IsDisposed}', but is set to {testStream.IsDisposed}";
                        Assert.True(options.LeaveStreamOpen != testStream.IsDisposed, message);
                    }
                }
            }
        }

public void UseReader(IReader reader, CompressionType expectedCompression)
        {
while (reader.MoveToNextEntry())
            {
if (!reader.Entry.IsDirectory)
                {
                    Assert.Equal(expectedCompression, reader.Entry.CompressionType);
                    reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
                    {
                        ExtractFullPath = true,
                        Overwrite = true
                    });
                }
            }
        }
    }

zip压缩用法

Write(CompressionType.None, "Zip.none.noEmptyDirs.zip", "Zip.none.noEmptyDirs.zip", Encoding.UTF8);

zip解压用法

Read("Zip.zipx", CompressionType.LZMA);

项目还未支持

  • RAR 5 解密
  • 7Zip 写入
  • Zip64 解压缩
  • 多卷Zip支持
项目地址: https://github.com/adamhathcock/sharpcompress

专注分享编程知识、热门有用有趣的开源项目

相关文章
|
3月前
|
XML 测试技术 API
利用C#开发ONVIF客户端和集成RTSP播放功能
利用C#开发ONVIF客户端和集成RTSP播放功能
1530 123
|
C# Android开发 虚拟化
C# 一分钟浅谈:MAUI 跨平台移动应用开发
.NET MAUI 是 Microsoft 推出的跨平台框架,支持 Windows、macOS、iOS 和 Android。本文从基础概念入手,探讨 MAUI 的常见问题、易错点及解决方案,并通过代码示例详细说明。涵盖平台特定代码、XAML 语法、数据绑定、性能优化和调试技巧等内容,帮助开发者更好地掌握 .NET MAUI。
1197 55
|
10月前
|
物联网 数据处理 C#
C#实现上位机开发,串口通信,读写串口数据并处理16进制数据
C#实现上位机开发,串口通信,读写串口数据并处理16进制数据。在自动化、物联网以及工业控制行业中,上位机开发是一项重要的技能。本教程主要介绍使用C#进行上位机开发,重点在于串口通信和数据处理。
1982 82
|
11月前
|
C# 开发工具 C++
code runner 运行C#项目
本文介绍了如何修改Code Runner设置使 Visual Studio Code (VS Code) 能直接运行完整的 C# 项目。传统方式依赖 cscript 工具,仅支持 .csx 文件,功能受限且已停止维护。新配置使用 `dotnet run` 命令,结合一系列炫酷的cmd指令,将指令定位到具体的csproj文件上进行运行。
588 38
|
8月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
327 0
|
12月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
407 12
|
12月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
327 12
|
3月前
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
232 19
|
4月前
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
668 0
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
275 3