lua使用zlib压缩和解压

最近学习lua+nginx。在项目中要使用nginx的反向代理,从中遇见诸多麻烦!最蛋疼的是使用的平台是windows,哎!这套东西在window的相关文档资料很少!写写关于lua使用zlib压缩和解压的问题。

有两种方式可以完成

第一种:使用lua-zlib

看了一下别人的方式,http://blog.csdn.net/kowity/article/details/7229815

在windows下编译这几个库并没成功,失败了。

第二种:使用lua-ffi-zlib。

使用之前必须有zlib包,如果使用的是openresty,在跟目录下回有.so或者dll库文件。PS:下载之后文件中的名字是zlib.dll,更改为zlib1.dll

下载文件https://github.com/hamishforbes/lua-ffi-zlib,里面有测试用例。可以直接使用!

if arg[1] == nil then
    print("No file provided")
    return
else
    f = io.open(arg[1], "rb")
    input = function(bufsize)
        local d = f:read(bufsize)
        if d == nil then
            return nil
        end
        in_crc = zlib.crc(d, in_crc)
        in_adler = zlib.adler(d, in_adler)
        uncompressed = uncompressed..d
        return d
    end
end

PS:注意这段代码,这里input函数结束的标准是返回nil,如果返回其他的会报错或者死循环。

修改之后的我的版本:


local table_insert = table.insert
local table_concat = table.concat
local zlib = require('ffi-zlib')
local chunk = 16384

local str = "ab"
local count = 0
local input = function(bufsize)
	local start = count > 0 and bufsize*count or 1
    local data = str:sub(start, (bufsize*(count+1)-1))
	if data == "" then
		data = nil
	end
	ngx.say("##################")
	ngx.say(data)
	ngx.say("##################")
    count = count + 1
    return data
end


local output_table = {}
local output = function(data)
    table_insert(output_table, data)
end

-- Compress the data
ngx.say('Compressing')
local ok, err = zlib.deflateGzip(input, output, chunk)
if not ok then
    ngx.say(err)
end
local compressed = table_concat(output_table,'')
ngx.say("---------------------------")
ngx.say(compressed)
ngx.say("---------------------------")


-- Decompress it again

ngx.say('Decompressing')
output_table = {}
local count = 0
local input = function(bufsize)
    local start = count > 0 and bufsize*count or 1
    local data = compressed:sub(start, (bufsize*(count+1)-1) )
    count = count + 1
    return data
end

local ok, err = zlib.inflateGzip(input, output, chunk)
if not ok then
    ngx.say(err)
end
local output_data = table_concat(output_table,'')

ngx.say("---------------------------")
ngx.say(output_data)
ngx.say("---------------------------")


如果没有使用nginx,ngx.say换位print。代码很简单,压缩一个字符串和解压一个字符串!


有更好的方法,请大家赐教!谢谢









Lua Subtree 是 Git 的一个功能,它允许你将一个 Git 仓库作为另一个 Git 仓库的子目录进行管理,这样就可以将一个 Git 仓库作为另一个 Git 仓库的一部分进行版本控制。在 Lua 的开发中,我们可以使用 Lua Subtree 来添加和使用第三方 Lua 库。 以下是使用 Lua Subtree 的步骤: 1. 在你的 Lua 项目中添加一个新的 Git Subtree。 ```bash git subtree add --prefix=<local-path> <remote-repo> <remote-branch> --squash ``` 其中,`<local-path>` 是子目录的相对路径,`<remote-repo>` 是要添加的 Git 仓库的 URL,`<remote-branch>` 是要添加的 Git 仓库的分支。`--squash` 参数表示将所有 Git 提交合并为一个提交。 例如,我们要将 `lua-resty-template` 库添加到我们的 Lua 项目中: ```bash git subtree add --prefix=libs/lua-resty-template https://github.com/bungle/lua-resty-template.git master --squash ``` 2. 将新添加的子目录提交到你的 Lua 项目中。 ```bash git commit -m "Add lua-resty-template library" ``` 3. 将子目录中的代码更新到最新的版本。 ```bash git subtree pull --prefix=<local-path> <remote-repo> <remote-branch> --squash ``` 例如,要将 `lua-resty-template` 更新到最新版本: ```bash git subtree pull --prefix=libs/lua-resty-template https://github.com/bungle/lua-resty-template.git master --squash ``` 4. 在你的 Lua 代码中使用新添加的库。 在你的 Lua 代码中添加以下代码来加载新添加的库: ```lua package.path = package.path .. ";libs/lua-resty-template/lib/?.lua" local template = require "resty.template" ``` 现在,你可以在你的 Lua 代码中使用 `template` 模块中的函数了。 总结一下,使用 Lua Subtree 来添加和使用第三方 Lua 库的步骤如下: 1. 添加 Git Subtree。 2. 提交子目录中的代码。 3. 更新子目录中的代码。 4. 在你的 Lua 代码中使用新添加的库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值