Module: Omnizip::Formats::Bzip2File
- Defined in:
- lib/omnizip/formats/bzip2_file.rb
Overview
BZIP2 file format
BZIP2 is a file compression format that uses the BZip2 algorithm with block-sorting compression (Burrows-Wheeler Transform).
Format structure:
- Magic bytes: "BZh" (0x42 0x5A 0x68)
- Block size indicator: '1'-'9' (100KB - 900KB)
- Compressed blocks
- End-of-stream marker
The format supports multiple compressed blocks, each with its own CRC32 checksum for data integrity verification.
Constant Summary collapse
- BZIP2_MAGIC =
BZIP2 magic bytes
"BZh"- BLOCK_SIZE_MIN =
Block size markers ('1' to '9')
1- BLOCK_SIZE_MAX =
9- STREAM_MAGIC =
Stream markers
0x314159265359- BLOCK_MAGIC =
π in hex
0x177245385090- STREAM_END_MAGIC =
Block start marker
0x177245385090
Class Method Summary collapse
-
.compress(input_path, output_path, options = {}) ⇒ Object
Compress a file with BZIP2.
-
.compress_stream(input_io, output_io, options = {}) ⇒ Object
Compress data stream with BZIP2.
-
.decompress(input_path, output_path) ⇒ Object
Decompress a BZIP2 file.
-
.decompress_stream(input_io, output_io) ⇒ Object
Decompress BZIP2 stream.
-
.register! ⇒ Object
Register BZIP2 format when loaded.
Class Method Details
.compress(input_path, output_path, options = {}) ⇒ Object
Compress a file with BZIP2
40 41 42 43 44 45 46 |
# File 'lib/omnizip/formats/bzip2_file.rb', line 40 def compress(input_path, output_path, = {}) input_data = File.binread(input_path) File.open(output_path, "wb") do |output| compress_stream(StringIO.new(input_data), output, ) end end |
.compress_stream(input_io, output_io, options = {}) ⇒ Object
Compress data stream with BZIP2
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/omnizip/formats/bzip2_file.rb', line 65 def compress_stream(input_io, output_io, = {}) level = [:level] || 9 level = level.clamp(BLOCK_SIZE_MIN, BLOCK_SIZE_MAX) input_data = input_io.read # Write BZIP2 header write_header(output_io, level) # Get BZip2 algorithm bzip2 = AlgorithmRegistry.get(:bzip2).new # Compress data compressed_io = StringIO.new compressed_io.set_encoding(Encoding::BINARY) bzip2.compress( StringIO.new(input_data), compressed_io, (level), ) # Write compressed data compressed_io.rewind compressed_data = compressed_io.read output_io.write(compressed_data) end |
.decompress(input_path, output_path) ⇒ Object
Decompress a BZIP2 file
52 53 54 55 56 57 58 |
# File 'lib/omnizip/formats/bzip2_file.rb', line 52 def decompress(input_path, output_path) File.open(input_path, "rb") do |input| File.open(output_path, "wb") do |output| decompress_stream(input, output) end end end |
.decompress_stream(input_io, output_io) ⇒ Object
Decompress BZIP2 stream
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/omnizip/formats/bzip2_file.rb', line 97 def decompress_stream(input_io, output_io) # Read and verify header read_header(input_io) # Get BZip2 algorithm bzip2 = AlgorithmRegistry.get(:bzip2).new # Decompress data decompressed_io = StringIO.new decompressed_io.set_encoding(Encoding::BINARY) bzip2.decompress(input_io, decompressed_io) # Write decompressed data decompressed_io.rewind output_io.write(decompressed_io.read) end |
.register! ⇒ Object
Register BZIP2 format when loaded
116 117 118 119 |
# File 'lib/omnizip/formats/bzip2_file.rb', line 116 def register! FormatRegistry.register(".bz2", Omnizip::Formats::Bzip2File) FormatRegistry.register(".bzip2", Omnizip::Formats::Bzip2File) end |