[Solved] Unable to read compressed file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

[Solved] Unable to read compressed file

Post by alberto_lara »

Hi guys, I'm not fully familiarized with the math or filesystem API and I'm stuck here:

Code: Select all

function love.load()
    dataComp = love.math.compress("dsff sd  87sd f9f7 sd9 fsd")
    ok = love.filesystem.write("test1", dataComp)

    if ok then
        print("data compressed:")
        print(tostring(love.filesystem.read("test1")))
    end
end

function love.update(dt)

end

function love.draw()

end
I don't get any prints on the console (I'm on linux) and my intuition says I should get at least the "data compressed" text (if I comment the filesystem.read line then I get it, otherwise I get nothing, that's what confuses me more!). I can confirm the data is actually being written in a "test1" file.

Any help will be appreciated. Thanks in advance.

EDIT: If I don't compress the data then I can save it and get it again with no problems so I guess it has something to do with compress?
Last edited by alberto_lara on Tue Mar 07, 2017 10:02 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Unable to read compressed file

Post by raidho36 »

Clearly your issue is that both of those prints are behind the if-guard, and it doesn't execute for one reason or another.
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Unable to read compressed file

Post by alberto_lara »

That was the first thing I thought as well, but if you comment this line:

Code: Select all

print(tostring(love.filesystem.read("test1")))
then you get the "data compressed:" text, so the "ok" flag is = true. The weird thing is, if I try to read the file, then I get no prints, like if "ok" is = false, but if the read() function is causing it to be false, then that makes no sense for me (because it has to be true to call read() anyway... pretty weird)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Unable to read compressed file

Post by raidho36 »

Oh right, I see. Compression creates all 256 single byte characters (which can in turn cause invalid multi byte characters) most of which are unprintable or otherwise break printing. Just don't print the compressed byte string. Convert it to hex format or base 64, if you need to print it anyway.
Last edited by raidho36 on Tue Mar 07, 2017 9:14 pm, edited 1 time in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Unable to read compressed file

Post by Nixola »

Maybe there's some weird byte you print; try printing the uncompressed thata, or each of the bytes ( print(love.filesystem.read("test1"):gsub(".", function(c) return ("0x%x "):format(c:byte()) end)) )
EDIT - ninja'd by raidho, but answer is still kind of relevant
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Unable to read compressed file

Post by alberto_lara »

I need to read the compressed data and then decompress it, sorry, I didn't mention that. Any ideas of how can I do this?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Unable to read compressed file

Post by zorg »

io.write may print it, but as raidho said, even then it's also dependent on the console/terminal language encoding and the font, so a hexdump would be better:

Code: Select all

-- from: http://lua-users.org/wiki/HexDump
function hex_dump(buf)
      for byte=1, #buf, 16 do
         local chunk = buf:sub(byte, byte+15)
         io.write(string.format('%08X  ',byte-1))
         chunk:gsub('.', function (c) io.write(string.format('%02X ',string.byte(c))) end)
         io.write(string.rep(' ',3*(16-#chunk)))
         io.write(' ',chunk:gsub('%c','.'),"\n") 
      end
   end
Also, for decompressing, there's the fittingly named love.math.decompress that goes with the compress function.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Unable to read compressed file

Post by alberto_lara »

zorg wrote: Tue Mar 07, 2017 9:20 pm Also, for decompressing, there's the fittingly named love.math.decompress that goes with the compress function.
I'm aware of this, but I don't really know if this is the right way (I guess not, should I do first the conversion thing? if so, how can I convert that dumped data to a lua table again?)

Code: Select all

function love.load()
    dataComp = love.math.compress("dsff sd  87sd f9f7 sd9 fsd")
    ok = love.filesystem.write("test1", dataComp)

    if ok then
        decompStr = love.math.decompress(love.filesystem.read("test1"))
        print(decompStr)
    end
end

function love.update(dt)

end

function love.draw()

end
this throws:

Code: Select all

Error: main.lua:338: Invalid compressed data format: 32
EDIT: I'm aware the file read returns "raw" content, so LÖVE doesn't really know if it's a CompressedData object (right?) so this is the part I care, how to save a compressed file (which type is CompressedData), get it again and somehow tell to LÖVE that it is an object of that type so I can get the original string.
Last edited by alberto_lara on Tue Mar 07, 2017 9:47 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Unable to read compressed file

Post by raidho36 »

The read function returns string plus it's length. Both get passed into decompress, which needs second argument to be compression type, and of course "32" of not a valid one.
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Unable to read compressed file

Post by alberto_lara »

Holy crap, thanks for that last comment! It works fine now with this approach:

Code: Select all

function love.load()
    dataComp = love.math.compress([[
        data = {
            {[1] = 32432, [2] = 3423},
            {[1] = 32432, [2] = 3423},
            {[1] = 32432, [2] = 3423},
            {[1] = 32432, [2] = 3423},
            {[1] = 32432, [2] = 3423},
            {[1] = 32432, [2] = 3423},
        }
    ]], "lz4")
    ok = love.filesystem.write("test1", dataComp)

    if ok then
        local data, size = love.filesystem.read("test1")
        decompStr = love.math.decompress(data, "lz4")
        print(decompStr)
    end
end

function love.update(dt)

end

function love.draw()

end
Post Reply

Who is online

Users browsing this forum: Google [Bot], Roland Chastain and 246 guests