import zipfile
import os
import tarfile
import shutil

# Remove temp directory if exists
shutil.rmtree('flatsome-cleaned-temp', ignore_errors=True)

# Extract tar.gz
os.makedirs('flatsome-cleaned-temp')
with tarfile.open('flatsome-cleaned-3.20.5.tar.gz', 'r:gz') as tar:
    tar.extractall('flatsome-cleaned-temp')

# Create ZIP
zipf = zipfile.ZipFile('flatsome-cleaned-3.20.5.zip', 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk('flatsome-cleaned-temp/flatsome-cleaned'):
    for file in files:
        file_path = os.path.join(root, file)
        arcname = os.path.relpath(file_path, 'flatsome-cleaned-temp/flatsome-cleaned')
        zipf.write(file_path, arcname)

zipf.close()

# Clean up temp directory
shutil.rmtree('flatsome-cleaned-temp')

print("ZIP archive created: flatsome-cleaned-3.20.5.zip")
