First , In the first volume file created test.z01 Add... To the front of \x50\x4b\x07\x08 This is the file header of volume compression (header), Occupy 4 Bytes . In fact, a single compressed file itself header That's it , The volume compression requires two emmm. Then it is from a single large compressed file test.zip Read from " One subvolume size -4 Bytes " The data of , write in test.zip.001 in , How to read data of a sub volume size , write in test.zip.002, And so on .
import os
import random
import sys
import zipfile
def zip_by_volume(file_path, block_size):
file_size = os.path.getsize(file_path)
path, file_name = os.path.split(file_path)
suffix = file_name.split('.')[-1]
zip_file = file_path + '.zip'
with zipfile.ZipFile(zip_file, 'w') as zf:
zf.write(file_path, arcname=file_name)
if file_size <= block_size:
return zip_file
else:
fp = open(zip_file, 'rb')
count = file_size // block_size