Decompresses a Gzip file and writes the contents to a temporary file. Args: filename: The path to the gzip file to decompress. chunk_size: Size of chunks to read and write at a time; defaults to 4MB. Returns: The path to the temporary file containing the decompresse
(
filename: pathlib.Path, chunk_size: int = 4 * 1024 * 1024
)
| 156 | |
| 157 | |
| 158 | def decompress_gzip( |
| 159 | filename: pathlib.Path, chunk_size: int = 4 * 1024 * 1024 |
| 160 | ) -> pathlib.Path: |
| 161 | """Decompresses a Gzip file and writes the contents to a temporary file. |
| 162 | |
| 163 | Args: |
| 164 | filename: The path to the gzip file to decompress. |
| 165 | chunk_size: Size of chunks to read and write at a time; defaults to 4MB. |
| 166 | |
| 167 | Returns: |
| 168 | The path to the temporary file containing the decompressed data. |
| 169 | |
| 170 | Raises: |
| 171 | gzip.BadGzipFile: If the file is not a valid gzip file. |
| 172 | """ |
| 173 | with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 174 | with gzip.open(filename, "rb") as file_handle: |
| 175 | while True: |
| 176 | chunk = file_handle.read(chunk_size) |
| 177 | if not chunk: |
| 178 | break |
| 179 | temp_file.write(chunk) |
| 180 | return pathlib.Path(temp_file.name) |