zlib compression
BlitzMax Forums/BlitzMax Programming/zlib compression
| ||
how exactly do you use zlib compression? as usual the docs don't give any examples or even an explanation of what it does. could some kind person give an example of how to use it? Cheers Charlie |
| ||
here you go :)Local str:String = "This is some random text to compress with zlib.~n" + .. "00000000000000000000000000000000000000000000000000000000000000000~n" +.. "11111111111111111111111111111111111111111111111111111111111111111~n" +.. "22222222222222222222222222222222222222222222222222222222222222222~n" +.. "33333333333333333333333333333333333333333333333333333333333333333~n" Local in:Byte Ptr = str.ToCString() Local out:Byte[ str.Length * 2] ' make sure zlib has enough room to work with Local outsz:Int = out.Length ' compressed size is written to this compress( out,outsz, in,str.Length) Print "~ncompressed(" + outsz +"): " + String.FromBytes(out,outsz) Local data:Byte[ out.Length] Local datasz:Int = data.Length uncompress( data,datasz, out,outsz) Print "~nuncompressed(" + datasz +"): " + String.FromCString(data) |
| ||
Another example. I've included 2 easy functions I found through the forums, and added an extra feature to automatically deal with the size of the bank destination, so you can just use compressbank/uncompressbank functions without needing to know the destination size when decompressing it: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 destBank.Resize(destBankSize+4) 'The size of the bank is included at the end - so you don't need to know the destination size later destBank.PokeInt(destBankSize,CurrentSize) 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 |
| ||
thanks, thats great! as an additional thing, how would i load and save this. i.e. load compressed data, decompress it, change it, re-compress and save? Cheers Charlie |
| ||
http://www.blitzbasic.com/Community/posts.php?topic=67985#759617 |
| ||
just do a bank.save(<url>) and bank = loadbank(<url>) |
| ||
5 years later and unashamed: Yay, Thank You! 8) |
| ||
I recommend ditching zlib and using the lzma module. Faster, better compression. |
| ||
I recommend ditching zlib and using the lzma module. Faster, better compression. Tried that.... None of the stuff I coded with it compiled in Windows and Linux, on Mac it worked, but with a lot of objections from the compiler too. |