【问题标题】:estimate and create zip files of specific size from a directory从目录估计并创建特定大小的 zip 文件
【发布时间】:2021-03-15 11:14:49
【问题描述】:

我正在构建一个独立于平台的基于 python 的工具,它在最后给出一个 zip 文件。然后用户必须在我们的平台上上传这个 zip 文件。现在我们为每个用户设置了 250mb 的上传限制。因此,如果用户的 zip 文件 > 250MB(这是可能的,也是可接受的),他们将无法上传。然后他们必须有选择地选择他们的文件并在上传限制内创建大量 zip 文件,然后上传它们。

为了减轻用户的痛苦,我希望在工具本身内实现这个功能,即工具应该

  1. 动态压缩目录中的文件
  2. 当压缩文件大小达到 250MB 时,它会创建一个 zip 文件,例如zipfile_partn.zip(用于第 n 个 250 MB 的 zip 文件
  3. 最终,该工具应该有一堆 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 大小。

【问题讨论】:

    标签: python zip


    【解决方案1】:

    我已经使用 python 中的zipfile 创建了允许大小的 zip。我们遍历每个文件并写入 zip,如果 zip 大小等于或超过我们正在写入的当前文件,我们关闭它并创建新的 zip 文件。

    import os
    import zipfile
    
    # size is defined in mbs
    def zip_folder(path, max_filesize=100):
        files_written = 1 # file counter
        zip_name = f"{files_written}.zip" # first zip filename
        zipWriter = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) # initialize zip writer
        zip_names = [f"{files_written}.zip"]
        for root, dirs, files in os.walk(path):
            for file in files:
                zipWriter.write(os.path.join(root, file), 
                           os.path.relpath(os.path.join(root, file), 
                                           os.path.join(path, '..')))
                
                # current zip size
                current_size = os.path.getsize(zip_name) >> 20
                if current_size > max_filesize: # if zip size increase allowed size
                    zipWriter.close() # close current zip writer
                    files_written += 1 # add increment
                    zip_name = f"{files_written}.zip" # new zip name
                    zip_names.append(zip_name) # add new filename to list
                    
                    zipWriter = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) # initialize writer
    
        return zip_names
    
    zip_folder("path", 1024) # will create zip of 1GB each
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多