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

发布于 2024-11-01 01:53:13 字数 770 浏览 2 评论 0原文

我正在寻找更改我的程序解压缩使用 zlib 的文件的方式。目前我正在使用 offzip & .bat(批处理程序)在单击事件时解压缩文件。

这是我在批处理程序中的内容(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,并且可能会接受它,但只是尝试做一些最好的事情。

预先感谢您对此事的任何帮助。

I'm looking to change how my program decompresses file that use zlib. At the moment I'm using offzip & .bat(batch program) to decompress the file upon click event.

Here's what I have in the batch program(love command promt XD)

@ECHO Off

cd %0\..\

start  %~dp0offzip.exe -a -1 *.bsg %~dp0 0

But that just looks tacky to me using a batch file.

So here's the question. How would I go about the following.

  1. Open up a file, decompress it
  2. Recompress it

Don't know if this helps or not, but here are the option I currently use to decompress and recompress the file using offzip now.

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

If you could give me an example or something that would help as well. I'm also looking for an free easy to use library that supports zlib. I've seen zlib.net, and will probably go with that, but just tring to do my homework on the best thing out there.

Thanks in advance for any help in the matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2024-11-08 01:53:13

您需要 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:


You need DotNetZip. The price is right (hard to beat free). Performance seems to be least as good as gzip/zip and their brethren.

Usage is simple. Here's how to decompress a zlib-compressed file:

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 ;
        }
    }

}

Compression is just as easy:

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) ;
  }
}

Or, if you've got a know quantity of data, you can use a static method

byte[] data = File.ReadAllBytes(@"c:\foobar.uncompressed.data") ;
ZlibStream.CompressBuffer(data) ;

Building zip files isn't much more difficult, either. It also supports gzip-style compression.


Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still available at Codeplex. It looks like the code has migrated to Github:


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文