summaryrefslogtreecommitdiffstats
path: root/vendor/plugins/coderay-0.7.6.227/lib/coderay/helpers/gzip_simple.rb
blob: 76aeb2274719f91530ebe406133575c00d9955a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# =GZip Simple
#
# A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
#
# Author: murphy (mail to murphy cYcnus de)
#
# Version: 0.2 (2005.may.28)
#
# ==Documentation
#
# See +GZip+ module and the +String+ extensions.
#
module GZip

  require 'zlib'

  # The default zipping level. 7 zips good and fast.
  DEFAULT_GZIP_LEVEL = 7

  # Unzips the given string +s+.
  #
  # Example:
  #   require 'gzip_simple'
  #   print GZip.gunzip(File.read('adresses.gz'))
  def GZip.gunzip s
    Zlib::Inflate.inflate s
  end

  # Zips the given string +s+.
  #
  # Example:
  #   require 'gzip_simple'
  #   File.open('adresses.gz', 'w') do |file
  #     file.write GZip.gzip('Mum: 0123 456 789', 9)
  #   end
  #
  # If you provide a +level+, you can control how strong
  # the string is compressed:
  # - 0: no compression, only convert to gzip format
  # - 1: compress fast
  # - 7: compress more, but still fast (default)
  # - 8: compress more, slower
  # - 9: compress best, very slow
  def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
    Zlib::Deflate.new(level).deflate s, Zlib::FINISH
  end
end


# String extensions to use the GZip module.
#
# The methods gzip and gunzip provide an even more simple
# interface to the ZLib:
#
#   # create a big string
#   x = 'a' * 1000
#   
#   # zip it
#   x_gz = x.gzip
#   
#   # test the result
#   puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
#   #-> Zipped 1000 bytes to 19 bytes.
#   
#   # unzipping works
#   p x_gz.gunzip == x  #-> true
class String
  # Returns the string, unzipped.
  # See GZip.gunzip
  def gunzip
    GZip.gunzip self
  end
  # Replaces the string with its unzipped value.
  # See GZip.gunzip
  def gunzip!
    replace gunzip
  end

  # Returns the string, zipped.
  # +level+ is the gzip compression level, see GZip.gzip.
  def gzip level = GZip::DEFAULT_GZIP_LEVEL
    GZip.gzip self, level
  end
  # Replaces the string with its zipped value.
  # See GZip.gzip.
  def gzip!(*args)
    replace gzip(*args)
  end
end

if $0 == __FILE__
  eval DATA.read, nil, $0, __LINE__+4
end

__END__
#CODE

# Testing / Benchmark
x = 'a' * 1000
x_gz = x.gzip
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]  #-> Zipped 1000 bytes to 19 bytes.
p x_gz.gunzip == x  #-> true

require 'benchmark'

INFO = 'packed to %0.3f%%'  # :nodoc:

x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
Benchmark.bm(10) do |bm|
  for level in 0..9
    bm.report "zip #{level}" do
      $x = x.gzip level
    end
    puts INFO % [100.0 * $x.size / x.size]
  end
  bm.report 'zip' do
    $x = x.gzip
  end
  puts INFO % [100.0 * $x.size / x.size]
  bm.report 'unzip' do
    $x.gunzip
  end
end