forked from netfarm/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.py
136 lines (109 loc) · 3.35 KB
/
compress.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4 -*-
#
# Netfarm Mail Archiver - release 2
#
# Copyright (C) 2005-2007 Gianluigi Tiesi <sherpya@netfarm.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
# ======================================================================
## @file compress.py
## Helper for file compression
__doc__ = '''Netfarm Archiver - release 2.1.0 - Helper for file compression'''
__version__ = '2.1.0'
__all__ = [ 'CompressedFile', 'compressors' ]
from cStringIO import StringIO
compressors = {}
## Gzip File support
try:
from gzip import GzipFile
compressors.update({'gzip': 'GzipCompressedFile'})
except:
pass
## Zip File support
try:
import zlib
from zipfile import ZipFile
from zipfile import ZIP_STORED, ZIP_DEFLATED
compressors.update({'zip': 'ZipCompressedFile'})
except:
pass
## BZip2 File support
try:
from bz2 import BZ2Compressor
compressors.update({'bzip2': 'BZip2CompressedFile'})
except:
pass
class UnsupportedCompressor(Exception):
pass
class InvalidMethod(Exception):
pass
class GzipCompressedFile:
def __init__(self, **args):
self.data = StringIO()
self.classobj = GzipFile(args.get('name'), 'wb', args.get('ratio'), self.data)
def write(self, data):
self.classobj.write(data)
def getdata(self):
self.classobj.close()
return self.data.getvalue()
def close(self):
self.data.close()
def __del__(self):
try:
self.data.close()
self.close()
except:
pass
class ZipCompressedFile:
def __init__(self, **args):
self.data = StringIO()
self.name = args.get('name')
if args.get('ratio') > 0:
ratio = ZIP_DEFLATED
else:
ratio = ZIP_STORED
self.classobj = ZipFile(self.data, 'wb', ratio)
def write(self, data):
self.classobj.writestr(self.name, data)
def getdata(self):
self.classobj.close()
return self.data.getvalue()
def close(self):
self.data.close()
def __del__(self):
try:
self.close()
except:
pass
class BZip2CompressedFile:
def __init__(self, **args):
self.classobj = BZ2Compressor(args.get('ratio'))
def write(self, data):
self.classobj.compress(data)
def getdata(self):
return self.classobj.flush()
def close(self):
pass
def CompressedFile(**args):
compressor = args.get('compressor', None)
if compressor is None or not compressors.has_key(compressor):
raise UnsupportedCompressor
args['name'] = args.get('name', 'Unnamed')
ratio = None
try:
ratio = int(args.get('ratio', 9))
except:
pass
if ratio is None or (ratio < 0) or (ratio > 9):
raise InvalidMethod
args['ratio'] = ratio
return globals().get(compressors[compressor])(**args)