1. 程式人生 > >openresty使用gzip壓縮解壓

openresty使用gzip壓縮解壓

1、檢視 zlib在centos 中是否存在?
rpm -qa | grep zlib

顯示:
zlib-devel-1.2.3-29.el6.x86_64
zlib-1.2.3-29.el6.x86_64

2、安裝cmake編譯器

yum install -y gcc gcc-c++ make automake 
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar -zxvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./bootstrap
gmake
gmake install

檢查cmake安裝
cmake --version
顯示
cmake version 2.8.10.2
表示安裝成功

3、下載lua-zlib包,並解壓
unzip lua-zlib-master.zip
cd /usr/local/software/lua-zlib-master

cmake -DLUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1

若報錯:CMake Error at CMakeLists.txt:27 (find_package):
  By not providing "FindLua.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Lua", but
  CMake did not find one.

/usr/local/share/cmake-2.8/Modules目錄下(該目錄可能不存在 FindLua.cmake,查詢其他.cmke檔案獲取目錄 ),有 FindLua51.cmake,改名為 FindLua.cmake,重新執行上個命令。


make

cp zlib.so /usr/local/openresty/lualib/zlib.so

4.lua指令碼呼叫

local zip = require 'zlib'

local uncompress = zip.inflate()
local compress = zip.deflate()


local deflated, eof, bytes_in,bytes_out =compress("asdasdasdasdasdasdasdasdasd", 'finish')
print(deflated, eof, bytes_in,bytes_out

)
local uss,ret,getin,getout=uncompress(deflated)print(uss,ret,getin,getout)

print(uss,ret,getin,getout)

5.zlib庫不能直接壓縮gzip格式,使用lua-ffi-zlib

原始碼路徑:https://github.com/hamishforbes/lua-ffi-zlib

呼叫:

local ffi_zlib = require "lib.ffi-zlib"

local chunk = 16384
    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
        print(data)
        count = count + 1  
        return data  
    end 
    local output_table = {}  
    local output = function(data)
        insert(output_table, data)
    end 
    local ok, err = ffi_zlib.deflateGzip(input, output, chunk)  
    if not ok then  
        print(err)
    end  
    local compress = concat(output_table,'')  
    ngx.header["Content-Encoding"] = "gzip"

ngx.print(compress)