Re: How to compress with zlib
Posted: Tue Nov 12, 2013 5:25 pm
I think your right, .. or at least with this zlib. thanks.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
#ENABLE_GZIP = 16
; #ZLIB_VERSION = "1.2.8"
#Z_NULL = 0
#Z_OK = 0
#Z_STREAM_END = 1
#Z_FINISH = 4
#Z_BLOCK = 5
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
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
PB_Alignment1.b[4]
CompilerEndIf
data_type.i
adler.l
reserved.l
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
PB_Alignment2.b[8]
CompilerEndIf
EndStructure
ImportC "zlib.lib"
zlibVersion()
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.l)
deflate(*strm, flush.i)
deflateEnd(*strm)
EndImport : Global ZLIB_VERSION$ = PeekS(zlibVersion())
Procedure InflatePayload(*Payload, windowBits.i)
LengthToRead = MemorySize(*Payload)
LengthToWrite = FileSize("1test.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
#Z_STREAM_ERROR = -2
#Z_MEM_ERROR = -4
#Z_VERSION_ERROR = -6
Procedure.s deflateInitErr(Err.l)
errorMsg.s
Select Err
Case #Z_STREAM_ERROR
errorMsg = "Invalid parameter passed in to function."
Case #Z_MEM_ERROR
errorMsg = "Insufficient memory."
Case #Z_VERSION_ERROR
errorMsg = "The version of zlib.h and the version of the library linked do not match."
Default
errorMsg = "Unknown error code."
EndSelect
ProcedureReturn errorMsg
EndProcedure
#Z_ERRNO = -1
#Z_DATA_ERROR = -3
#Z_BUF_ERROR = -5
Procedure.s deflateErr(Err.l)
errorMsg.s
Select Err
Case #Z_ERRNO
errorMsg = "Error occured while reading file."
Case #Z_STREAM_ERROR
errorMsg = "The stream state was inconsistent (e.g., next_in or next_out was NULL)."
Case #Z_DATA_ERROR
errorMsg = "The deflate data was invalid or incomplete."
Case #Z_MEM_ERROR
errorMsg = "Memory could not be allocated for processing."
Case #Z_BUF_ERROR
errorMsg = "Ran out of output buffer for writing compressed bytes."
Case #Z_VERSION_ERROR
errorMsg = "The version of zlib.h and the version of the library linked do not match."
Default
errorMsg = "Unknown error code."
EndSelect
ProcedureReturn errorMsg
EndProcedure
; Debug "zLib Version: "+PeekS(zlibVersion())
Debug "zLib Version: "+ZLIB_VERSION$
#Z_DEFLATED = 8
#Z_DEFAULT_STRATEGY = 0
#MAX_MEM_LEVEL = 9
;Compression Levels
#Z_DEFAULT_COMPRESSION = -1
#Z_NO_COMPRESSION = 0
#Z_BEST_SPEED = 1
#Z_BEST_COMPRESSION = 9
#Z_NORM_COMPRESSION = 6
Procedure DeflatePayload(*Input, windowBits.i)
LengthToRead = MemorySize(*Input)
strm.Z_STREAM
strm\next_in = *Input
strm\avail_in = LengthToRead
strm\opaque = @opaque
strm\zalloc = #Z_NULL
strm\zfree = #Z_NULL
deflateStatus.l = deflateInit2_(@strm, #Z_BEST_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
; deflateStatus.l = deflateInit2_(@strm, #Z_DEFAULT_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
; deflateStatus.l = deflateInit2_(@strm, #Z_NO_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
; deflateStatus.l = deflateInit2_(@strm, #Z_NORM_COMPRESSION, #Z_DEFLATED, windowBits | #ENABLE_GZIP, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, ZLIB_VERSION$, SizeOf(Z_STREAM))
If deflateStatus <> #Z_OK : Debug "deflateInit2_(): "+ deflateInitErr(deflateStatus) : End : EndIf
LengthToWrite = deflateBound(@strm, LengthToRead)
*data_out = AllocateMemory(LengthToWrite)
If *data_out =0 : Debug "data_out Memory Allocation failed." : ProcedureReturn : EndIf
strm\next_out = *data_out
strm\avail_out = LengthToWrite
deflateStatus.l = deflate(@strm, #Z_FINISH)
If deflateStatus <> #Z_STREAM_END : Debug "deflate(): "+ deflateErr(deflateStatus) : End : EndIf
*Payload = AllocateMemory(strm\total_out)
If *Payload =0 : FreeMemory(*data_out) : Debug "Payload Memory Allocation failed." : ProcedureReturn : EndIf
CopyMemory(*data_out, *Payload, MemorySize(*Payload))
ProcedureReturn *Payload
EndProcedure
If OpenFile(0, "1test.dds")
LengthToRead = FileSize("1test.dds")
*Input = AllocateMemory(LengthToRead)
ReadData(0, *Input, LengthToRead)
CloseFile(0)
*Payload = DeflatePayload(*Input, 15)
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
Code: Select all
FileSeek(#File, length - 4)
unpsz = ReadLong(#File)
but my old code not want to unpack it. even last 4 bytes it is not unpacked size, as before i am unpack vgm files. it is becouse zlib is update to new one? where to get that new one? official cite have source, not dll it self.// .DMF files are compressed using zlib, after you decompress a DMF, you will start to read the next chunk of bytes.
Code: Select all
Inflate_Initialize(*stream, 16 | #MAX_WBITS)
Code: Select all
Inflate_Initialize(*stream, 15 | #MAX_WBITS)
NB*: DefleMask was used to test the files..DelekDefleMask.BReal Ghostbusters Intro
...