Is there an easy way to deflate a ZLIB to a PNG with only c#? [duplicate]

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.


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:

  • https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
  • https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/