WinForm应用程序中C#Zlib的用法

问题描述

| 我正在寻找改变程序解压缩使用zlib的文件的方式。目前,我正在使用offzip和.bat(批处理程序)在点击事件时解压缩文件。 这是批处理程序中的内容(love命令promt XD)
@ECHO Off

cd %0\\..\\

start  %~dp0offzip.exe -a -1 *.bsg %~dp0 0
但这对我来说使用批处理文件显得有些俗气。 所以这是问题。我将如何处理以下内容。 打开一个文件,解压缩 重新压缩 不知道这是否有帮助,但是这是我当前使用offzip解压缩和重新压缩文件的选项。
Decompress:  offzip.exe -a -1 <file> <path> 0
-a decompresses whole file
-1 keeps decompressed file in one piece
0 starts at the offset 0x0

Recompress: packzip.exe -w -15 <input file> <output>
-w    winbits
-15   winbits equal -15
如果您能给我一个例子或对您有所帮助的话。我也在寻找一个免费的易于使用的库,该库支持zlib。我已经看过zlib.net,并且可能会喜欢它,但是只是想尽自己最大的努力来做我的作业。 在此先感谢您的帮助。     

解决方法

您需要DotNetZip。价格是正确的(很难被击败)。性能似乎至少与gzip / zip及其兄弟产品一样好。 用法很简单。这是解压缩zlib压缩文件的方法:
using System.IO  ;
using Ionic.Zlib ;

namespace ZlibExample
{
    class Program
    {
        static void Main( string[] args )
        {
            using ( Stream     compressed = File.OpenRead( @\"c:\\foobar.zlib\" ) )
            using ( ZlibStream zlib       = new ZlibStream( compressed,CompressionMode.Decompress ) )
            {
                byte[] buf  = new byte[short.MaxValue] ;
                int    bufl ;
                while ( 0 != (bufl=zlib.Read(buf,buf.Length) ) )
                {
                   DoSomethingWithDecompressedData( buf,bufl ) ;
                }
            }
            return ;
        }
    }

}
压缩同样简单:
using ( Stream     compressed = File.OpenWrite( @\"c:\\foobar.zlib\" ) )
using ( ZlibStream zlib       = new ZlibStream( compressed,CompressionMode.Compress ) )
{
  byte[] buf ;
  while ( null != (buf=ReadSomeDataToCompress()) )
  {
    zlib.Write(buf,buf.Length) ;
  }
}
或者,如果您知道一定数量的数据,则可以使用静态方法
byte[] data = File.ReadAllBytes(@\"c:\\foobar.uncompressed.data\") ;
ZlibStream.CompressBuffer(data) ;
同样,构建zip文件也不难。它还支持gzip样式的压缩。 编辑要注意:DotNetZip曾经居住在Codeplex。 Codeplex已关闭。旧的存档仍可从Codeplex获得。看起来代码已迁移到Github: https://github.com/DinoChiesa/DotNetZip。看起来是原始作者的回购。 https://github.com/haf/DotNetZip.Semverd。这似乎是当前维护的版本。它也通过Nuget打包在https://www.nuget.org/packages/DotNetZip/     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...