ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
Byte Buffer Compression with ZLib
By: W-Buffer
So you need to resolve your file size issues? Well an option
is to compress the files, but how?
We are going to use the ZLib.dll currently
provided by Alessandro Iacopetti & Gilles Vollant to compress files.
First you need to put the ZLib.dll in your \windows\system directory,
then you just declare these API calls:
This one to Copy Memory betwen Bytes Buffers:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
This to Compress a Byte Buffer:
Public Declare Function compress Lib "zlib.dll" Alias "compress2" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long, ByVal Level As Long) As Long
And this is for... Uncompress? (duh!):
Public Declare Function uncompress Lib "zlib.dll" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long) As Long
Now lets say that you have Byte Buffer, to compress it just:
Where TBuff is a temporary Byte buffer, BuffSize is the size of TBuff
(about 1.01% more then Bytes + 12 Bytes more), Bytes are the Byte Buffer
that you wanna compress, and Level is the Compression Level that you want to
use (From 0 To 9). TBuff is the Compressed Buffer, BuffSize
is the Lenght of Compressed Bytes, you just use CopyMemory to copy
compressed Buffer (TBuff) to another:
'Set the new Size of Bytes
ReDim Bytes(BuffSize - 1)
'Copy from TBuff to Bytes
CopyMemory Bytes(0), TBuff(0), BuffSize
Now to Uncompress is almost the same but you use Uncompress
API call instead and the BufferSize is the Original Size of
the Byte Buffer (plus the 1.01% and 12 Bytes), this is the code:
'Uncompress Bytes() to original size
uncompress TBuff(0), BuffSize, Bytes(0), UBound(Bytes) + 1
'Set the new size of Bytes() now uncompressed
ReDim Bytes(BuffSize - 1)
'Now Copy from TBuff() to Bytes()
CopyMemory Bytes(0), TBuff(0), BuffSize
Notes: In the code examples when you call Compress or uncompress,
the API call returns in BuffSize the Compressed or Uncompressed size
of TBuff()
Well thats all for now, soon you'll see my RLE theory, but till then
Download my ZLib source that has usefull functions like: