Chris@0: # =GZip Simple Chris@0: # Chris@0: # A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.) Chris@0: # Chris@0: # Author: murphy (mail to murphy rubychan de) Chris@0: # Chris@0: # Version: 0.2 (2005.may.28) Chris@0: # Chris@0: # ==Documentation Chris@0: # Chris@0: # See +GZip+ module and the +String+ extensions. Chris@0: # Chris@0: module GZip Chris@0: Chris@0: require 'zlib' Chris@0: Chris@0: # The default zipping level. 7 zips good and fast. Chris@0: DEFAULT_GZIP_LEVEL = 7 Chris@0: Chris@0: # Unzips the given string +s+. Chris@0: # Chris@0: # Example: Chris@0: # require 'gzip_simple' Chris@0: # print GZip.gunzip(File.read('adresses.gz')) Chris@0: def GZip.gunzip s Chris@0: Zlib::Inflate.inflate s Chris@0: end Chris@0: Chris@0: # Zips the given string +s+. Chris@0: # Chris@0: # Example: Chris@0: # require 'gzip_simple' Chris@0: # File.open('adresses.gz', 'w') do |file Chris@0: # file.write GZip.gzip('Mum: 0123 456 789', 9) Chris@0: # end Chris@0: # Chris@0: # If you provide a +level+, you can control how strong Chris@0: # the string is compressed: Chris@0: # - 0: no compression, only convert to gzip format Chris@0: # - 1: compress fast Chris@0: # - 7: compress more, but still fast (default) Chris@0: # - 8: compress more, slower Chris@0: # - 9: compress best, very slow Chris@0: def GZip.gzip s, level = DEFAULT_GZIP_LEVEL Chris@0: Zlib::Deflate.new(level).deflate s, Zlib::FINISH Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: # String extensions to use the GZip module. Chris@0: # Chris@0: # The methods gzip and gunzip provide an even more simple Chris@0: # interface to the ZLib: Chris@0: # Chris@0: # # create a big string Chris@0: # x = 'a' * 1000 Chris@0: # Chris@0: # # zip it Chris@0: # x_gz = x.gzip Chris@0: # Chris@0: # # test the result Chris@0: # puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] Chris@0: # #-> Zipped 1000 bytes to 19 bytes. Chris@0: # Chris@0: # # unzipping works Chris@0: # p x_gz.gunzip == x #-> true Chris@0: class String Chris@0: # Returns the string, unzipped. Chris@0: # See GZip.gunzip Chris@0: def gunzip Chris@0: GZip.gunzip self Chris@0: end Chris@0: # Replaces the string with its unzipped value. Chris@0: # See GZip.gunzip Chris@0: def gunzip! Chris@0: replace gunzip Chris@0: end Chris@0: Chris@0: # Returns the string, zipped. Chris@0: # +level+ is the gzip compression level, see GZip.gzip. Chris@0: def gzip level = GZip::DEFAULT_GZIP_LEVEL Chris@0: GZip.gzip self, level Chris@0: end Chris@0: # Replaces the string with its zipped value. Chris@0: # See GZip.gzip. Chris@0: def gzip!(*args) Chris@0: replace gzip(*args) Chris@0: end Chris@0: end Chris@0: Chris@0: if $0 == __FILE__ Chris@0: eval DATA.read, nil, $0, __LINE__+4 Chris@0: end Chris@0: Chris@0: __END__ Chris@0: #CODE Chris@0: Chris@0: # Testing / Benchmark Chris@0: x = 'a' * 1000 Chris@0: x_gz = x.gzip Chris@0: puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes. Chris@0: p x_gz.gunzip == x #-> true Chris@0: Chris@0: require 'benchmark' Chris@0: Chris@0: INFO = 'packed to %0.3f%%' # :nodoc: Chris@0: Chris@0: x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join Chris@0: Benchmark.bm(10) do |bm| Chris@0: for level in 0..9 Chris@0: bm.report "zip #{level}" do Chris@0: $x = x.gzip level Chris@0: end Chris@0: puts INFO % [100.0 * $x.size / x.size] Chris@0: end Chris@0: bm.report 'zip' do Chris@0: $x = x.gzip Chris@0: end Chris@0: puts INFO % [100.0 * $x.size / x.size] Chris@0: bm.report 'unzip' do Chris@0: $x.gunzip Chris@0: end Chris@0: end