How to extract files from a zip file using Lua?

How to extract files using Lua?

Update: now I 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 place.

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 he fall every time he reaches the end?

+6
lua extraction zip
source share
3 answers

Short answer:

LuaZip is a lightweight Lua extension library used to read files stored in 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 file system using the Lua io module . If you require file system operations not supported by ANSI C, see LuaFileSystem . LuaFileSystem is a Lua library designed to complement the set of functions associated with file systems offered by the standard Lua distribution. LuaFileSystem offers a portable way to access the basic 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 a standard I / O module, but works with gzip compressed files.

+7
source share

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

In any case, 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 
+2
source share

The repository "lua-compress-deflatelua" on GitHub, "davidm", implements the Gzip algorithm in simple Lua. Link: https://github.com/davidm/lua-compress-deflatelua (Files are in the lmod directory.)

Usage example:

 local DEFLATE = require 'compress.deflatelua' -- uncompress gzip file local fh = assert(io.open('foo.txt.gz', 'rb')) local ofh = assert(io.open('foo.txt', 'wb')) DEFLATE.gunzip {input=fh, output=ofh} fh:close(); ofh:close() 
+1
source share

All Articles