Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to load and save binary data — Gideros Forum

How to load and save binary data

antixantix Member
edited August 2017 in General questions
I am having trouble getting a binary file to save and load. I am trying to do the following..

1. encode a table to a json string
2. compress the string with zlib
3. save the result to device
4. reload the saved data
5. decompress it back into a json string
6. decode it back into a table

The issue seems to be in the saving and loading of the file. In my example the compressed string is 143 bytes in size. When it is saved and reloaded it's size is only 5 bytes. Does anyone know how I can actually save the entire compressed data and reload it again?
require("json")
 
local file, blob, comp, data
 
data = {
  version = "1.1",
  luaversion = "5.1",
  tiledversion = "1.0.2",
  orientation = "orthogonal",
  renderorder = "left-down",
  width = 100,
  height = 50,
  tilewidth = 16,
  tileheight = 16,
  nextobjectid = 188,
  properties = {},
  tilesets = {}
}
 
blob = json.encode(data) -- encode table
comp = zlib.compress(blob, 9, 8, 15, 9, 0) -- compress json string
 
print("#datasize before write", #comp)
 
file = io.open("|D|data", "wb") -- write binary file
file:write(comp)
file:close()
file = nil
 
file = io.open("|D|data", "rb") -- read binary file
comp = file:read()
file:close()
file = nil
print("#datasize after read", #comp)
 
--blob = zlib.decompress(comp, 15) -- recreate json string
--data = json.decode(json_blob2) -- recreate table

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    @antix, did you try
    comp = file:read("*all")
    ?
  • @hgy29 thanks! It works now @};-
Sign In or Register to comment.