Module: ZstdRuby

Defined in:
lib/zstd_ruby.rb

Constant Summary collapse

DEFAULT_COMPRESSION_LEVEL =

Default compression level

3
MIN_COMPRESSION_LEVEL =

Valid compression level range

1
MAX_COMPRESSION_LEVEL =
22

Class Method Summary collapse

Class Method Details

.compress_file(input_path, output_path, level: DEFAULT_COMPRESSION_LEVEL) ⇒ Boolean

Compress a file using zstd compression

Examples:

ZstdRuby.compress_file('input.txt', 'output.txt.zst')
ZstdRuby.compress_file('input.txt', 'output.txt.zst', level: 9)

Parameters:

  • input_path (String)

    path to the input file to compress

  • output_path (String)

    path where the compressed file will be written

  • level (Integer) (defaults to: DEFAULT_COMPRESSION_LEVEL)

    compression level (1-22, default: 3)

    • Level 1: fastest compression, lower ratio

    • Level 3: default, good balance

    • Level 22: best compression, slowest

Returns:

  • (Boolean)

    true on success

Raises:

  • (ArgumentError)

    if compression level is invalid

  • (RuntimeError)

    if file operations fail



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zstd_ruby.rb', line 29

def compress_file(input_path, output_path, level: DEFAULT_COMPRESSION_LEVEL)
  # Validate compression level
  unless level.is_a?(Integer)
    raise ArgumentError, "Compression level must be an integer"
  end

  unless level >= MIN_COMPRESSION_LEVEL && level <= MAX_COMPRESSION_LEVEL
    raise ArgumentError,
          "Compression level must be between #{MIN_COMPRESSION_LEVEL} and #{MAX_COMPRESSION_LEVEL}"
  end

  # Convert paths to strings
  input_path = input_path.to_s
  output_path = output_path.to_s

  # Call native extension
  ZstdRubyNative.compress_file_native(input_path, output_path, level)
end

.decompress_file(input_path, output_path) ⇒ Boolean

Decompress a zstd-compressed file

Examples:

ZstdRuby.decompress_file('input.txt.zst', 'output.txt')

Parameters:

  • input_path (String)

    path to the compressed input file

  • output_path (String)

    path where the decompressed file will be written

Returns:

  • (Boolean)

    true on success

Raises:

  • (RuntimeError)

    if file operations or decompression fail



59
60
61
62
63
64
65
66
# File 'lib/zstd_ruby.rb', line 59

def decompress_file(input_path, output_path)
  # Convert paths to strings
  input_path = input_path.to_s
  output_path = output_path.to_s

  # Call native extension
  ZstdRubyNative.decompress_file_native(input_path, output_path)
end