-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzBytesIO.py
107 lines (92 loc) · 3.03 KB
/
gzBytesIO.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
import gzip
try:
# For Python 3
from io import BytesIO
except ImportError:
# For Python 2
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
class GzipBytesIO(object):
def __init__(self, initial_bytes=None, mode='wb'):
"""
Initialize the GzipBytesIO object.
:param initial_bytes: Optional initial bytes to load into the buffer.
:param mode: Mode of operation, 'wb' for writing or 'rb' for reading.
"""
self.buffer = BytesIO()
self.mode = mode
if 'w' in mode:
self.gzip_file = gzip.GzipFile(fileobj=self.buffer, mode='wb')
elif 'r' in mode:
if initial_bytes:
self.buffer.write(initial_bytes)
self.buffer.seek(0)
self.gzip_file = gzip.GzipFile(fileobj=self.buffer, mode='rb')
else:
raise ValueError("Mode must be 'rb' or 'wb'")
self.closed = False
def write(self, data):
"""
Write data to the GzipBytesIO buffer.
:param data: Data to write.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
if 'w' not in self.mode:
raise IOError("File not open for writing")
self.gzip_file.write(data)
def read(self, size=-1):
"""
Read data from the GzipBytesIO buffer.
:param size: Number of bytes to read.
:return: Decompressed data.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
if 'r' not in self.mode:
raise IOError("File not open for reading")
return self.gzip_file.read(size)
def seek(self, offset, whence=0):
"""
Seek to a position in the GzipBytesIO buffer.
:param offset: Offset to seek to.
:param whence: Reference point for offset.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
return self.gzip_file.seek(offset, whence)
def tell(self):
"""
Tell the current position in the GzipBytesIO buffer.
:return: Current position.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
return self.gzip_file.tell()
def flush(self):
"""
Flush the GzipBytesIO buffer.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
self.gzip_file.flush()
def close(self):
"""
Close the GzipBytesIO buffer.
"""
if not self.closed:
self.gzip_file.close()
self.buffer.close()
self.closed = True
def getvalue(self):
"""
Get the compressed data from the buffer.
:return: Compressed data.
"""
if self.closed:
raise ValueError("I/O operation on closed file.")
if 'w' in self.mode:
self.gzip_file.flush()
return self.buffer.getvalue()