1. -- ************************************************************************
  2. -- Script:   GZDEFLATE
  3. -- Version:  0.2
  4. -- Date:     2009-08-09
  5. -- Author:   Valentin Schmidt
  6. --
  7. -- Description:
  8. -- implements DEFLATE compression according to RFC1951 (http://www.faqs.org/rfcs/rfc1951)
  9. -- data is e.g. compatible with PHP's gzdeflate()/gzinflate() functions.
  10. --
  11. -- Notice: unfortunately the current implementation of the compress/uncompress functions of director's bytearray class use
  12. -- ZLIB (i.e. gzcompress) instead of raw DEFLATE compression. this is no limitation for compressing data (as
  13. -- DEFLATE/GZIP/ZIP files) since the 6 additional ZLIB bytes (2 bytes at the beginning, 4 adler32 checksum bytes at the end)
  14. -- just need to be removed to create valid DEFLATE compressed data (that can then be used to create GZIP/ZIP files). but
  15. -- unfortunately the bytearray's uncompress() function fails if the 4 adler32 checksum bytes at the end of the bytearray are
  16. -- missing or incorrect, and therefor INFLATE, GZIP and ZIP decompression is only possible if the adler32 checksums of the
  17. -- original file(s) are known.
  18. -- one way to lessen this strong limitation a little bit, so at least GZIP and ZIP files created with those scripts can be
  19. -- decompressed without requiring any additional info, is to store the adler32 checksums as comments inside the GZP/ZIP file.
  20. -- such files can be decompresssed with any 3rd party tool (which just ignore the comments) as well as with code based on
  21. -- byterrays's uncompress function.
  22. --
  23. -- Requirements:
  24. -- * fileIO xtra
  25. -- * fileIO wrapper functions file_get_bytes/file_put_bytes
  26. --
  27. -- ************************************************************************
  28.  
  29. ----------------------------------------
  30. -- deflate data
  31.  
  32. -- NOTICE:
  33. -- passed bytearray baInput is changed (bytearray is compressed)!!!
  34. -- if this is not wanted, either create a copy or use ba.uncompress to restore original state
  35. ----------------------------------------
  36. on gzdeflate_bytearray (baInput)
  37.   info = [:]
  38.   baInput.compress()
  39.   info["data"] = bytearray()
  40.   info["data"].writeByteArray(baInput, 3, baInput.length-6) -- remove header and adler32 checksum bytes
  41.  
  42.   baInput.position = baInput.length-3
  43.   baInput.endian=#bigEndian
  44.   info["adler32"] = baInput.readInt32()
  45.  
  46.   return info
  47. end
  48.  
  49. ----------------------------------------
  50. -- deflate file
  51. ----------------------------------------
  52. on gzdeflate_file(inputFile, outputFile)
  53.   ba = file_get_bytes(inputFile)
  54.   ba.compress()
  55.   file_put_bytes (outputFile, ba, 3, ba.length-6) -- remove header and adler32 checksum bytes
  56. end
  57.  
  58. ----------------------------------------
  59. -- uncompress (inflate) deflated data
  60. -- unfortunately only possible if adler32 checksum of original file is known (=unnecessary limitation, bad conception by adobe)
  61. ----------------------------------------
  62. on gzinflate_bytearray (ba, adler32)  
  63.   ret = bytearray(ba.length+6)
  64.  
  65.   -- add header (0x78 0xDA = 120 218)
  66.   ret[1] = 120
  67.   ret[2] = 218
  68.  
  69.   -- copy deflated data
  70.   ret.position = 3
  71.   ret.writeByteArray(ba)
  72.   ret.endian = #bigEndian
  73.  
  74.   -- add adler32 checksum
  75.   ret.writeInt32(adler32)
  76.  
  77.   ret.uncompress()
  78.  
  79.   return ret
  80. end
  81.  
  82. ----------------------------------------
  83. -- uncompress (inflate) deflated file
  84. -- unfortunately only possible if adler32 checksum of original file is known (=unnecessary limitation, bad conception by adobe)
  85. ----------------------------------------
  86. on gzinflate_file(inputFile, outputFile, adler32)
  87.   ba = file_get_bytes(inputFile)
  88.   ok = file_put_bytes (outputFile, gzinflate_bytearray(ba, adler32))
  89. end
  90.  
[raw code]