簡體   English   中英

使用 Python 將 zip 文件拆分為多個塊

[英]Split a zip-file into chunks with Python

我有一段代碼可以成功創建一個 zip 文件,但是如果它的大小超過 1MB,我需要拆分這個文件。

我有這段代碼,但它不起作用:

    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)

我想以字節形式檢索拆分文件。 壓縮部分正在工作,但拆分沒有。

閱讀您正在使用的模塊的文檔,即https://pypi.org/project/split-file-reader

里面應該有使用說明。

編輯:這是一個例子:

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))

根據split_file_reader 文檔SplitFileWriter的第一個參數可以是生成類似文件的對象的生成器。 這將允許您將 zip 文件拆分為BytesIO塊列表。

這是一個工作示例腳本:

import zipfile
from io import BytesIO
from lxml import etree
from split_file_reader.split_file_writer import SplitFileWriter

# make element tree
# tree = etree.ElementTree(batch_element)
tree = etree.parse('/tmp/test.xml')

# 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()

chunks = []

def gen(lst):
    while True:
        lst.append(BytesIO())
        yield lst[-1]

with SplitFileWriter(gen(chunks), 1_000_000) as sfw:
    with zipfile.ZipFile(sfw, "w") as zip_file:
        zip_file.writestr('Batch.xml', xml_file)

for i, chunk in enumerate(chunks):
    print(f'chunk {i}: {len(chunk.getvalue())}')

輸出:

chunk 0: 1000000
chunk 1: 1000000
chunk 2: 1000000
chunk 3: 1000000
chunk 4: 1000000
chunk 5: 887260

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM