Skip to content

dearblue/ruby-extzstd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extzstd - ruby bindings for Zstd (Zstandard)

This is unofficial ruby bindings for the data compression library Zstd (Zstandard).

"extzstd" is supported decompression with the legacy formats (zstd-0.1 - 0.7).

HOW TO USE

basic usage (simply encode/decode)

# First, load library
require "extzstd"

# Prepair data
source = "sample data..." * 50

# simply compression
encdata = Zstd.encode(source)
puts "encdata.bytesize=#{encdata.bytesize}"

# simply decompression
decdata = Zstd.decode(encdata)
puts "decdata.bytesize=#{decdata.bytesize}"

# Comparison source and decoded data
p source == decdata # => true

Streaming compression (with block)

outport = StringIO.new("")
Zstd.encode(outport) do |encoder|
  encoder.write "abcdefg\n"
  encoder << "hijklmn\n"
  encoder.write "opqrstu\n"
  encoder << "vwxyz\n"
end

Streaming compression (without block and write to file)

file = File.open("sample.zst", "wb")
encoder = Zstd.encode(file)

encoder.write "abcdefg\n"
encoder << "hijklmn\n"
encoder.write "opqrstu\n"
encoder << "vwxyz\n"

encoder.close
file.close

Streaming decompression (with block and read from file)

File.open("sample.zst", "rb") do |file|
  Zstd.decode(file) do |decoder|
    p decoder.read(8)  # => "abcdefg\n"
    p decoder.read(1)  # => "h"
    p decoder.read(2)  # => "ij"
    p decoder.read     # => "klmn\nopqrstu\nvwxyz\n"
  end
end

Support Ractor (Ruby3 feature)

Ruby3 の Ractor に対応しています。

require "extzstd"

using Zstd

p Ractor.new {
  Ractor.yield ("abcdefg" * 9).to_zstd, move: true
}.take

Specification