Class: LZMA

Inherits:
Object
  • Object
show all
Defined in:
lib/lzma.rb

Defined Under Namespace

Modules: C Classes: Stream

Class Method Summary collapse

Class Method Details

.decompress(what, buf_len = 4096, &blk) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/lzma.rb', line 158

def self.decompress(what, buf_len=4096, &blk)
  what = StringIO.new(what.to_s) unless what.is_a?(IO)
  res = ''
  blk = lambda {|chunk| res << chunk } unless block_given?

  stream = Stream.new(what, buf_len).decoder

  until what.eof?
    stream.read
    action = what.eof? ? :FINISH : :RUN

    begin
      code = nil
      raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))

      blk.call(stream.next_out)
    end while stream.avail_out.zero?
  end

  stream.finalize
  what.close

  block_given? ? what : res
end

.extract(file, to = file.gsub(/\.(xz|lzma)$/, '')) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/lzma.rb', line 183

def self.extract(file, to=file.gsub(/\.(xz|lzma)$/, ''))
  File.unlink(to) if File.file?(to)
  File.open(to, 'wb') {|f|
    decompress(File.open(file)) {|chunk|
      f.write(chunk)
    }
  }

  to
end