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

首頁 > 學院 > 開發設計 > 正文

lua使用zlib壓縮和解壓

2019-11-11 05:58:52
字體:
來源:轉載
供稿:網友

最近學習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")    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函數結束的標準是返回nil,如果返回其他的會報錯或者死循環。

修改之后的我的版本:

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。代碼很簡單,壓縮一個字符串和解壓一個字符串!

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴彦县| 凤阳县| 竹北市| 托克托县| 阿合奇县| 鄢陵县| 闵行区| 伊宁市| 友谊县| 涡阳县| 荃湾区| 米林县| 深水埗区| 当雄县| 龙门县| 崇仁县| 呼玛县| 扬中市| 新晃| 岳阳县| 亳州市| 金平| 东乌珠穆沁旗| 阿克陶县| 宝清县| 望江县| 上林县| 澄江县| 桦甸市| 巴彦淖尔市| 嵩明县| 东安县| 隆尧县| 定州市| 临朐县| 和静县| 简阳市| 太白县| 两当县| 宁强县| 黄山市|