You can download this code by clicking the button below.
This code is now available for download.
This function combines two files into a ZIP file, then converts it to a GZIP-compressed TAR file, and finally deletes the ZIP file.
Technology Stack : os, shutil, zipfile, tarfile
Code Type : Function
Code Difficulty :
def zip_file(file1, file2):
import os
import shutil
import zipfile
import tarfile
base_name = os.path.splitext(os.path.basename(file1))[0]
zip_name = f"{base_name}_combined.zip"
with zipfile.ZipFile(zip_name, 'w') as zipf:
zipf.write(file1)
zipf.write(file2)
with tarfile.open(f"{base_name}_combined.tar", "w:gz") as tar:
tar.add(file1, arcname=os.path.basename(file1))
tar.add(file2, arcname=os.path.basename(file2))
shutil.rmtree(f"{base_name}_combined.zip")