c# Zlib在winform应用程序中的使用

本文关键字:应用程序 Zlib winform | 更新日期: 2023-09-27 17:50:09

我希望改变我的程序如何解压缩使用zlib的文件。目前我使用的是offzip &.bat(批处理程序)以在单击event时解压缩文件。

这是我在批处理程序(爱命令提示XD)中的内容

@ECHO Off
cd %0'..'
start  %~dp0offzip.exe -a -1 *.bsg %~dp0 0

但是对我来说,使用批处理文件看起来很俗气。

问题来了。我该怎么做呢

  1. 打开文件,解压缩
  2. 将它

不知道这是否有帮助,但这里是我目前使用的选项,现在使用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,可能会用它,但我只是想做我的功课,最好的东西在那里。

事先感谢您对此事的帮助。

c# Zlib在winform应用程序中的使用

需要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,0,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,0,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。看起来是原作者的repo。
  • https://github.com/haf/DotNetZip.Semverd。这看起来是当前维护的版本。它也可以通过Nuget在https://www.nuget.org/packages/DotNetZip/
  • 获得。