Class: Cabriolet::CAB::Extractor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(io_system, decompressor) ⇒ Extractor

Initialize a new extractor

Parameters:



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

#decompressorObject (readonly)

Returns the value of attribute decompressor.



9
10
11
# File 'lib/cabriolet/cab/extractor.rb', line 9

def decompressor
  @decompressor
end

#io_systemObject (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

Parameters:

  • cabinet (Models::Cabinet)

    Cabinet to extract from

  • output_dir (String)

    Directory to extract to

  • options (Hash)

    Extraction options

Options Hash (**options):

  • :preserve_paths (Boolean)

    Preserve directory structure (default: true)

  • :set_timestamps (Boolean)

    Set file modification times (default: true)

  • :progress (Proc)

    Progress callback

Returns:

  • (Integer)

    Number of files extracted



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, **options)
  preserve_paths = options.fetch(:preserve_paths, true)
  set_timestamps = options.fetch(:set_timestamps, true)
  progress = options[: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, **options)

    # Set timestamp if requested
    if set_timestamps && 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

Parameters:

  • file (Models::File)

    File to extract

  • output_path (String)

    Where to write the file

  • options (Hash)

    Extraction options

Options Hash (**options):

  • :salvage (Boolean)

    Enable salvage mode

Returns:

  • (Integer)

    Number of bytes extracted

Raises:



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, **options)
  salvage = options[: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