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

查看:574
本文介绍了如何使用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 是轻量级的

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 是使用ZIP压缩的Lua虚拟文件系统.

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

如果您需要阅读 gzip 流或gzip压缩的 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天全站免登陆