fixed gzip decompress
This commit is contained in:
parent
08ccea270d
commit
fd5e9542f6
@ -6,6 +6,7 @@ import asyncio
|
|||||||
import gzip
|
import gzip
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import io
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -147,15 +148,29 @@ class CZDS:
|
|||||||
logging.debug(f'Decompressing {filepath}')
|
logging.debug(f'Decompressing {filepath}')
|
||||||
output_path = filepath[:-3] # Remove .gz extension
|
output_path = filepath[:-3] # Remove .gz extension
|
||||||
|
|
||||||
async with aiofiles.open(filepath, 'rb') as f_in:
|
try:
|
||||||
content = await f_in.read()
|
async with aiofiles.open(filepath, 'rb') as f_in:
|
||||||
with gzip.open(content, 'rb') as gz:
|
content = await f_in.read()
|
||||||
async with aiofiles.open(output_path, 'wb') as f_out:
|
|
||||||
await f_out.write(gz.read())
|
# Use BytesIO to handle the content as a file-like object
|
||||||
|
with io.BytesIO(content) as bytes_io:
|
||||||
|
with gzip.GzipFile(fileobj=bytes_io, mode='rb') as gz:
|
||||||
|
decompressed_content = gz.read()
|
||||||
|
|
||||||
|
async with aiofiles.open(output_path, 'wb') as f_out:
|
||||||
|
await f_out.write(decompressed_content)
|
||||||
|
|
||||||
if cleanup:
|
if cleanup:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
logging.debug(f'Removed original gzip file: {filepath}')
|
logging.debug(f'Removed original gzip file: {filepath}')
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f'Failed to decompress {filepath}: {str(e)}'
|
||||||
|
logging.error(error_msg)
|
||||||
|
# Clean up any partial files
|
||||||
|
if os.path.exists(output_path):
|
||||||
|
os.remove(output_path)
|
||||||
|
raise Exception(error_msg)
|
||||||
|
|
||||||
|
|
||||||
async def download_zone(self, url: str, output_directory: str, decompress: bool = False, cleanup: bool = True, semaphore: asyncio.Semaphore = None):
|
async def download_zone(self, url: str, output_directory: str, decompress: bool = False, cleanup: bool = True, semaphore: asyncio.Semaphore = None):
|
||||||
|
Loading…
Reference in New Issue
Block a user