如何在 ruby​​ 中解压缩 Gzip 字符串?

发布于 2024-08-03 01:58:59 字数 253 浏览 6 评论 0原文

Zlib::GzipReader 可以采用“IO 或类似 IO 的对象”。正如文档中所述,它是输入的。

Zlib::GzipReader.open('hoge.gz') {|gz|
  print gz.read
}

File.open('hoge.gz') do |f|
  gz = Zlib::GzipReader.new(f)
  print gz.read
  gz.close
end

我应该如何解压缩字符串?

Zlib::GzipReader can take "an IO, or IO-like, object." as it's input, as stated in docs.

Zlib::GzipReader.open('hoge.gz') {|gz|
  print gz.read
}

File.open('hoge.gz') do |f|
  gz = Zlib::GzipReader.new(f)
  print gz.read
  gz.close
end

How should I ungzip a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

盗琴音 2024-08-10 01:58:59

上述方法对我来说不起作用。
我不断收到不正确的标头检查(Zlib::DataError)错误。显然,它假设您默认有一个标头,但情况可能并非总是如此。

我实施的解决方法是:

require 'zlib'
require 'stringio'
gz = Zlib::GzipReader.new(StringIO.new(resp.body.to_s))    
uncompressed_string = gz.read

The above method didn't work for me.
I kept getting incorrect header check (Zlib::DataError) error. Apparently it assumes you have a header by default, which may not always be the case.

The work around that I implemented was:

require 'zlib'
require 'stringio'
gz = Zlib::GzipReader.new(StringIO.new(resp.body.to_s))    
uncompressed_string = gz.read
方圜几里 2024-08-10 01:58:59

默认情况下,Zlib 假定您的压缩数据包含标头。
如果您的数据不包含标头,它将因引发 Zlib::DataError 而失败。

您可以通过以下解决方法告诉 Zlib 假设数据没有标头:

def inflate(string)
  zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

Zlib by default asumes that your compressed data contains a header.
If your data does NOT contain a header it will fail by raising a Zlib::DataError.

You can tell Zlib to assume the data has no header via the following workaround:

def inflate(string)
  zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end
玩物 2024-08-10 01:58:59

在 Rails 中,您可以使用:

  • ActiveSupport::Gzip.compress("my string")
  • ActiveSupport::Gzip.decompress()

In Rails you can use:

  • ActiveSupport::Gzip.compress("my string")
  • ActiveSupport::Gzip.decompress().
や莫失莫忘 2024-08-10 01:58:59

您需要 Zlib::Inflate 来解压缩字符串和 Zlib::Deflate 用于压缩

  def inflate(string)
    zstream = Zlib::Inflate.new
    buf = zstream.inflate(string)
    zstream.finish
    zstream.close
    buf
  end

You need Zlib::Inflate for decompression of a string and Zlib::Deflate for compression

  def inflate(string)
    zstream = Zlib::Inflate.new
    buf = zstream.inflate(string)
    zstream.finish
    zstream.close
    buf
  end
狼性发作 2024-08-10 01:58:59

zstream = Zlib::Inflate.new(16+Zlib::MAX_WBITS)

zstream = Zlib::Inflate.new(16+Zlib::MAX_WBITS)

り繁华旳梦境 2024-08-10 01:58:59

使用(-Zlib::MAX_WBITS),我得到错误:无效的代码长度设置错误:无效的块类型
以下唯一的内容也适用于我。

Zlib::GzipReader.new(StringIO.new(response_body)).read

Using (-Zlib::MAX_WBITS), I got ERROR: invalid code lengths set and ERROR: invalid block type
The only following works for me, too.

Zlib::GzipReader.new(StringIO.new(response_body)).read
心舞飞扬 2024-08-10 01:58:59

我使用上面的答案来使用 Zlib::Deflate

我不断收到损坏的文件(对于小文件),并且花了很多小时才发现可以使用以下方法修复问题:

buf = zstream.deflate(string,Zlib::FINISH)

没有 zstream.finish 行!

def self.deflate(string)
    zstream = Zlib::Deflate.new
    buf = zstream.deflate(string,Zlib::FINISH)
    zstream.close
    buf
end

I used the answer above to use a Zlib::Deflate

I kept getting broken files (for small files) and it took many hours to figure out that the problem can be fixed using:

buf = zstream.deflate(string,Zlib::FINISH)

without the the zstream.finish line!

def self.deflate(string)
    zstream = Zlib::Deflate.new
    buf = zstream.deflate(string,Zlib::FINISH)
    zstream.close
    buf
end
淤浪 2024-08-10 01:58:59

要gunzip内容,请使用以下代码(在1.9.2上测试)

Zlib::GzipReader.new(StringIO.new(content), :external_encoding => content.encoding).read

注意编码问题

To gunzip content, use following code (tested on 1.9.2)

Zlib::GzipReader.new(StringIO.new(content), :external_encoding => content.encoding).read

Beware of encoding problems

末が日狂欢 2024-08-10 01:58:59

现在我们不需要任何额外的参数。有 deflateinflate 类方法允许像这样的快速单行代码:

>> data = "Hello, Zlib!"
>> compressed = Zlib::Deflate.deflate(data)
=> "x\234\363H\315\311\311\327Q\210\312\311LR\004\000\032\305\003\363"
>> uncompressed = Zlib::Inflate.inflate(compressed)
=> "Hello, Zlib!"

我认为它回答了“我应该如何解压缩字符串?”的问题。最好的。 :)

We don't need any extra parameters these days. There are deflate and inflate class methods which allow for quick oneliners like these:

>> data = "Hello, Zlib!"
>> compressed = Zlib::Deflate.deflate(data)
=> "x\234\363H\315\311\311\327Q\210\312\311LR\004\000\032\305\003\363"
>> uncompressed = Zlib::Inflate.inflate(compressed)
=> "Hello, Zlib!"

I think it answers the question "How should I ungzip a string?" the best. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文