'******************************************************************************* ' MODULE: modZLib ' FILENAME: modZLib.bas ' AUTHOR: Jared De Blander ' CREATED: May 18, 2006 ' COPYRIGHT: Copyright (c) 2006 Jared De Blander. All Rights Reserved. ' WEBSITE: http://www.jx90.com/ ' E-MAIL: jared@deblander.org ' 'LICENSE: ' This library is free software; you can redistribute it and/or ' modify it under the terms of the GNU Lesser General Public ' License as published by the Free Software Foundation; either ' version 2.1 of the License, or (at your option) any later version. ' ' This library is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ' Lesser General Public License for more details. ' ' You should have received a copy of the GNU Lesser General Public ' License along with this library; if not, write to the Free Software ' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ' ' DESCRIPTION: ' Compress/Extract files using zlib. Requires zlib.dll version 1.1.4! ' ' MODIFICATION HISTORY: ' 5/18/2006 ' 1. Initial release. ' '******************************************************************************* Private Declare Function Compress Lib "zlib.dll" Alias "compress" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long) As Long Private Declare Function Extract Lib "zlib.dll" Alias "uncompress" (Dest As Any, destLen As Any, Src As Any, ByVal srcLen As Long) As Long Public Enum mzErrors Z_OK = 0 Z_STREAM_ERROR = -2 'Bad compression level Z_DATA_ERROR = -3 'Corrupted data Z_MEM_ERROR = -4 'Insufficient memory Z_BUF_ERROR = -5 'Insufficient space in output buffer Z_INVALID_FILE = 1 'File not created by us Z_UNHANDLED = 2 'Error not handled End Enum Public Function mzCompressFile(theFileIn As String, theFileOut As String) As Long Dim FFIO As Long Dim inSize As Long Dim outSize As Long Dim inBuffer() As Byte Dim outBuffer() As Byte Dim cResult As Long On Error GoTo exitCompress 'Default error mzCompressFile = mzErrors.Z_UNHANDLED 'Allocate space for input data inSize = FileLen(theFileIn) ReDim inBuffer(0 To inSize - 1) 'Read input data FFIO = FreeFile Open theFileIn For Binary As #FFIO Get #FFIO, , inBuffer Close #FFIO 'Allocate space for output data. 'Requires 1% larger than the uncompressed data plus 12 bytes outSize = 1.01 * inSize + 12 ReDim outBuffer(0 To outSize - 1) 'Compress data and save result cResult = Compress(outBuffer(0), outSize, inBuffer(0), inSize) If cResult = mzErrors.Z_OK Then 'resize output buffer to correct size ReDim Preserve outBuffer(0 To outSize - 1) FFIO = FreeFile 'Kill any existing files On Error Resume Next Kill theFileOut If Err Then Err.Clear On Error GoTo exitCompress Open theFileOut For Binary As #FFIO Put #FFIO, , inSize Put #FFIO, , outSize Put #FFIO, , outBuffer Close #FFIO mzCompressFile = Z_OK Else mzCompressFile = cResult End If exitCompress: End Function Public Function mzExtractFile(theFileIn As String, theFileOut As String) As Long Dim FFIO As Long Dim inSize As Long Dim outSize As Long Dim inBuffer() As Byte Dim outBuffer() As Byte Dim cResult As Long On Error GoTo exitExtract 'Default error mzExtractFile = mzErrors.Z_UNHANDLED 'Read input data FFIO = FreeFile Open theFileIn For Binary As #FFIO 'Read uncompressed size Get #FFIO, , outSize 'Allocate space for output data ReDim outBuffer(0 To outSize - 1) 'read compressed size Get #FFIO, , inSize 'Allocate space for input data ReDim inBuffer(0 To inSize - 1) 'read compressed data Get #FFIO, , inBuffer Close #FFIO cResult = Extract(outBuffer(0), outSize, inBuffer(0), inSize) If cResult = mzErrors.Z_OK Then 'Kill any existing files On Error Resume Next Kill theFileOut If Err Then Err.Clear On Error GoTo exitExtract 'Write original file Open theFileOut For Binary As #FFIO Put #FFIO, , outBuffer Close #FFIO mzExtractFile = Z_OK Else mzExtractFile = cResult End If exitExtract: End Function