Class: Zlib::GzipReader

Inherits:
GzipFile show all
Defined in:
lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb

Constant Summary collapse

OSES =
['FAT filesystem',
'Amiga',
'VMS (or OpenVMS)',
'Unix',
'VM/CMS',
'Atari TOS',
'HPFS fileystem (OS/2, NT)',
'Macintosh',
'Z-System',
'CP/M',
'TOPS-20',
'NTFS filesystem (NT)',
'QDOS',
'Acorn RISCOS',
'unknown']

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GzipFile

#close

Constructor Details

#initialize(io) ⇒ GzipReader

Returns a new instance of GzipReader.



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 522

def initialize io

  #Add a helper method to check bits
  ::Fixnum.module_eval do
    def isbitset? bit_to_check
    if self & (2 ** bit_to_check)  == (2 ** bit_to_check) then true else false end
  end
  end

  super()
  @io = io
  io.read.each_byte {|b| @input_buffer << b}
  if @input_buffer[@in_pos+=1] != 0x1f || @input_buffer[@in_pos+=1] != 0x8b then raise Zlib::GzipFile::Error.new("not in gzip format") end
  if @input_buffer[@in_pos+=1] != 0x08 then raise Zlib::GzipFile::Error.new("unknown compression method") end
  flg = @input_buffer[@in_pos+=1]
  @ftext = flg.isbitset? 0
  @fhcrc = flg.isbitset? 1
  @fextra = flg.isbitset? 2
  @fname = flg.isbitset? 3
  @fcomment = flg.isbitset? 4
  @mtime = Time.at(@input_buffer[@in_pos+=1] | (@input_buffer[@in_pos+=1] << 8) | (@input_buffer[@in_pos+=1] << 16) | (@input_buffer[@in_pos+=1] << 24))
  @xfl = @input_buffer[@in_pos+=1]
  @os = OSES[@input_buffer[@in_pos+=1]]
  if @fextra then
    @xlen = (@input_buffer[@in_pos+=1] | (@input_buffer[@in_pos+=1] << 8))
    @xtra_field = []
    @xlen.times {@xtra_field << @input_buffer[@in_pos+=1]}
  end
  if @fname then
    @original_name = ""
    until @original_name["\0"].nil? == false
      @original_name.concat(@input_buffer[@in_pos+=1])
    end
    @original_name.chop!
  end
  if @fcomment then
    @comment = ""
    until @comment["\0"].nil? == false
      @comment.concat(@input_buffer[@in_pos+=1])
    end
    @comment.chop!
  end
  if @fhcrc then
    @header_crc = @input_buffer[@in_pos+=1] | (@input_buffer[@in_pos+=1] << 8)
  end
  @contents = ""
  until @in_pos == @input_buffer.length-1
    @contents.concat(@input_buffer[@in_pos+=1])
  end

  #we do raw deflate, no headers
  @zstream = Zlib::Inflate.new -MAX_WBITS
  @inflated = StringIO.new(@zstream.inflate @contents)

end

Class Method Details

.open(filename) ⇒ Object



597
598
599
600
601
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 597

def open filename
  io = File.open filename
  gz = self.new io
  if block_given? then yield gz else gz end
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


582
583
584
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 582

def eof?
  @inflated.eof?
end

#posObject



586
587
588
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 586

def pos
  @inflated.pos
end

#read(length = nil) ⇒ Object



578
579
580
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 578

def read length=nil
  @inflated.read length
end

#rewindObject



590
591
592
593
# File 'lib/motion-bundler/mocks/zliby-0.0.5/zlib.rb', line 590

def rewind
  @inflated.rewind
  @io.seek 0, IO::SEEK_SET
end