comparison vendor/gems/coderay-0.9.7/lib/coderay/helpers/gzip_simple.rb @ 523:0b6c82dead28 luisf

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