如何使用 Lua 从 zip 文件中提取文件? [英] How to extract files from a zip file using Lua?

查看:93
本文介绍了如何使用 Lua 从 zip 文件中提取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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\")

为什么每次到最后都会崩溃?

推荐答案

简答:

LuaZip 是一个轻量级的 Lua 扩展库,用于读取存储在 zip 文件中的文件.该 API 与标准 Lua I/O 库 API 非常相似.

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.

使用 LuaZip 从存档中读取文件,然后使用 Lua io 模块将它们写入文件系统.如果您需要 ANSI C 不支持的文件系统操作,请查看 LuaFileSystem.LuaFileSystem 是一个 Lua 库,用于补充标准 Lua 发行版提供的与文件系统相关的功能集.LuaFileSystem 提供了一种可移植的方式来访问底层目录结构和文件属性.

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.

进一步阅读:

LAR 是 Lua 使用 ZIP 压缩的虚拟文件系统.

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

如果您需要阅读 gzip 流或 gzip 压缩的 tar 文件 然后看看 gzio.Lua gzip 文件 I/O 模块模拟标准 I/O 模块,但对压缩的 gzip 格式文件进行操作.

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.

这篇关于如何使用 Lua 从 zip 文件中提取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆