国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

lua使用zlib壓縮和解壓

2019-11-11 06:01:50
字體:
供稿:網(wǎng)友

最近學習lua+nginx。在項目中要使用nginx的反向代理,從中遇見諸多麻煩!最蛋疼的是使用的平臺是windows,哎!這套東西在window的相關(guān)文檔資料很少!寫寫關(guān)于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")    returnelse    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    endendPS:注意這段代碼,這里input函數(shù)結(jié)束的標準是返回nil,如果返回其他的會報錯或者死循環(huán)。

修改之后的我的版本:

local table_insert = table.insertlocal table_concat = table.concatlocal zlib = require('ffi-zlib')local chunk = 16384local str = "ab"local count = 0local 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 dataendlocal output_table = {}local output = function(data)    table_insert(output_table, data)end-- Compress the datangx.say('Compressing')local ok, err = zlib.deflateGzip(input, output, chunk)if not ok then    ngx.say(err)endlocal compressed = table_concat(output_table,'')ngx.say("---------------------------")ngx.say(compressed)ngx.say("---------------------------")-- Decompress it againngx.say('Decompressing')output_table = {}local count = 0local 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 dataendlocal ok, err = zlib.inflateGzip(input, output, chunk)if not ok then    ngx.say(err)endlocal output_data = table_concat(output_table,'')ngx.say("---------------------------")ngx.say(output_data)ngx.say("---------------------------")如果沒有使用nginx,ngx.say換位print。代碼很簡單,壓縮一個字符串和解壓一個字符串!

有更好的方法,請大家賜教!謝謝


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 冀州市| 黑龙江省| 芜湖县| 青浦区| 遂平县| 九江市| 贵溪市| 蓬安县| 荃湾区| 闽侯县| 宁河县| 崇礼县| 河东区| 武城县| 广饶县| 克什克腾旗| 太康县| 吴江市| 峡江县| 广水市| 石屏县| 赤壁市| 南郑县| 宁化县| 勃利县| 涪陵区| 上高县| 双城市| 新竹市| 阆中市| 永修县| 定州市| 太白县| 临沭县| 六安市| 万源市| 望奎县| 通州市| 莆田市| 运城市| 曲周县|