Class: Cabriolet::CAB::Extractor
- Inherits:
-
Object
- Object
- Cabriolet::CAB::Extractor
- Defined in:
- lib/cabriolet/cab/extractor.rb
Overview
Extractor handles the extraction of files from cabinets
Defined Under Namespace
Classes: BlockReader
Instance Attribute Summary collapse
-
#decompressor ⇒ Object
readonly
Returns the value of attribute decompressor.
-
#io_system ⇒ Object
readonly
Returns the value of attribute io_system.
Instance Method Summary collapse
-
#extract_all(cabinet, output_dir, **options) ⇒ Integer
Extract all files from a cabinet.
-
#extract_file(file, output_path, **options) ⇒ Integer
Extract a single file from the cabinet.
-
#initialize(io_system, decompressor) ⇒ Extractor
constructor
Initialize a new extractor.
Constructor Details
#initialize(io_system, decompressor) ⇒ Extractor
Initialize a new extractor
15 16 17 18 |
# File 'lib/cabriolet/cab/extractor.rb', line 15 def initialize(io_system, decompressor) @io_system = io_system @decompressor = decompressor end |
Instance Attribute Details
#decompressor ⇒ Object (readonly)
Returns the value of attribute decompressor.
9 10 11 |
# File 'lib/cabriolet/cab/extractor.rb', line 9 def decompressor @decompressor end |
#io_system ⇒ Object (readonly)
Returns the value of attribute io_system.
9 10 11 |
# File 'lib/cabriolet/cab/extractor.rb', line 9 def io_system @io_system end |
Instance Method Details
#extract_all(cabinet, output_dir, **options) ⇒ Integer
Extract all files from a cabinet
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cabriolet/cab/extractor.rb', line 112 def extract_all(cabinet, output_dir, **) preserve_paths = .fetch(:preserve_paths, true) = .fetch(:set_timestamps, true) progress = [:progress] # Create output directory FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir) count = 0 cabinet.files.each do |file| # Determine output path output_path = if preserve_paths ::File.join(output_dir, file.filename) else ::File.join(output_dir, ::File.basename(file.filename)) end # Extract file extract_file(file, output_path, **) # Set timestamp if requested if && file.modification_time ::File.utime(file.modification_time, file.modification_time, output_path) end # Set file permissions based on attributes set_file_attributes(output_path, file) count += 1 progress&.call(file, count, cabinet.files.size) end count end |
#extract_file(file, output_path, **options) ⇒ Integer
Extract a single file from the cabinet
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 63 64 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 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cabriolet/cab/extractor.rb', line 27 def extract_file(file, output_path, **) salvage = [:salvage] || @decompressor.salvage folder = file.folder # Validate file raise Cabriolet::ArgumentError, "File has no folder" unless folder if file.offset > Constants::LENGTH_MAX raise DecompressionError, "File offset beyond 2GB limit" end # Check file length filelen = file.length if filelen > (Constants::LENGTH_MAX - file.offset) unless salvage raise DecompressionError, "File length exceeds 2GB limit" end filelen = Constants::LENGTH_MAX - file.offset end # Check for merge requirements if folder.needs_prev_merge? raise DecompressionError, "File requires previous cabinet, cabinet set is incomplete" end # Check file fits within folder unless salvage max_len = folder.num_blocks * Constants::BLOCK_MAX if file.offset > max_len || filelen > (max_len - file.offset) raise DecompressionError, "File extends beyond folder data" end end # Create output directory if needed output_dir = ::File.dirname(output_path) FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir) # Create input wrapper that reads CFDATA blocks across cabinets input_handle = BlockReader.new(@io_system, folder.data, folder.num_blocks, salvage) begin # Create output file output_fh = @io_system.open(output_path, Constants::MODE_WRITE) begin # Create decompressor decomp = @decompressor.create_decompressor(folder, input_handle, output_fh) # Skip to file offset if needed if file.offset.positive? # Decompress and discard bytes before file start temp_output = System::MemoryHandle.new("", Constants::MODE_WRITE) temp_decomp = @decompressor.create_decompressor(folder, input_handle, temp_output) temp_decomp.decompress(file.offset) end # Decompress the file decomp.decompress(filelen) filelen ensure output_fh.close end ensure input_handle.close end end |