zlib compress/uncompress banks

BlitzMax Forums/BlitzMax Programming/zlib compress/uncompress banks

Ghost Dancer(Posted 2007) [#1]
Well, what I thought would be a simple job of writing a data packer is turning into a complete nightmare!

I am trying to compress/uncompress a bank. This code was posted a while ago:

Import Pub.ZLib

Function CompressBank:TBank( bank:TBank )
	Local size=bank.Size()
	Local out_size=size+size/10+32
	Local out:TBank=TBank.Create( out_size )
	compress out.Buf()+4,out_size,bank.Buf(),size
	out.PokeByte 0,size
	out.PokeByte 1,size Shr 8
	out.PokeByte 2,size Shr 16
	out.PokeByte 3,size Shr 24
	out.Resize out_size+4
	Return out
End Function

Function UncompressBank:TBank( bank:TBank )
	Local out_size
	out_size:|bank.PeekByte(0)
	out_size:|bank.PeekByte(1) Shl 8
	out_size:|bank.PeekByte(2) Shl 16
	out_size:|bank.PeekByte(3) Shl 24
	Local out:TBank=TBank.Create( out_size )
	uncompress out.Buf(),out_size,bank.Buf()+4,bank.Size()-4
	Return out
End Function


but it does not seem to compress, and the uncompress function errors. Has anyone actually got this (or something similar) working?


klepto2(Posted 2007) [#2]
Try this module, it is a Packer I have written for my personal use. It supports a tree like directory, Passwords, Zip and a simple password based encryption system. Source is available.
a comfortable Gui interface unfortunatly not.

If you have questions or you need an example post it and I will send you an email with the example.

Link: http://www.blitzforum.de/upload/file.php?id=1256


Yan(Posted 2007) [#3]
Here's some old'n'crusty code...

...and it seems to still work too. Always a bonus!


Ghost Dancer(Posted 2007) [#4]
Thank you both - I've managed to get my own simple version working:

Function compressBank:TBank(sourceBank:TBank, level = 9)
	Local destBankSize = Ceil(sourceBank.Size() * 1.001) + 12
	Local destBank:TBank = CreateBank(destBankSize)
	
	compress2(destBank.Buf(), destBankSize, sourceBank.Buf(), sourceBank.Size(), level)
	
	'resize bank to it's compressed size
	destBank.Resize(destBankSize)
	
	Return destBank
End Function


Function uncompressBank:TBank(sourceBank:TBank, destBankSize)
'destBankSize holds size of the uncompressed data saved previously
	Local destBank:TBank = CreateBank(destBankSize)
	
	uncompress(destBank.Buf(), destBankSize, sourceBank.Buf(), sourceBank.Size())
	
	Return destBank
End Function



Tachyon(Posted 2007) [#5]
Hey klepto2 - Your packer mod looks nice. Could I have an example or two to get started with (as you so kindly offered in your post above!) =D

Thank you!


klepto2(Posted 2007) [#6]
Here we go it is a converted of the tiledrop.bmx in the sample folder of Blitzmax (BlitzmaxPath/samples/birdie/games/tiledrop)

Just save it within the same folder like the original bmx file.




Tachyon(Posted 2007) [#7]
Thank you Klepto2!!


MacSven(Posted 2008) [#8]
I have modified the Compression function to save the Packed data after compressing direct to the disk. The sourcebank is a bank that i created in the mainapplication as;

global mybank:tbank=createbank(4096)



I want now that i load the compressed bank from disk and uncompress it direct to my bank. how can i realize that.

i create a new bank like:

local banknew:tbank=loadbank("bank.pak")

uncompressBank (banknew, 4095)

i setup the uncompress funtion to this:

uncompress(myBankBank.Buf(), destBankSize, sourceBank.Buf(), sourceBank.Size())

but it does not work! Can someone help me.


Hardcoal(Posted 2012) [#9]
it would be nice if it was possible to read from a pak like from a disk.

lets say you want to loadimage using xors3d command XloadImage
how can you do that with your pak reader?

if you could do XLoadImage(ReadFile(PKLPacker,"FileName$") that would be nice

any way how do i use XloadImage or XLoadMesh with your packer?


Zeke(Posted 2012) [#10]
(yeah, this is quite old topic, but...) you can use this: http://blitzbasic.com/Community/posts.php?topic=71734
or simply use xMountPackFile(...)

Last edited 2012


ImaginaryHuman(Posted 2012) [#11]
I recommend trying the LZMA module, it's faster and compresses better.