Module: Omnizip::Formats::SevenZip::EncodedHeader
- Includes:
- Constants
- Defined in:
- lib/omnizip/formats/seven_zip/encoded_header.rb
Overview
Handles 7-Zip encoded headers
7-Zip can compress the Next Header metadata with LZMA2 to save space. This module provides functionality to encode and decode headers.
Constant Summary
Constants included from Constants
Constants::MAJOR_VERSION, Constants::MAX_NUM_BONDS, Constants::MAX_NUM_CODERS, Constants::MAX_NUM_PACK_STREAMS, Constants::MINOR_VERSION, Constants::SIGNATURE, Constants::SIGNATURE_SIZE, Constants::START_HEADER_SIZE
Class Method Summary collapse
-
.compress_header(header_data) ⇒ String
Compress header data with LZMA2.
-
.encode(header_data) ⇒ String
Encode next header with LZMA2 compression.
-
.encode_uint64(value) ⇒ String
Encode unsigned 64-bit integer in 7-Zip variable-length format.
Class Method Details
.compress_header(header_data) ⇒ String
Compress header data with LZMA2
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/omnizip/formats/seven_zip/encoded_header.rb', line 68 def compress_header(header_data) # Use 7-Zip SDK LZMA2 encoder for 7-Zip format encoder = Omnizip::Implementations::SevenZip::LZMA2::Encoder.new( dict_size: 4096, # Small dictionary for headers lc: 3, lp: 0, pb: 2, standalone: false, # No property byte ) encoder.encode(header_data) end |
.encode(header_data) ⇒ String
Encode next header with LZMA2 compression
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/omnizip/formats/seven_zip/encoded_header.rb', line 19 def encode(header_data) # Compress header with LZMA2 compressed = compress_header(header_data) # Build encoded header property encoded = String.new(encoding: "BINARY") encoded << [PropertyId::ENCODED_HEADER].pack("C") # Write pack info for compressed header encoded << [PropertyId::PACK_INFO].pack("C") encoded << encode_uint64(0) # Pack position encoded << encode_uint64(1) # Number of pack streams # Write size encoded << [PropertyId::SIZE].pack("C") encoded << encode_uint64(compressed.bytesize) encoded << [PropertyId::K_END].pack("C") # Write coder info (LZMA2) encoded << [PropertyId::UNPACK_INFO].pack("C") encoded << [PropertyId::FOLDER].pack("C") encoded << encode_uint64(1) # Number of folders encoded << [0].pack("C") # External flag (0 = inline) # Number of coders encoded << encode_uint64(1) # Coder info for LZMA2 # Method ID: LZMA2 = 0x21 encoded << [1].pack("C") # Main byte (1 byte for ID, no properties) encoded << [0x21].pack("C") # LZMA2 method ID # Unpack size encoded << [PropertyId::CODERS_UNPACK_SIZE].pack("C") encoded << encode_uint64(header_data.bytesize) encoded << [PropertyId::K_END].pack("C") # Append compressed data encoded << compressed encoded end |
.encode_uint64(value) ⇒ String
Encode unsigned 64-bit integer in 7-Zip variable-length format
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/omnizip/formats/seven_zip/encoded_header.rb', line 85 def encode_uint64(value) return [value].pack("C") if value < 0x80 result = String.new(encoding: "BINARY") shift = 0 loop do byte = value & 0x7F value >>= 7 if value.zero? result << [byte].pack("C") break else result << [byte | 0x80].pack("C") end shift += 7 end result end |