Package Peach :: Package Transformers :: Module compress
[hide private]

Source Code for Module Peach.Transformers.compress

  1   
  2  ''' 
  3  Some default compression transforms (gzip, compress, etc). 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Transformers.compress-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2005-2008 Michael Eddington 
 11  # Copyright (c) 2004-2005 IOActive Inc. 
 12  # 
 13  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 14  # of this software and associated documentation files (the "Software"), to deal 
 15  # in the Software without restriction, including without limitation the rights  
 16  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 17  # copies of the Software, and to permit persons to whom the Software is  
 18  # furnished to do so, subject to the following conditions: 
 19  # 
 20  # The above copyright notice and this permission notice shall be included in     
 21  # all copies or substantial portions of the Software. 
 22  # 
 23  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 24  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 25  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 26  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 27  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 28  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 29  # SOFTWARE. 
 30  # 
 31   
 32  # Authors: 
 33  #   Michael Eddington (mike@phed.org) 
 34   
 35  # $Id: Peach.Transformers.compress-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 36   
 37  import zlib, bz2 
 38  from Peach.transformer import Transformer 
 39   
40 -class GzipCompress(Transformer):
41 ''' 42 Gzip compression transform. Also allows for compression level 43 selection (default is 6). 44 ''' 45
46 - def __init__(self, level = 6):
47 ''' 48 @type level: number 49 @param level: level is an integer from 1 to 9 controlling the level 50 of compression; 1 is fastest and produces the least compression, 9 51 is slowest and produces the most. The default value is 6. 52 ''' 53 Transformer.__init__(self) 54 self._level = level 55 self._wbits = 15
56
57 - def realEncode(self, data):
58 return zlib.compress(data, self._level)
59
60 - def realDecode(self, data):
61 return zlib.decompress(data, self._wbits)
62 63
64 -class GzipDecompress(Transformer):
65 ''' 66 Gzip decompression transform. 67 ''' 68
69 - def __init__(self, wbits = 15):
70 ''' 71 @type wbits: number 72 @param wbits: The absolute value of wbits is the base two logarithm 73 of the size of the history buffer (the ``window size'') used when 74 compressing data. Its absolute value should be between 8 and 15 for 75 the most recent versions of the zlib library, larger values resulting 76 in better compression at the expense of greater memory usage. The 77 default value is 15. When wbits is negative, the standard gzip 78 header is suppressed; this is an undocumented feature of the zlib 79 library, used for compatibility with unzip's compression file format. 80 ''' 81 Transformer.__init__(self) 82 self._wbits = wbits 83 self._level = 6
84
85 - def realEncode(self, data):
86 return zlib.decompress(data, self._wbits)
87
88 - def realDecode(self, data):
89 return zlib.compress(data, self._level)
90 91
92 -class Bz2Compress(Transformer):
93 ''' 94 bzip2 compression transform. Also allows for compression level 95 selection (default is 9). 96 ''' 97
98 - def __init__(self, level = 9):
99 ''' 100 @type level: number 101 @param level: The compresslevel parameter, if given, must be a number 102 between 1 and 9; the default is 9. 103 ''' 104 Transformer.__init__(self) 105 self._level = level
106
107 - def realEncode(self, data):
108 return bz2.compress(data, self._level)
109
110 - def realDecode(self, data):
111 return bz2.decompress(data)
112 113
114 -class Bz2Decompress(Transformer):
115 ''' 116 bzip2 decompression transform. 117 ''' 118
119 - def realEncode(self, data):
120 return bz2.decompress(data)
121
122 - def realDecode(self, data):
123 return bz2.compress(data, 6)
124 125 126 # end 127