class Zlib::GzipFile

Constants

GZFILE_FLAG_HEADER_FINISHED
GZFILE_FLAG_SYNC
GZFILE_READ_SIZE
GZ_EXTRAFLAG_FAST
GZ_EXTRAFLAG_SLOW
GZ_FLAG_COMMENT
GZ_FLAG_ENCRYPT
GZ_FLAG_EXTRA
GZ_FLAG_MULTIPART
GZ_FLAG_ORIG_NAME
GZ_FLAG_UNKNOWN_MASK
GZ_MAGIC1
GZ_MAGIC2
GZ_METHOD_DEFLATE
Gzfile
OS_CODE

Public Class Methods

gzfile_s_open(filename,mode,level,strategy,&blk) click to toggle source
# File lib/pr/zlib.rb, line 850
def self.gzfile_s_open(filename,mode,level,strategy,&blk)
  io = File.open(filename,mode)
  self.wrap(io,level,strategy,&blk)
end
wrap(io, level=Z_DEFAULT_COMPRESSION, strategy=Z_DEFAULT_STRATEGY) { |obj| ... } click to toggle source
# File lib/pr/zlib.rb, line 771
def self.wrap(io, level=Z_DEFAULT_COMPRESSION, strategy=Z_DEFAULT_STRATEGY)
  obj = new(io,level,strategy)
  if block_given?
    begin
      yield(obj)
    ensure
      obj.gzfile_ensure_close()
    end
  else
    return obj
  end
end

Public Instance Methods

GZFILE_IS_FINISHED(gz) click to toggle source
# File lib/pr/zlib.rb, line 731
def GZFILE_IS_FINISHED(gz)
  gz.z.ZSTREAM_IS_FINISHED() && (gz.z.buf.nil? || gz.z.buf.offset.zero?)
end
close() click to toggle source
# File lib/pr/zlib.rb, line 819
def close
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  gzfile_close(true)
  @gz.io
end
closed?() click to toggle source
# File lib/pr/zlib.rb, line 831
def closed?
  @gz.io.nil?
end
comment() click to toggle source
# File lib/pr/zlib.rb, line 814
def comment
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.comment ? @gz.comment.dup : nil
end
crc() click to toggle source
# File lib/pr/zlib.rb, line 789
def crc
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.crc
end
finish() click to toggle source
# File lib/pr/zlib.rb, line 825
def finish
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  gzfile_close(false)
  @gz.io
end
gzfile_close(closeflag) click to toggle source
# File lib/pr/zlib.rb, line 751
def gzfile_close(closeflag)
  io = @gz.io
  send(@gz.end)

  @gz.io = nil
  @gz.orig_name = nil
  @gz.comment = nil

  if closeflag && defined?(io.close)
    io.close
  end
end
gzfile_ensure_close() click to toggle source
# File lib/pr/zlib.rb, line 764
def gzfile_ensure_close()
  if @gz.z.ZSTREAM_IS_READY()
    gzfile_close(true)
  end
  nil
end
level() click to toggle source
# File lib/pr/zlib.rb, line 799
def level
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.level
end
mtime() click to toggle source
# File lib/pr/zlib.rb, line 794
def mtime
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  Time.at(@gz.mtime)
end
orig_name() click to toggle source
# File lib/pr/zlib.rb, line 809
def orig_name
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.orig_name ? @gz.orig_name.dup : nil
end
os_code() click to toggle source
# File lib/pr/zlib.rb, line 804
def os_code
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.os_code
end
sync() click to toggle source
# File lib/pr/zlib.rb, line 835
def sync
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  !(@gz.z.flags & GZFILE_FLAG_SYNC).zero?
end
sync=(mode) click to toggle source
# File lib/pr/zlib.rb, line 840
def sync=(mode)
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  if(mode)
    @gz.z.flags |= GZFILE_FLAG_SYNC
  else
    @gz.z.flags &= ~GZFILE_FLAG_SYNC
  end
  mode
end
to_io() click to toggle source
# File lib/pr/zlib.rb, line 784
def to_io
  raise GzipFile::Error, "closed gzip stream" unless @gz.z.ZSTREAM_IS_READY()
  @gz.io
end

Private Instance Methods

gzfile_calc_crc(str) click to toggle source
# File lib/pr/zlib.rb, line 1021
def gzfile_calc_crc(str)
  if (str.length <= @gz.ungetc)
    @gz.ungetc -= str.length
  else
    @gz.crc = crc32(@gz.crc, str[@gz.ungetc,str.length - @gz.ungetc],
     str.length - @gz.ungetc)
    @gz.ungetc = 0
  end
end
gzfile_get16(src) click to toggle source
# File lib/pr/zlib.rb, line 881
def gzfile_get16(src)
  src.unpack('v').first
end
gzfile_get32(src) click to toggle source
# File lib/pr/zlib.rb, line 885
def gzfile_get32(src)
  src.unpack('V').first
end
gzfile_make_header() click to toggle source
# File lib/pr/zlib.rb, line 893
def gzfile_make_header
  buf = 0.chr * 10
  flags = 0
  extraflags = 0
  if @gz.orig_name
    flags |= GZ_FLAG_ORIG_NAME
  end
  if @gz.comment
    flags |= GZ_FLAG_COMMENT
  end
  if @gz.mtime.zero?
    @gz.mtime = Time.now.to_i
  end
  if (@gz.level == Z_BEST_SPEED)
    extraflags |= GZ_EXTRAFLAG_FAST
  elsif (@gz.level == Z_BEST_COMPRESSION)
    extraflags |= GZ_EXTRAFLAG_SLOW
  end
  buf[0] = GZ_MAGIC1.chr
  buf[1] = GZ_MAGIC2.chr
  buf[2] = GZ_METHOD_DEFLATE.chr
  buf[3] = flags.chr
  buf[4,4] = gzfile_set32(@gz.mtime)
  buf[8] = extraflags.chr
  buf[9] = @gz.os_code.chr
  @gz.z.zstream_append_buffer(buf,buf.length)

  if @gz.orig_name
    @gz.z.zstream_append_buffer(@gz.orig_name,@gz.orig_name.length)
    @gz.z.zstream_append_buffer("\0", 1)
  end
  if @gz.comment
    @gz.z.zstream_append_buffer(@gz.comment,@gz.comment.length)
    @gz.z.zstream_append_buffer("\0", 1)
  end

  @gz.z.flags |= GZFILE_FLAG_HEADER_FINISHED
end
gzfile_new(funcs,endfunc) click to toggle source
# File lib/pr/zlib.rb, line 857
def gzfile_new(funcs,endfunc)
  @gz = Gzfile.new
  @gz.z = ZStream.new
  @gz.z.zstream_init(funcs)
  @gz.io = nil
  @gz.level = 0
  @gz.mtime = 0
  @gz.os_code = OS_CODE
  @gz.orig_name = nil
  @gz.comment = nil
  @gz.crc = crc32(0,nil,0)
  @gz.lineno = 0
  @gz.ungetc = 0
  @gz.end = endfunc
  self
end
gzfile_read_header() click to toggle source
# File lib/pr/zlib.rb, line 940
def gzfile_read_header()
  if !gzfile_read_raw_ensure(10)
   raise GzipFile::Error, "not in gzip format"
  end

  head = @gz.z.input

  if (head[0].ord != GZ_MAGIC1 || head[1].ord != GZ_MAGIC2)
     raise GzipFile::Error, "not in gzip format"
  end
  if (head[2].ord != GZ_METHOD_DEFLATE)
     raise GzipFile::Error, "unsupported compression method #{head[2].ord}"
  end

  flags = head[3].ord
  if (flags & GZ_FLAG_MULTIPART).nonzero?
     raise GzipFile::Error, "multi-part gzip file is not supported"
  elsif (flags & GZ_FLAG_ENCRYPT).nonzero?
     raise GzipFile::Error, "encrypted gzip file is not supported"
  elsif (flags & GZ_FLAG_UNKNOWN_MASK).nonzero?
     raise GzipFile::Error, "unknown flags 0x%02x" % flags
  end

  if (head[8].ord & GZ_EXTRAFLAG_FAST).nonzero?
     @gz.level = Z_BEST_SPEED
  elsif (head[8].ord & GZ_EXTRAFLAG_SLOW).nonzero?
     @gz.level = Z_BEST_COMPRESSION
  else
     @gz.level = Z_DEFAULT_COMPRESSION
  end

  @gz.mtime = gzfile_get32(head[4,4])
  @gz.os_code = head[9].ord
  @gz.z.zstream_discard_input(10)

  if (flags & GZ_FLAG_EXTRA).nonzero?
     if !gzfile_read_raw_ensure(2)
       raise GzipFile::Error, "unexpected end of file"
     end
     len = gzfile_get16(@gz.z.input)
     if !gzfile_read_raw_ensure(2 + len)
       raise GzipFile::Error, "unexpected end of file"
     end
     @gz.z.zstream_discard_input(2 + len)
  end
  if (flags & GZ_FLAG_ORIG_NAME).nonzero?
     ap = gzfile_read_raw_until_zero(0)
     len = ap
     @gz.orig_name = @gz.z.input[0,len]
     @gz.z.zstream_discard_input(len + 1)
  end
  if (flags & GZ_FLAG_COMMENT).nonzero?
     ap = gzfile_read_raw_until_zero(0)
     len = ap
     @gz.comment = @gz.z.input[0,len]
     @gz.z.zstream_discard_input(len + 1)
  end

  if (@gz.z.input && @gz.z.input.length > 0)
    @gz.z.zstream_run(0, 0, Z_SYNC_FLUSH)
  end
end
gzfile_reset() click to toggle source
# File lib/pr/zlib.rb, line 874
def gzfile_reset()
  @gz.z.zstream_reset
  @gz.crc = crc32(0,nil,0)
  @gz.lineno = 0
  @gz.ungetc = 0
end
gzfile_set32(n) click to toggle source
# File lib/pr/zlib.rb, line 889
def gzfile_set32(n)
  [n].pack('V')
end