Reputation: 29
I am trying to compress an archive to a ".zip" file (or several) with a maximum size (eg 1024mb) with Python.
The only libraries I have found are gzip, shutil and zipfile, but I cannot find any option in them that allows me to split these files with a maximum size.
I don't want to use functionalities of the operating system, I would like to do it with a python library. Is this possible?
Thank you very much.
Upvotes: 2
Views: 1461
Reputation: 1
You can chunk the file into several parts based on the size by passing the max_part_size
argument for SplitFileWriter
. Read the documentation.
A simple example:
from split_file_reader.split_file_writer import SplitFileWriter
import pathlib, math
file_path = '/var/example.mp4'
part_size = 1024 * 1024 * 1024, # 1024 mb
file = pathlib.Path(file_path)
part_count = math.ceil(file.stat().st_size / part_size)
width = len(str(part_count)) + 1
compressed_names = [file.stem+".zip."+str(i).zfill(width) for i in range(part_count)]
with SplitFileWriter(
max_part_size=part_size,
filenames=compressed_names
) as sfw:
with zipfile.ZipFile(file=sfw, mode='w') as zipf:
zipf.write(
file.absolute(),
file.name
)
Upvotes: 0
Reputation: 61
You should be able do this with zipfile, but I haven't tested this.
Make a list of zipfile objects with only one zipfile object inside. Iterate over the files you want to compress, adding them to the newest zipfile in the list, then checking to see if the new element puts you over your file size limit. If it does, delete the item you just zipped from the zipfile, then add a new zipfile to the zipfile list and add the just deleted file to the newest zipfile object in the list.
You will need to use ZipInfo.compress_size to find out the compressed file size.
Upvotes: 2