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(zipP开发者_如何学JAVAath, 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?
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.
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
The "lua-compress-deflatelua" repository on GitHub, by "davidm", implements the Gzip algorithm in plain Lua. Link: https://github.com/davidm/lua-compress-deflatelua (The files are in the lmod directory.)
Example usage:
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()
精彩评论