python - 将文件夹压缩成多个部分?

标签 python zip

是否可以在 Python 中将一个文件夹压缩成多个文件?我刚刚找到了一些如何将文件夹/文件压缩到单个 zip 容器的示例。

简而言之:如何在 Python 中将一个文件夹压缩到多个 zip 部分?

最佳答案

您可以先将文件压缩成一个巨大的文件,然后再将其拆分成多个部分。经过测试,它有效。

# MAX = 500*1024*1024    # 500Mb    - max chapter size
MAX = 15*1024*1024
BUF = 50*1024*1024*1024    # 50GB     - memory buffer size


def file_split(FILE, MAX):
    '''Split file into pieces, every size is  MAX = 15*1024*1024 Byte''' 
    chapters = 1
    uglybuf = ''
    with open(FILE, 'rb') as src:
        while True:
            tgt = open(FILE + '.%03d' % chapters, 'wb')
            written = 0
            while written < MAX:
                if len(uglybuf) > 0:
                    tgt.write(uglybuf)
                tgt.write(src.read(min(BUF, MAX - written)))
                written += min(BUF, MAX - written)
                uglybuf = src.read(1)
                if len(uglybuf) == 0:
                    break
            tgt.close()
            if len(uglybuf) == 0:
                break
            chapters += 1

def zipfiles(directory, outputZIP = 'attachment.zip'):
    # path to folder which needs to be zipped
    # directory = './outbox'

    # calling function to get all file paths in the directory
    file_paths = get_all_file_paths(directory)

    # printing the list of all files to be zipped
    print('Following files will be zipped:')
    for file_name in file_paths:
        print(file_name)

    # writing files to a zipfile
    with ZipFile(outputZIP,'w') as zip:
        # writing each file one by one
        for file in file_paths:
            zip.write(file)

    print('All files zipped successfully!')  

if __name__ == '__main__':
    outputZIP = 'attachment.zip'
    zipfiles(directory, outputZIP)
    file_split(outputZIP, MAX)

关于python - 将文件夹压缩成多个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955556/

相关文章:

python - 为什么具有名为 "del"、 "return"等的对象属性是语法错误?

java - 如何从字符串在 java 中创建 Gzip 存档?

java - 从 InputStream 创建 Java 7 zip 文件系统

python - 在 Python 中创建不可变对象(immutable对象)

python - 合并 pySpark RDD 中的列表列表

python - 使用 argparse 指定文件扩展名

python - 如何在Python中创建矩阵AxB?

java - 使用 angular $http 或 jquery.ajax 下载二进制文件时遇到问题

powershell - 使用 powershell 获取许多 zip 文件的未压缩大小

Android:是否可以将 ZIP 文件添加为原始资源并使用 ZipFile 读取它?