Class: Zlib::Deflate

Inherits:
ZStream show all
Defined in:
lib/pr/zlib/deflate.rb

Instance Attribute Summary

Attributes inherited from ZStream

#buf, #flags, #func, #input, #stream

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZStream

#ZSTREAM_IS_CLOSING, #ZSTREAM_IS_FINISHED, #ZSTREAM_IS_READY, #ZSTREAM_READY, #raise_zlib_error, #zstream_append_buffer, #zstream_append_input, #zstream_buffer_ungetc, #zstream_detach_buffer, #zstream_detach_input, #zstream_discard_input, #zstream_end, #zstream_expand_buffer, #zstream_init, #zstream_passthrough_input, #zstream_reset, #zstream_reset_input, #zstream_run, #zstream_shift_buffer, #zstream_sync

Constructor Details

#initialize(level = Z_DEFAULT_COMPRESSION, wbits = MAX_WBITS, memlevel = DEF_MEM_LEVEL, strategy = Z_DEFAULT_STRATEGY) ⇒ Deflate

Returns a new instance of Deflate.



30
31
32
33
34
35
36
37
38
# File 'lib/pr/zlib/deflate.rb', line 30

def initialize(level = Z_DEFAULT_COMPRESSION, wbits = MAX_WBITS, memlevel = DEF_MEM_LEVEL, strategy = Z_DEFAULT_STRATEGY)
  @z = ZStream.new
  @z.zstream_init(DeflateFuncs)
  err = deflateInit2(@z.stream, level, Z_DEFLATED, wbits, memlevel, strategy)
  if err != Z_OK
    raise_zlib_error(err, @z.stream.msg)
  end
  @z.ZSTREAM_READY()
end

Class Method Details

.deflate(src, level = Z_DEFAULT_COMPRESSION) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pr/zlib/deflate.rb', line 13

def self.deflate(src, level = Z_DEFAULT_COMPRESSION)
  @z = ZStream.new
  @z.zstream_init(DeflateFuncs)
  err = deflateInit(@z.stream, level)
  if err != Z_OK
    raise_zlib_error(err, @z.stream.msg)
  end
  @z.ZSTREAM_READY()

  begin
    dst = deflate_run(src)
  ensure
    @z.zstream_end()
  end
  dst
end

.deflate_run(src) ⇒ Object



8
9
10
11
# File 'lib/pr/zlib/deflate.rb', line 8

def self.deflate_run(src)
  @z.zstream_run(src, src.length, Z_FINISH)
  @z.zstream_detach_buffer()
end

Instance Method Details

#<<(src) ⇒ Object



66
67
68
69
# File 'lib/pr/zlib/deflate.rb', line 66

def <<(src)
  do_deflate(src, Z_NO_FLUSH)
  self
end

#deflate(src, flush = Z_NO_FLUSH) ⇒ Object



61
62
63
64
# File 'lib/pr/zlib/deflate.rb', line 61

def deflate(src, flush = Z_NO_FLUSH)
  do_deflate(src, flush)
  @z.zstream_detach_buffer
end

#flush(v_flush) ⇒ Object



71
72
73
74
75
76
# File 'lib/pr/zlib/deflate.rb', line 71

def flush(v_flush)
  if v_flush != Z_NO_FLUSH
    @z.zstream_run('', 0, flush)
  end
  @z.zstream_detach_buffer()
end

#initialize_copy(orig) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/pr/zlib/deflate.rb', line 40

def initialize_copy(orig)
  z1 = @z
  z2 = orig.z
  err = deflateCopy(z1.stream, z2.stream)
  if err != Z_OK
    raise_zlib_error(err, 0)
  end
  z1.flags = z2.flags
end

#params(level = Z_DEFAULT_COMPRESSION, strategy = Z_DEFAULT_STRATEGY) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pr/zlib/deflate.rb', line 78

def params(level = Z_DEFAULT_COMPRESSION, strategy = Z_DEFAULT_STRATEGY)
  err = deflateParams(@z.stream, level, strategy)
  while err == Z_BUF_ERROR
    warn('deflateParams() returned Z_BUF_ERROR')
    @z.zstream_expand_buffer()
    err = deflateParams(@z.stream, level, strategy)
  end
  if err != Z_OK
    raise_zlib_error(err, @z.stream.msg)
  end

  nil
end

#set_dictionary(dic) ⇒ Object



92
93
94
95
96
97
# File 'lib/pr/zlib/deflate.rb', line 92

def set_dictionary(dic)
  err = deflateSetDictionary(@z.stream, dic, dic.length)
  if err != Z_OK
    raise_zlib_error(err, @z.stream.msg)
  end
end