annotate vendor/plugins/coderay-0.9.2/lib/coderay/helpers/gzip_simple.rb @ 863:818ff422eece bug_168

Close obsolete branch bug_168
author Chris Cannam
date Tue, 07 Jun 2011 10:56:57 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 # =GZip Simple
Chris@0 2 #
Chris@0 3 # A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
Chris@0 4 #
Chris@0 5 # Author: murphy (mail to murphy rubychan de)
Chris@0 6 #
Chris@0 7 # Version: 0.2 (2005.may.28)
Chris@0 8 #
Chris@0 9 # ==Documentation
Chris@0 10 #
Chris@0 11 # See +GZip+ module and the +String+ extensions.
Chris@0 12 #
Chris@0 13 module GZip
Chris@0 14
Chris@0 15 require 'zlib'
Chris@0 16
Chris@0 17 # The default zipping level. 7 zips good and fast.
Chris@0 18 DEFAULT_GZIP_LEVEL = 7
Chris@0 19
Chris@0 20 # Unzips the given string +s+.
Chris@0 21 #
Chris@0 22 # Example:
Chris@0 23 # require 'gzip_simple'
Chris@0 24 # print GZip.gunzip(File.read('adresses.gz'))
Chris@0 25 def GZip.gunzip s
Chris@0 26 Zlib::Inflate.inflate s
Chris@0 27 end
Chris@0 28
Chris@0 29 # Zips the given string +s+.
Chris@0 30 #
Chris@0 31 # Example:
Chris@0 32 # require 'gzip_simple'
Chris@0 33 # File.open('adresses.gz', 'w') do |file
Chris@0 34 # file.write GZip.gzip('Mum: 0123 456 789', 9)
Chris@0 35 # end
Chris@0 36 #
Chris@0 37 # If you provide a +level+, you can control how strong
Chris@0 38 # the string is compressed:
Chris@0 39 # - 0: no compression, only convert to gzip format
Chris@0 40 # - 1: compress fast
Chris@0 41 # - 7: compress more, but still fast (default)
Chris@0 42 # - 8: compress more, slower
Chris@0 43 # - 9: compress best, very slow
Chris@0 44 def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
Chris@0 45 Zlib::Deflate.new(level).deflate s, Zlib::FINISH
Chris@0 46 end
Chris@0 47 end
Chris@0 48
Chris@0 49
Chris@0 50 # String extensions to use the GZip module.
Chris@0 51 #
Chris@0 52 # The methods gzip and gunzip provide an even more simple
Chris@0 53 # interface to the ZLib:
Chris@0 54 #
Chris@0 55 # # create a big string
Chris@0 56 # x = 'a' * 1000
Chris@0 57 #
Chris@0 58 # # zip it
Chris@0 59 # x_gz = x.gzip
Chris@0 60 #
Chris@0 61 # # test the result
Chris@0 62 # puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
Chris@0 63 # #-> Zipped 1000 bytes to 19 bytes.
Chris@0 64 #
Chris@0 65 # # unzipping works
Chris@0 66 # p x_gz.gunzip == x #-> true
Chris@0 67 class String
Chris@0 68 # Returns the string, unzipped.
Chris@0 69 # See GZip.gunzip
Chris@0 70 def gunzip
Chris@0 71 GZip.gunzip self
Chris@0 72 end
Chris@0 73 # Replaces the string with its unzipped value.
Chris@0 74 # See GZip.gunzip
Chris@0 75 def gunzip!
Chris@0 76 replace gunzip
Chris@0 77 end
Chris@0 78
Chris@0 79 # Returns the string, zipped.
Chris@0 80 # +level+ is the gzip compression level, see GZip.gzip.
Chris@0 81 def gzip level = GZip::DEFAULT_GZIP_LEVEL
Chris@0 82 GZip.gzip self, level
Chris@0 83 end
Chris@0 84 # Replaces the string with its zipped value.
Chris@0 85 # See GZip.gzip.
Chris@0 86 def gzip!(*args)
Chris@0 87 replace gzip(*args)
Chris@0 88 end
Chris@0 89 end
Chris@0 90
Chris@0 91 if $0 == __FILE__
Chris@0 92 eval DATA.read, nil, $0, __LINE__+4
Chris@0 93 end
Chris@0 94
Chris@0 95 __END__
Chris@0 96 #CODE
Chris@0 97
Chris@0 98 # Testing / Benchmark
Chris@0 99 x = 'a' * 1000
Chris@0 100 x_gz = x.gzip
Chris@0 101 puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
Chris@0 102 p x_gz.gunzip == x #-> true
Chris@0 103
Chris@0 104 require 'benchmark'
Chris@0 105
Chris@0 106 INFO = 'packed to %0.3f%%' # :nodoc:
Chris@0 107
Chris@0 108 x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
Chris@0 109 Benchmark.bm(10) do |bm|
Chris@0 110 for level in 0..9
Chris@0 111 bm.report "zip #{level}" do
Chris@0 112 $x = x.gzip level
Chris@0 113 end
Chris@0 114 puts INFO % [100.0 * $x.size / x.size]
Chris@0 115 end
Chris@0 116 bm.report 'zip' do
Chris@0 117 $x = x.gzip
Chris@0 118 end
Chris@0 119 puts INFO % [100.0 * $x.size / x.size]
Chris@0 120 bm.report 'unzip' do
Chris@0 121 $x.gunzip
Chris@0 122 end
Chris@0 123 end