Ooops
November 28, 2020, 02:13:59 AM

Author Topic: [bb] gzip files and banks by Klaas [ 1+ years ago ]  (Read 444 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] gzip files and banks by Klaas [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : gzip files and banks
Author : Klaas
Posted : 1+ years ago

Description : You can get the DLL from here: <a href="http://www.winimage.com/zLibDll/" target="_blank">http://www.winimage.com/zLibDll/[/url]

There is a lot more in this dll ... but this is a good start   i think

The adler stuff is a pretty fast CRC Checksum .. the other functions are pretty selfexplaining.

[decl]
.lib "zlib.dll"

zip_open%(path$,mode$): "gzopen"
zip_close%(file%):"gzclose"
zip_write%(file%,buffer*,len%):"gzwrite"
zip_compress%(dest*,destLen*,source*,sourceLen%): "compress"
zip_uncompress%(dest*, destLen*, source*,sourceLen%):"uncompress"
zip_version$():"zlibVersion"
zip_adler32%(adler%,buffer*,length%):"adler32"
zip_crc32%(adler%,buffer*,length%):"CRC32"
[/decl]


Code :
Code: BlitzBasic
  1. If Not FileType("zlib.dll")=1 Then RuntimeError("zlib.dll missing")
  2.  
  3. Function gZipFile$(org_file$,destination$="")
  4.         source_bank=CreateBank(FileSize(org_file$))
  5.         source_len=BankSize(source_bank)
  6.         f=OpenFile(org_file$)
  7.         If Not f
  8.                 Return "-ERROR cannot open source file"
  9.         EndIf
  10.         ReadBytes source_bank,f,0,BankSize(source_bank)
  11.         CloseFile f
  12.        
  13.         new_file$=org_file$+".gz"
  14.        
  15.         file = zip_open(new_file$, "wb")
  16.         If (file = 0)
  17.                 Return("-ERROR cannot open destination file")
  18.         EndIf
  19.        
  20.         suc = zip_write(file,source_bank,source_len)
  21.         If Not suc
  22.                 Return("-ERROR cannot compress file")
  23.         EndIf
  24.         file = zip_close(file)
  25.         Return FileSize(new_file$)
  26. End Function
  27.  
  28. Function gzip_adler32file(file$)
  29.         f = ReadFile(file)
  30.         If Not f Then Return False
  31.        
  32.         bufferSize = 4096
  33.         buffer = CreateBank(bufferSize)
  34.         adler = zip_adler32(0,buffer,0)
  35.         While Not Eof(f)
  36.                 length = ReadBytes(buffer,f,0,bufferSize)
  37.                 adler = zip_adler32(adler,buffer,length)
  38.         Wend
  39.         CloseFile(f)
  40.         Return adler
  41. End Function
  42.  
  43. Function gzip_adler32bank(bank)
  44.         adler = zip_adler32(0,bank,0)
  45.         adler = zip_adler32(adler,bank,BankSize(bank))
  46.         Return adler
  47. End Function
  48.  
  49. Function gzip_compressBank(bank)
  50.         sourceLen = BankSize(bank)
  51.         dest = CreateBank(Ceil(sourceLen * 1.1)+12)
  52.         destLen = CreateBank(4)
  53.         PokeInt(destLen,0,BankSize(dest))
  54.         If zip_compress(dest, destLen, bank,sourceLen) <> 0 Then Return False
  55.         newLen = PeekInt(destLen,0)
  56.        
  57.         If newlen <= 0
  58.                 FreeBank destLen
  59.                 FreeBank dest
  60.                 Return False
  61.         EndIf
  62.         ResizeBank(bank,newLen)
  63.         CopyBank(dest,0,bank,0,newlen)
  64.        
  65.         FreeBank destLen
  66.         FreeBank dest
  67.         Return bank
  68. End Function
  69.  
  70. Function gzip_uncompressBank(bank,Length)
  71.         sourceLen = BankSize(bank)
  72.         dest = CreateBank(Length)
  73.         destLen = CreateBank(4)
  74.         PokeInt(destLen,0,Length)
  75.         If zip_uncompress(dest, destLen, bank,sourceLen) <> 0 Then Return callback
  76.         ResizeBank(bank,Length)
  77.         CopyBank(dest,0,bank,0,Length)
  78.        
  79.         FreeBank destLen
  80.         FreeBank dest
  81.         Return bank
  82. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal