tags:

views:

48

answers:

1

I have a c++ application that interfaces with lua files. I have a lua file that extracts zip files, which works when I run it using SciTe or the Lua command line. But when I try to run it from a c++ application it doesn't seem to work.

 require "zip"

function ExtractZipFiles(zipFilename, destinationPath)
    zipFile, err = zip.open(zipFilename)

    -- iterate through each file insize the zip file
    for file in zipFile:files() do
        currentFile, err = zipFile:open(file.filename)
        currentFileContents = currentFile:read("*a") -- read entire contents of current file
        currentFile:close()
        hBinaryOutput = io.open(destinationPath .. file.filename, "wb")

        -- write current file inside zip to a file outside zip
        if(hBinaryOutput)then
            hBinaryOutput:write(currentFileContents)
            hBinaryOutput:close()
        end
    end

    zipFile:close()
end

-- Unit Test
ExtractZipFiles("SZProcessTests.zip", "Infections\\")

If I have Lua installed on the computer and double-click on the lua file it runs and the files are extracted as expected. But, it doesn't work if I try to run the lua file from C++ like this:

void CSZTestClientMessagesDlg::OnBtnExecute() 
{
    L = lua_open();
    luaL_openlibs(L);
    luaL_dofile(L, "ExtractZipFiles.lua");
    lua_close(L);

    return;
}
+4  A: 

First of all check for errors:

if (luaL_dofile(L, "ExtractZipFiles.lua"))
{
    std::cerr << "Lua error : " << lua_tostring(L, -1) << std::endl;
}

Apart from that, my guess is that Lua can't locate the zip module -- check your package paths settings, read about require in the Lua manual.

In general, you need to check where the zip module is (zip.lua?) and make sure it's accessible during runtime (you can manually load it for example if all else fails).

Kornel Kisielewicz
Great response, that was very helpful.
Brian T Hannan