Lua:如何在内存中gzip一个字符串(gzip,不是zlib)? [英] Lua: How to gzip a string (gzip, not zlib) in memory?

查看:759
本文介绍了Lua:如何在内存中gzip一个字符串(gzip,不是zlib)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串,如何使用gzip将其压缩在内存中?我正在使用Lua.

Given a string, how can I compress it in memory with gzip? I'm using Lua.

听起来像是一个简单的问题,但是有大量的库.到目前为止,我尝试过的所有操作要么已死,要么只能生成zlib压缩字符串.在我的用例中,我需要接收者期望的gzip压缩.

It sounds like an easy problem, but there is a huge list of libraries. So far, all that I tried were either dead or I could produce only zlib compressed strings. In my use case, I need gzip compression, as it is expected by the receiver.

作为测试,如果将压缩后的字符串转储到文件中,zcat应该可以将其解压缩.

As a test, if you dump the compressed string to a file, zcat should be able to decompress it.

我正在使用OpenResty,所以任何Lua库都应该没问题.

I'm using OpenResty, so any Lua library should be fine.

(到目前为止,我唯一可行的解​​决方案是将字符串转储到文件中,调用os.execute("gzip /tmp/example.txt")并将其读回.不幸的是,这不是实际的解决方案.)

(The only solution that I got working so far is to dump the string in a file, call os.execute("gzip /tmp/example.txt") and read it back. Unfortunately, that is not a practical solution.)

推荐答案

事实证明zlib与gzip并不遥远.区别在于gzip还有一个附加的标头.

It turns out that zlib is not far away from gzip. The difference is that gzip has an additional header.

要获取此标头,您可以像这样使用 lua-zlib :

To get this header, you can use lua-zlib like this:

local zlib = require "zlib"

-- input:  string
-- output: string compressed with gzip
function compress(str)
   local level = 5
   local windowSize = 15+16
   return zlib.deflate(level, windowSize)(str, "finish")
end

说明:

  • deflate的第二个参数是窗口大小.确保已写入gzip标头.如果省略该参数,则会得到一个zlib压缩字符串.
  • level是gzip压缩级别(1 =最差,9 =最佳)

以下是deflate的文档(来源: lua-zlib文档):

Here is the documentation of the deflate (source: lua-zlib documentation):

function stream = zlib.deflate([int compression_level],[int window_size])

function stream = zlib.deflate([ int compression_level ], [ int window_size ])

If no compression_level is provided uses Z_DEFAULT_COMPRESSION (6),
compression level is a number from 1-9 where zlib.BEST_SPEED is 1
and zlib.BEST_COMPRESSION is 9.

Returns a "stream" function that compresses (or deflates) all
strings passed in.  Specifically, use it as such:

string deflated, bool eof, int bytes_in, int bytes_out =
        stream(string input [, 'sync' | 'full' | 'finish'])

    Takes input and deflates and returns a portion of it,
    optionally forcing a flush.

    A 'sync' flush will force all pending output to be flushed to
    the return value and the output is aligned on a byte boundary,
    so that the decompressor can get all input data available so
    far.  Flushing may degrade compression for some compression
    algorithms and so it should be used only when necessary.

    A 'full' flush will flush all output as with 'sync', and the
    compression state is reset so that decompression can restart
    from this point if previous compressed data has been damaged
    or if random access is desired. Using Z_FULL_FLUSH too often
    can seriously degrade the compression. 

    A 'finish' flush will force all pending output to be processed
    and results in the stream become unusable.  Any future
    attempts to print anything other than the empty string will
    result in an error that begins with IllegalState.

    The eof result is true if 'finish' was specified, otherwise
    it is false.

    The bytes_in is how many bytes of input have been passed to
    stream, and bytes_out is the number of bytes returned in
    deflated string chunks.

这篇关于Lua:如何在内存中gzip一个字符串(gzip,不是zlib)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆