Quick Links: Gideros Home | Download Gideros | Developer Guide
How to load and save binary data
  • antixantix +1 -1
    Member
    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
    Check out my DevBlog, my GitHub, and my games Falling Animals | Breaky Wall | Exetor
  • hgy29hgy29 +1 -1
    Maintainer Accepted Answer
    @antix, did you try
    comp = file:read("*all")
    ?
  • antixantix +1 -1
    Member
    @hgy29 thanks! It works now @};-
    Check out my DevBlog, my GitHub, and my games Falling Animals | Breaky Wall | Exetor

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with Google Sign In with OpenID

In this Discussion

Top Posters