LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

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

admin
2023年8月16日 9:34 本文热度 474
解压缩对于我们日常工作太经常用到了,毕竟它有诸多好处,比如节省空间、方便管理传输、保密作用等等。
而对于我们开发人员来说,项目开发也是经常需要用到,今天就给大家推荐用一个可以用于压缩、解压、解密,并支持多种压缩类型和格式的开源项目。

项目简介

这是一个纯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

- End -

该文章在 2023/8/16 9:39:40 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved