【发布时间】:2021-03-15 11:14:49
【问题描述】:
我正在构建一个独立于平台的基于 python 的工具,它在最后给出一个 zip 文件。然后用户必须在我们的平台上上传这个 zip 文件。现在我们为每个用户设置了 250mb 的上传限制。因此,如果用户的 zip 文件 > 250MB(这是可能的,也是可接受的),他们将无法上传。然后他们必须有选择地选择他们的文件并在上传限制内创建大量 zip 文件,然后上传它们。
为了减轻用户的痛苦,我希望在工具本身内实现这个功能,即工具应该
- 动态压缩目录中的文件
- 当压缩文件大小达到 250MB 时,它会创建一个 zip 文件,例如zipfile_partn.zip(用于第 n 个 250 MB 的 zip 文件
- 最终,该工具应该有一堆 zip 文件,每个 250mb(或更少),然后用户可以上传到我们的平台上
我发现了类似的问题:Python: Continuously check size of files being added to list, stop at size, zip list, continue
import os,os.path, zipfile,time
#### Function to create zip file ####
# Add the files from the list to the zip archive
def zipFunction(zipList):
# Specify zip archive output location and file name
zipName = "O:\Exam Scan\exam-scan-ad_automate_input_pdf\pdfs\output.zip"
# Create the zip file object
zipA = zipfile.ZipFile(zipName, "w", allowZip64=True)
# Go through the list and add files to the zip archive
for w in zipList:
# Create the arcname parameter for the .write method. Otherwise the zip file
# mirrors the directory structure within the zip archive (annoying).
arcname = w[len(root)+1:]
# Write the files to a zip
zipA.write(w, arcname, zipfile.ZIP_DEFLATED)
# Close the zip process
zipA.close()
return
sTime = time.time()
# Set the size counter
totalSize = 0
# Create an empty list for adding files to count MB and make zip file
zipList = []
pdflist = []
xmlList = []
# Specify the directory to look at
searchDirectory = "O:\Exam Scan\exam-scan-ad_automate_input_pdf\pdfs"
# Create a counter to check number of files
count = 0
# Set the root, directory, and file name
for root,direc,f in os.walk(searchDirectory):
print(searchDirectory)
#Go through the files in directory
for name in f:
# Set the os.path file root and name
full = os.path.join(root,name)
# Split the file name from the file extension
n, ext = os.path.splitext(name)
# Get size of each file in directory, size is obtained in BYTES
fileSize = os.path.getsize(full)
# Add up the total sizes for all the files in the directory
totalSize += fileSize
# Convert from bytes to megabytes
# 1 kilobyte = 1,024 bytes
# 1 megabyte = 1,048,576 bytes
# 1 gigabyte = 1,073,741,824 bytes
megabytes = float(totalSize)/float(1048576)
zipList.append(full)
count +=1
print(megabytes)
if megabytes>5 :
zipFunction(zipList)
totalSize = 0
但这里的方法是文件的总大小达到限制时,代码会创建一个 zip。但我希望这是基于 zip 大小。
【问题讨论】: