Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract files from a zip file using Lua?

How do I extract files using Lua?

Update: I now have the following code but it crashes every time it reaches the end of the function, but it successfully extracts all the files and puts them in the right location.

require "zip"

function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath)
    local zfile, err = zip.open(zipPath .. zipFilename)

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

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

    zfile:close()
end
-- call the function
ExtractZipAndCopyFiles("C:\\Users\\bhannan\\Desktop\\LUA\\", "example.zip", "C:\\Users\\bhannan\\Desktop\\ZipExtractionOutput\\")

Why does it crash every time it reaches the end?

like image 370
Brian T Hannan Avatar asked May 13 '10 18:05

Brian T Hannan


2 Answers

Short Answer:

LuaZip is a lightweight Lua extension library used to read files stored inside zip files. The API is very similar to the standard Lua I/O library API.

Use LuaZip to read files from the archive and then write them to the filesystem using the Lua io module. If you require filesystem operations not supported by ANSI C, then take a look at LuaFileSystem. LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution. LuaFileSystem offers a portable way to access the underlying directory structure and file attributes.


Further reading:

LAR is a virtual file system for Lua using ZIP compression.

If you need to read gzip streams or gzipped tar files then take a look at gzio. The Lua gzip file I/O module emulates the standard I/O module, but operates on compressed gzip format files.

like image 169
Judge Maygarden Avatar answered Sep 28 '22 07:09

Judge Maygarden


It seems that you forgot to close currFile in the loop. I'm not sure why it crashes : maybe some sloppy resources management code or resource exhaustion (the number of files you can opened may be limited)...

Anyway the correct code is :

require "zip"

function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath)
local zfile, err = zip.open(zipPath .. zipFilename)

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

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

zfile:close()
end
like image 31
Name is carl Avatar answered Sep 28 '22 08:09

Name is carl