Commit f16913bf authored by JOSSOUD Olivier's avatar JOSSOUD Olivier
Browse files

utils. Use temporary directory when de-re-compressing ZIP files.

parent 0b4040ac
......@@ -3,6 +3,7 @@ import sys
import configobj
import pkgutil
import zipfile
import tempfile
import wimcollect.logger as logger
......@@ -34,22 +35,24 @@ def get_config_parser() -> configobj.ConfigObj:
config_parser = configobj.ConfigObj(conf_file_path)
return config_parser
def recompress_file(zip_filepath: str):
# Directory where the original ZIP is, where the ZIP content will temporarily be extracted and where the final
# LZMA file will be created.
current_directory = os.path.dirname(zip_filepath)
# Extract zip file and delete it
files_in_zip = extract_compressed_file(compressed_filepath=zip_filepath,
dest_dir=current_directory,
delete_compressed=True)
with tempfile.TemporaryDirectory() as tmp_dirpath:
# Extract zip file and delete it
files_in_zip = extract_compressed_file(compressed_filepath=zip_filepath,
dest_dir=tmp_dirpath,
delete_compressed=True)
# Compress the files
compressed_filepath = os.path.splitext(zip_filepath)[0] + ".lzma"
zipf = zipfile.ZipFile(compressed_filepath, 'w', zipfile.ZIP_LZMA)
for filepath in files_in_zip:
zipf.write(filepath, arcname=os.path.basename(filepath))
zipf.close()
# Compress the files
compressed_filepath = os.path.splitext(zip_filepath)[0] + ".lzma" # Replace .zip by .lzma.
zipf = zipfile.ZipFile(compressed_filepath, 'w', zipfile.ZIP_LZMA)
for filepath in files_in_zip:
zipf.write(filepath, arcname=os.path.basename(filepath))
zipf.close()
if os.path.exists(compressed_filepath):
for file in files_in_zip:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment