forked from pythcoiner/SeedQReader
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeflate.py
More file actions
35 lines (25 loc) · 938 Bytes
/
deflate.py
File metadata and controls
35 lines (25 loc) · 938 Bytes
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
import sys
import zlib
class DeflateIO:
def __init__(self, stream) -> None:
self.stream = stream
self.data = stream.read()
def read(self):
return zlib.decompress(self.data, wbits=-10)
def write(self, input_data):
compressor = zlib.compressobj(wbits=-10)
compressed_data = compressor.compress(input_data)
compressed_data += compressor.flush()
self.stream.seek(0) # Ensure we overwrite the stream from the beginning
self.stream.write(compressed_data)
self.stream.truncate() # Remove any remaining part of the old data
def __enter__(self):
# Return the instance itself when entering the context
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# Handle cleanup here if necessary
pass
class Deflate:
DeflateIO = DeflateIO
if "deflate" not in sys.modules:
sys.modules["deflate"] = Deflate