如何使用Lua从zip文件中提取文件?
如何使用Lua提取文件 更新:我现在有以下代码,但每次到达函数末尾时它都会崩溃,但它成功地提取了所有文件并将它们放在正确的位置如何使用Lua从zip文件中提取文件?,lua,zip,extraction,Lua,Zip,Extraction,如何使用Lua提取文件 更新:我现在有以下代码,但每次到达函数末尾时它都会崩溃,但它成功地提取了所有文件并将它们放在正确的位置 require "zip" function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath) local zfile, err = zip.open(zipPath .. zipFilename) -- iterate through each file insize the
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\\")
为什么每次到达终点都会崩溃?简短回答:
是一个轻量级扩展库,用于读取存储在zip文件中的文件。该API与标准Lua I/O库API非常相似
使用LuaZip从归档文件中读取文件,然后使用将它们写入文件系统。如果您需要ANSI C不支持的文件系统操作,请查看。LuaFileSystem是一个Lua库,用于补充标准Lua发行版提供的文件系统相关功能集。LuaFileSystem提供了一种可移植的方式来访问底层目录结构和文件属性
进一步阅读: 是一个使用ZIP压缩的Lua虚拟文件系统
如果您需要读取streams或gzip,请查看。Lua gzip文件I/O模块模拟标准I/O模块,但在压缩的gzip格式文件上运行。您似乎忘记了在循环中关闭currFile。 我不确定它崩溃的原因:可能是一些草率的资源管理代码或资源耗尽(您可以打开的文件数量可能有限) 无论如何,正确的代码是:
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
GitHub上由“davidm”编写的“lua compress deflatelua”存储库在普通lua中实现了Gzip算法。链接:(文件在lmod目录中。)
用法示例:
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()
我认为这行不通。我想实际提取zip中的文件,而不仅仅是查看zip中的文件。提取是从存档中读取并写入文件系统的过程。您需要关于写入文件系统的说明吗?如果是这样,请查看Lua
io
和os
模块。那么我必须读写每个文件吗?也许我最好通过windows系统调用来解压文件。如果你在脚本旁边安装了archiver,那就好了。我不知道有一个命令行工具可以提取与Windows捆绑的Zip文件。。。似乎写入文件很容易。然后我就可以从zip中打开每个文件,然后把它写到我想要的任何地方。