- the current example tries to inflate / deflate all at once, but this may not be the best solution for your requirements
- as I mentioned in a previous post - if your file is not text based you would have to modify the code a bit
-- I suggest comparing the two examples to see how they work with the different formats
- notice that I set the compressed size with: LengthToWrite = deflateBound(@strm, LengthToRead) / 2
-- not sure why delatebound is not optimizing compression - thus the reason for "dividing by two" (modify to whatever works)
Code: Select all
#ENABLE_GZIP = 16
#Z_NULL = 0
#ZLIB_VERSION = "1.2.8"
#Z_FINISH = 4
#Z_DEFAULT_COMPRESSION = -1
#Z_DEFLATED = 8
#MAX_MEM_LEVEL = 9
#Z_DEFAULT_STRATEGY = 0
Structure Z_STREAM
*next_in.Byte
avail_in.l
total_in.l
*next_out.Byte
avail_out.l
total_out.l
*msg.Byte
*state
zalloc.l
zfree.l
opaque.l
data_type.i
adler.l
reserved.l
EndStructure
ImportC "zlib.lib"
inflateInit2_(*strm, windowBits.i, Version.s, strm_size)
inflate(*strm, flush.i)
inflateEnd(*strm)
deflateInit2_(*strm, level.i, method.i, windowBits.i, memlevel.i, strategy.i, Version.s, strm_size)
deflateBound(*strm, sourceLen)
deflate(*strm, flush.i)
deflateEnd(*strm)
EndImport
Procedure InflatePayload(*Payload, windowBits.i)
LengthToRead = MemorySize(*Payload)
LengthToWrite = FileSize("test.dds")
*Output = AllocateMemory(LengthToWrite)
strm.Z_STREAM
strm\next_in = *Payload
strm\avail_in = LengthToRead
strm\next_out = *Output
strm\avail_out = LengthToWrite
strm\zalloc = #Z_NULL
strm\zfree = #Z_NULL
strm\opaque = #Z_NULL
inflateInit2_(@strm, windowBits, #ZLIB_VERSION, SizeOf(Z_STREAM))
inflate(@strm, #Z_FINISH)
inflateEnd(@strm)
ProcedureReturn *Output
EndProcedure
Procedure DeflatePayload(*Input, windowBits.i)
LengthToRead = MemorySize(*Input)
strm.Z_STREAM
strm\next_in = *Input
strm\avail_in = LengthToRead
strm\zalloc = #Z_NULL
strm\zfree = #Z_NULL
strm\opaque = #Z_NULL
deflateInit2_(@strm, #Z_DEFAULT_COMPRESSION, #Z_DEFLATED, windowBits, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, #ZLIB_VERSION, SizeOf(Z_STREAM))
LengthToWrite = deflateBound(@strm, LengthToRead) / 2
*Payload = AllocateMemory(LengthToWrite)
strm\next_out = *Payload
strm\avail_out = LengthToWrite
deflate(@strm, #Z_FINISH)
deflateEnd(@strm)
ProcedureReturn *Payload
EndProcedure
If OpenFile(0, "test.dds")
LengthToRead = FileSize("test.dds")
*Input = AllocateMemory(LengthToRead)
ReadData(0, *Input, LengthToRead)
CloseFile(0)
*Payload = DeflatePayload(*Input, 15 | #ENABLE_GZIP)
If CreateFile(0, "test.gz")
WriteData(0, *Payload, MemorySize(*Payload))
CloseFile(0)
EndIf
FreeMemory(*Payload)
EndIf
If OpenFile(0, "test.gz")
LengthToRead = FileSize("test.gz")
*Payload = AllocateMemory(LengthToRead)
ReadData(0, *Payload, LengthToRead)
CloseFile(0)
*Output = InflatePayload(*Payload, 15 | #ENABLE_GZIP)
If CreateFile(0, "test2.dds")
WriteData(0, *Output, MemorySize(*Output))
CloseFile(0)
EndIf
FreeMemory(*Output)
FreeMemory(*Payload)
EndIf