Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create zip archive using shutil.make_archive() without a file extension in python?

I need to create a zip archive with a custom extension of ‘.stz’ as this is the only file format accepted by the application it will be used in. Using shutil.make_archive the file always has the .zip file extension even if not specified by me. Currently just renaming it after creation. Is there a better way to do this?

import os
import shutil

output = “file.stz”
dir_name = “files/“
zip_name = shutil.make_archive(‘tmp’, ‘zip’, dir_name)
os.rename(zip_name, output)
like image 268
Jack Kershaw Avatar asked Oct 14 '25 08:10

Jack Kershaw


1 Answers

You could use shutil.register_archive_format to register your own extension, but still compress it as a zip file. This is more code but does allow you to skip your rename step.

def stzCompress():
  pass
 
shutil.register_archive_format('stz', stzCompress, description='stz file')
like image 90
DiB Avatar answered Oct 16 '25 23:10

DiB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!