Tweet 2d blitzmax zlib command
(Posted 2 years ago) markcwm

compress2( dest:Byte Ptr,dest_len Var,source:Byte Ptr,source_len,level )




Compress a block of data at specified compression level.


Miscellaneous / Pub.ZLib

(Posted 2 years ago) markcwm commented:

This example by Retimer, see topic

Local Bank:TBank = CreateBank() 
Local BStream:TStream = CreateBankStream(Bank)

BStream.WriteString("a bunch of data!")

Bank = compressBank(Bank) 'And your bank is now compressed

'This next line is for testing purposes
'to relink the stream to the compressed bank,
'so we can print out the compressed data
BStream = CreateBankStream(Bank)
Print "~n~nCompressed: " + BStream.ReadString(BStream.size())

'Now lets uncompress that data, and print it out
Bank = uncompressBank(Bank)
BStream = CreateBankStream(Bank)
Print "~n~nUncompressed: " + BStream.ReadString(BStream.size())

Function compressBank:TBank(sourceBank:TBank, level:Int = 9) 
    Local CurrentSize:Int = SourceBank.Size()
    Local destBankSize:Int = Ceil(sourceBank.Size() * 1.001) + 2 + 4
    Local destBank:TBank = CreateBank(destBankSize)
    compress2(destBank.Buf(), destBankSize, sourceBank.Buf(), sourceBank.Size(), level)
    'resize bank to it's compressed size
    'The size of the bank is included at the end - so you don't need to know the destination size later
    Return destBank
End Function

Function uncompressBank:TBank(sourceBank:TBank) 
    Local destBankSize:Int = sourceBank.PeekInt(Sourcebank.size()-4)
    Local destBank:TBank = CreateBank(destBankSize)
    uncompress(destBank.Buf(), destBankSize, sourceBank.Buf(), sourceBank.Size())
    Return destBank
End Function

Reply To Topic (minimum 10 characters)

Please log in to reply