Class: Zlib::Deflate

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

Instance Attribute Summary

Attributes inherited from ZStream

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZStream

#ZSTREAM_IS_CLOSING, #ZSTREAM_IS_FINISHED, #ZSTREAM_IS_READY, #ZSTREAM_READY, #adler, #avail_in, #avail_out, #avail_out=, #close, #closed?, #data_type, #finish, #finished?, #flush_next_in, #flush_next_out, #raise_zlib_error, #reset, #total_in, #total_out, #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.



528
529
530
531
532
533
534
535
536
# File 'lib/pr/zlib.rb', line 528

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



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/pr/zlib.rb', line 511

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



506
507
508
509
# File 'lib/pr/zlib.rb', line 506

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

Instance Method Details

#<<(src) ⇒ Object



564
565
566
567
# File 'lib/pr/zlib.rb', line 564

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

#deflate(src, flush = Z_NO_FLUSH) ⇒ Object



559
560
561
562
# File 'lib/pr/zlib.rb', line 559

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

#flush(v_flush) ⇒ Object



569
570
571
572
573
574
# File 'lib/pr/zlib.rb', line 569

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

#initialize_copy(orig) ⇒ Object



538
539
540
541
542
543
544
545
546
# File 'lib/pr/zlib.rb', line 538

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



576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/pr/zlib.rb', line 576

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



590
591
592
593
594
595
# File 'lib/pr/zlib.rb', line 590

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