Introduction
Some weeks ago, I discovered that Microsoft .NET 4.5 supports the Deflate version used in ZLib, and I decided to develop an implementation using only the .NET Framework.
This library has the basic Zlib functions, read and write, and supports the 3 .NET compression levels:
- Faster
- NoCompression
- Optimal
Inside the Code
My ZLib implementation has 3 classes:
- ZLibStream.cs
- ZLibHeader.cs
- Adler32.cs
ZlibStream
This class contains the I/O functions to correctly read or write the ZLib stream. Also, it has the control to determine if it's a supported ZLib stream or not.
ZLibHeader
The ZlibHeader
class has the functions for encode or decode the ZLib header.
Adler32
This class has the functions for check the hash in Adler32
. The result determines if the stream is correct or not.
How to Encode or Decode a ZLib Header
If you wish to encode a new ZLib header and get the correct 2 first bytes, you need to create a new instance of ZLibHeader
. Then, you can set the properties as you want and call the function EncodeZlibHeader
. This function will return the 2 bytes of the header.
private void GetZLibHeader()
{
byte[] bytesHeader;
ZLibHeader header = new ZLibHeader();
header.CompressionMethod = 8;
header.CompressionInfo = 7;
header.FDict = false;
switch (this.mCompressionLevel)
{
case CompressionLevel.NoCompression:
{
header.FLevel = FLevel.Faster;
break;
}
case CompressionLevel.Fastest:
{
header.FLevel = FLevel.Default;
break;
}
case CompressionLevel.Optimal:
{
header.FLevel = FLevel.Optimal;
break;
}
}
bytesHeader = header.EncodeZlibHeader();
.
.
.
}
To decode is more easy, ZLibHeader
has a static
property that receives the 2 bytes of the header. The function will return an instance of ZLibHeader
with all header information.
Using the Code for Compress or Decompress
The use of this library is very similar to using GZipStream
. The only thing you need is to create a new instance of ZLIBStream
, and read or write the wished stream.
Using System.IO.Compression;
Using ZLib;
.
.
.
OpenFileDialog dlgOpen = new OpenFileDialog();
CompressionLevel level = CompressionLevel.Optimal;
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
using (FileStream fsSource = new FileStream(dlgOpen.FileName, FileMode.Open, FileAccess.Read))
{
using (FileStream fsTarget = new FileStream(dlgOpen.FileName + ".zlib",
FileMode.Create, FileAccess.Write))
{
using (ZLIBStream zs = new ZLIBStream(fsTarget, level, true))
{
int bytesLeidos = 0;
byte[] buffer = new byte[1024];
while ((bytesLeidos = fsSource.Read(buffer, 0, buffer.Length)) > 0)
{
zs.Write(buffer, 0,bytesLeidos);
}
}
}
}
}
Using System.IO.Compression;
Using ZLib;
.
.
.
OpenFileDialog dlgOpen = new OpenFileDialog();
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
using (FileStream fsSource = new FileStream(dlgOpen.FileName, FileMode.Open, FileAccess.Read))
{
using (ZLIBStream zs = new ZLIBStream(fsSource, CompressionMode.Decompress, true))
{
using (FileStream fsTarget = new FileStream(dlgOpen.FileName + ".txt",
FileMode.Create, FileAccess.Write))
{
int bytesLeidos = 0;
byte[] buffer = new byte[1024];
while ((bytesLeidos = zs.Read(buffer, 0, buffer.Length)) > 0)
{
fsTarget.Write(buffer, 0, bytesLeidos);
}
}
}
}
}
History
- 17/02/2018: Improved some functions with bitwise operations
- 16/02/2015: Initial release