*******************************************************
* zLib.prg
* Author: William GC Steinford
* Date: June 20, 2002
* Purpose: Easy to use Compress/Uncompress utilities
* for VFP
*******************************************************
PROCEDURE zLib
PARAMETER cFunc, cStr
DO CASE
CASE upper(cFunc)='COMPRESS'
RETURN CompressIt(cStr)
CASE upper(cFunc)='UNCOMPRESS'
RETURN UnCompressIt(cStr)
ENDCASE
RETURN '
* Functions:
*!* int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
*!* int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
Function CompressIt( InFile )
DECLARE INTEGER compress IN zlib.dll AS zlibCompress ;
STRING @ dest, INTEGER @ destLen, ;
STRING src, INTEGER srcLen
* Compresses the source buffer into the destination buffer.
* sourceLen is the byte length of the source buffer. Upon entry,
* destLen is the total size of the destination buffer, which must
* be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit,
* destLen is the actual size of the compressed buffer.
LOCAL lnSize, lcBuff, lnFinalSize
lnSize = len(InFile)
*123,456,789,012,345 15 chars is enough for 100 Terabytes.
*100,000,000,000,000
lcBuff = space( len(InFile)*1.2 )
lnFinalSize = len(lcBuff)
Res = zlibCompress( @lcBuff, @lnFinalSize, InFile, lnSize )
If Res=0 && Success
RETURN PadL( alltrim(str(lnSize)), 15, '0' ) + Left( lcBuff, lnFinalSize )
endif
RETURN '
****************************************************************************
* Proc UnCompressIt
* The first 15 chars MUST be the (decimal) size of the uncompressed file
FUNCTION UnCompressIt( zLibFile )
DECLARE INTEGER uncompress IN zlib.dll AS zlibUnCompress ;
STRING @ dest, INTEGER @ destLen, ;
STRING src, INTEGER srcLen
* Decompresses the source buffer into the destination buffer.
* sourceLen is the byte length of the source buffer. Upon entry,
* destLen is the total size of the destination buffer, which must
* be large enough to hold the entire uncompressed data.
* (The size of the uncompressed data must have been saved previously
* by the compressor and transmitted to the decompressor by some mechanism
* outside the scope of this compression library.)
* Upon exit, destLen is the actual size of the compressed buffer.
* This function can be used to decompress a whole file at once if the input file is mmap'ed.
*
LOCAL lnSize, lcBuff, lnFinalSize
lnSize = len(zLibFile)
*123,456,789,012,345 15 chars is enough for 100 Terabytes.
*100,000,000,000,000
lnFinalSize = Val( Left( zLibFile, 15 ) )
lcBuff = space( lnFinalSize )
zLibFile = SubStr( zLibFile, 16 )
Res = zlibUnCompress( @lcBuff, @lnFinalSize, zLibFile, lnSize )
If Res=0 && Success
RETURN lcBuff
endif
RETURN '
********************************************************