为什么解压 zip 文件后 lua 崩溃?

发布于 2024-09-01 02:58:39 字数 947 浏览 5 评论 0原文

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

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

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

I 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技术交流群

发布评论

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

评论(2

眼眸里的那抹悲凉 2024-09-08 02:58:39

也许您需要在每次迭代中的 currFile:read() 之后调用 currFile:close()

Perhaps you need to call currFile:close() after currFile:read() in each iteration?

人生戏 2024-09-08 02:58:39

问题是 LuaZip 不会迭代所有打开的内部文件并在关闭包含它们的打开的 zip 文件之前关闭它们。因此,当垃圾收集器尝试关闭那些已将地毯从其下面拉出的内部文件时,系统稍后会崩溃。因此,只需删除 zfile:close() 行也可以修复此崩溃,因为垃圾收集器将以与分配相反的顺序释放 userdata

在提交补丁之前,我想与 Danilo、Andre 和 Tomas 讨论可能的解决方案,因为需要做出一些设计决策。例如,如果当客户端代码关闭 zip 文件时内部文件处于打开状态,您是否会保持 zip 文件打开直到所有内部文件都被释放或使每个内部文件的打开引用无效?也许应该不理会它,并且应该指示用户 (a) 让垃圾收集器处理关闭所有内部文件和 zip 文件,或者 (b) 在关闭包含的 zip 文件之前显式关闭所有内部文件。

The problem is that LuaZip does not iterate over all open internal files and close them before closing the open zip file in which they are contained. Thus, the system crashes later when the garbage collector attempts to close the internal files that have had the rug pulled out from under them. So, simply removing the zfile:close() line will also fix this crash because the garbage collector will release the userdata in reverse order of allocation.

I'd want to discuss possible solutions with Danilo, Andre and Tomas before submitting a patch because some design decisions need to be made. For example, if an internal file is open when client code closes the zip file do you keep the zip file open until all internal files are released or invalidate open references to each internal file? Perhaps it should be left alone and users should be instructed to either (a) let the garbage collector handle closing all internal and zip files or (b) explicitly close all internal files before closing the containing zip file.

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