-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfile_converter.py
More file actions
109 lines (89 loc) · 3.51 KB
/
file_converter.py
File metadata and controls
109 lines (89 loc) · 3.51 KB
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
"""File format conversion extension for databus-python-client.
Provides streaming pipeline for file decompression, re-compression,
and checksum validation during download operations.
"""
import gzip
import hashlib
from typing import BinaryIO, Optional
class FileConverter:
"""Handles file format conversion with streaming support."""
CHUNK_SIZE = 8192 # 8KB chunks for streaming
@staticmethod
def decompress_gzip_stream(
input_stream: BinaryIO,
output_stream: BinaryIO,
validate_checksum: bool = False,
) -> Optional[str]:
"""Decompress gzip stream with optional checksum computation.
Decompresses *input_stream* into *output_stream*. When
*validate_checksum* is ``True`` the SHA-256 digest of the
**decompressed** bytes is computed on-the-fly and returned.
To validate the checksum of the **compressed** input, use
:meth:`validate_checksum_stream` on the input stream before
calling this method.
Args:
input_stream: Input gzip compressed stream.
output_stream: Output decompressed stream.
validate_checksum: Whether to compute a SHA-256 checksum of
the decompressed output.
Returns:
Hex-encoded SHA-256 checksum of the decompressed data when
*validate_checksum* is ``True``, otherwise ``None``.
"""
hasher = hashlib.sha256() if validate_checksum else None
with gzip.open(input_stream, 'rb') as gz:
while True:
chunk = gz.read(FileConverter.CHUNK_SIZE)
if not chunk:
break
output_stream.write(chunk)
if hasher:
hasher.update(chunk)
return hasher.hexdigest() if hasher else None
@staticmethod
def compress_gzip_stream(
input_stream: BinaryIO,
output_stream: BinaryIO
) -> None:
"""Compress stream to gzip format.
Args:
input_stream: Input uncompressed stream
output_stream: Output gzip compressed stream
"""
with gzip.open(output_stream, 'wb') as gz:
while True:
chunk = input_stream.read(FileConverter.CHUNK_SIZE)
if not chunk:
break
gz.write(chunk)
@staticmethod
def validate_checksum_stream(
input_stream: BinaryIO,
expected_checksum: str
) -> bool:
"""Validate SHA256 checksum of a stream.
Args:
input_stream: Input stream to validate. Must be seekable; the stream
is rewound to position 0 both before reading and after a
successful validation.
expected_checksum: Expected SHA256 checksum
Returns:
True if checksum matches
Raises:
IOError: If checksum validation fails
"""
hasher = hashlib.sha256()
input_stream.seek(0)
while True:
chunk = input_stream.read(FileConverter.CHUNK_SIZE)
if not chunk:
break
hasher.update(chunk)
computed = hasher.hexdigest()
if computed.lower() != expected_checksum.lower():
raise IOError(
f"Checksum mismatch: expected {expected_checksum}, "
f"got {computed}"
)
input_stream.seek(0)
return True