如何使用 Lua 从 zip 文件中提取文件?

发布于 2024-09-01 14:39:38 字数 974 浏览 8 评论 0原文

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

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

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

Why does it crash every time it reaches the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-09-08 14:39:38

简短回答:

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 格式文件进行操作。

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.

与他有关 2024-09-08 14:39:38

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

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
风铃鹿 2024-09-08 14:39:38

GitHub 上的“lua-compress-deflatelua”存储库由“davidm”开发,用普通 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文