【问题标题】:C# Zlib usage in winform applicationC# Zlib 在 winform 应用程序中的使用
【发布时间】:2011-04-14 20:58:11
【问题描述】:

我希望更改我的程序解压缩使用 zlib 的文件的方式。目前我正在使用 offzip & .bat(batch program) 在点击事件时解压文件。

这是我在批处理程序中的内容(love command promt 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# winforms zlib


    【解决方案1】:

    您需要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 已关闭。旧档案仍然是available at Codeplex。看起来代码已经迁移到Github了:


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2020-10-12
      • 1970-01-01
      • 2014-02-18
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多