【问题标题】:How to extract files from a zip file using Lua?如何使用 Lua 从 zip 文件中提取文件?
【发布时间】:2011-02-19 05:26:40
【问题描述】:

如何使用 Lua 提取文件?

更新:我现在有以下代码,但每次到达函数末尾时都会崩溃,但它成功提取所有文件并将它们放在正确的位置。

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\\")

为什么每次到达终点都会崩溃?

【问题讨论】:

    标签: lua zip extraction


    【解决方案1】:

    简答:

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

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


    进一步阅读:

    LAR 是一个使用 ZIP 压缩的 Lua 虚拟文件系统。

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

    【讨论】:

    • 我不相信这会奏效。我想实际提取 zip 中的文件,而不仅仅是查看 zip 中的文件。
    • 提取是从存档读取并写入文件系统的过程。您需要有关写入文件系统的说明吗?如果是这样,请查看 Lua ioos 模块。
    • 所以我必须读写每个文件?也许我最好进行 Windows 系统调用来解压缩文件。
    • 如果您在脚本旁边安装存档器,那很好。我不知道用于提取与 Windows 捆绑的 Zip 文件的命令行实用程序。
    • 好吧,我放弃了……看起来写入文件很容易。然后我可以从 zip 中打开每个文件并将其写到我想要的任何地方。
    【解决方案2】:

    您似乎忘记在循环中关闭 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
    

    【讨论】:

      【解决方案3】:

      “davidm”在 GitHub 上的“lua-compress-deflatelua”存储库在普通 Lua 中实现了 Gzip 算法。链接:https://github.com/davidm/lua-compress-deflatelua(文件在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()
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2012-07-21
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多