'Split a zip-file into chunks with Python
I have a piece of a code which creates a zip file successfully, but I need to split this file if the size of it is more that 1MB.
I have this code but it doesn't work:
from split_file_reader.split_file_writer import SplitFileWriter
import zipfile
# make element tree
tree = etree.ElementTree(batch_element)
# make xml file and write it in stream
xml_object = BytesIO()
tree.write(xml_object, pretty_print=True, xml_declaration=False, encoding="utf-8")
xml_file = xml_object.getvalue()
final = BytesIO()
with SplitFileWriter(final, 1_000_000) as sfw:
with zipfile.ZipFile(sfw, "a") as zip_file:
zip_file.writestr('Batch.xml', xml_file)
I want to retrieve the split file as bytes. The zipping part is working, but the splitting doesn't.
Solution 1:[1]
Read the docs for the module which you are using, which is https://pypi.org/project/split-file-reader
It should have the usage instructions in there.
EDIT: This is an example:
with SplitFileWriter("split.zip.", 500_000) as sfw:
with zipfile.ZipFile(file=sfw, mode='w') as zipf:
for root, dirs, files in os.walk("./"):
for file in files:
if file.startswith("random_payload"):
zipf.write(os.path.join(root, file))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Psuedodoro |
