Logic Machine Forum
gunzip http response - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: gunzip http response (/showthread.php?tid=4246)



gunzip http response - spyder - 17.09.2022

Hello,

Has anyone unzipped a gzip byte array in Lua?

Thanks!


RE: gunzip http response - spyder - 19.09.2022

For now I am using the filesystem, but I am worried of wearing out the SD card by unzipping a file from 300kB -> 5MB every 10 minutes.

Is there a way of doing this in memory? Is there a part of the filesystem in memory?

Code:
--byteArray downloaded from website
io.writefile('/tmp/download.gz', byteArray)
os.execute('gunzip /tmp/download.gz')
local handle = io.popen('/tmp/download')
local firstByte = string.byte(handle:read(1))

Thanks


RE: gunzip http response - admin - 19.09.2022

/tmp is RAM-based file system so you can safely write there as many time as needed. Just make sure to remove any unused files or you can run out of memory in certain cases.

Another option is to use this library: https://raw.githubusercontent.com/hamishforbes/lua-ffi-zlib/master/lib/ffi-zlib.lua
Add is as user library named zlib.

Usage example:
Code:
zlib = require('user.zlib')
input = '...' -- binary string containing gzip data
offset = 1
buffer = {}

zlib.inflateGzip(function(size)
  local chunk = input:sub(offset, offset + size - 1)
  offset = offset + size

  if #chunk > 0 then
    return chunk
  end
end,
function(chunk)
  buffer[ #buffer + 1 ] = chunk
end)

output = table.concat(buffer)