如何使用Lua从ZIP文件中提取文件?
2010-5-13 20:53:19
收藏:0
阅读:336
评论:3
如何使用 Lua 提取文件?
更新:现在我有以下代码,但每次到达函数的末尾时都会崩溃,但它成功地提取了所有文件并将它们放在正确的位置上。
require "zip"
function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath)
local zfile, err = zip.open(zipPath .. zipFilename)
-- 逐个遍历 zip 文件中的每个文件
for file in zfile:files() do
local currFile, err = zfile:open(file.filename)
local currFileContents = currFile:read("*a") -- 读取当前文件的全部内容
local hBinaryOutput = io.open(destinationPath .. file.filename, "wb")
-- 将 zip 中的当前文件写到 zip 外的文件中
if(hBinaryOutput)then
hBinaryOutput:write(currFileContents)
hBinaryOutput:close()
end
end
zfile:close()
end
-- 调用函数
ExtractZipAndCopyFiles("C:\\Users\\bhannan\\Desktop\\LUA\\", "example.zip", "C:\\Users\\bhannan\\Desktop\\ZipExtractionOutput\\")
为什么每次到达末尾时都会崩溃?
原文链接 https://stackoverflow.com/questions/2829175
点赞
stackoverflow用户740464
似乎你在循环中忘了关闭currFile。 我不确定为什么会崩溃:也许是因为一些懒散的资源管理代码或资源耗尽(你可以打开的文件数量可能有限)...
不管怎样,正确的代码是:
require "zip"
function ExtractZipAndCopyFiles(zipPath, zipFilename, destinationPath)
local zfile, err = zip.open(zipPath .. zipFilename)
-- 遍历zip文件中的每个文件
for file in zfile:files() do
local currFile, err = zfile:open(file.filename)
local currFileContents = currFile:read("*a") -- 读取当前文件的所有内容
local hBinaryOutput = io.open(destinationPath .. file.filename, "wb")
-- 将zip文件中的当前文件写入zip文件外的文件
if(hBinaryOutput)then
hBinaryOutput:write(currFileContents)
hBinaryOutput:close()
end
currFile.close()
end
zfile:close()
end
2011-08-21 19:14:34
stackoverflow用户1210278
“davidm”在GitHub上的“lua-compress-deflatelua”存储库中,使用Lua实现了Gzip算法。链接:https://github.com/davidm/lua-compress-deflatelua(文件在lmod目录中。)
使用示例:
local DEFLATE = require 'compress.deflatelua'
-- 解压gzip文件
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()
2012-09-11 05:14:58
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
短答案:
LuaZip是一个轻量级的Lua扩展库,用于读取存储在 zip 文件中的文件。其 API 与标准的 Lua I/O 库 API 非常相似。
使用 LuaZip 从存档中读取文件,然后使用Lua io 模块将其写入文件系统。如果您需要文件系统操作不支持 ANSI C,则可以查看LuaFileSystem。LuaFileSystem 是一款 Lua 库,旨在补充标准 Lua 发行版提供的与文件系统相关的功能集。LuaFileSystem 提供了一种可移植的访问底层目录结构和文件属性的方式。
进一步阅读:
LAR是使用 ZIP 压缩技术的 Lua 虚拟文件系统。
如果需要读取gzip流或经过 gzip 压缩的tar 文件,可以查看gzio。Lua gzip 文件 I/O 模块模拟标准的 I/O 模块,但是在压缩的 gzip 格式文件上操作。