可以在Python上将文件夹压缩成多个文件吗?我刚刚找到了一些如何将文件夹/文件压缩到单个zip容器的示例。
简短:如何在Python中将一个文件夹压缩成多个zip部件?
答案 0 :(得分:2)
num_files = 4 #number of zip files to create
dir_to_zip = "/a/file/path/"
#get list of all files in directory
files = [os.path.join(dir_to_zip,file) for file in os.listdir(dir_to_zip)]
#partition the files how you want (here we just spit it into 4 equal length lists of files)
files_in_parts = zip(*zip(*[iter(files)]*num_files))
#enumerate over each partition and add it to its own zip file
for i,zip_contents in enumerate(files_in_parts,1):
with ZipFile('zipfile%s.zip'%i, 'w') as myzip:
map(myzip.write,zip_contents)
答案 1 :(得分:0)
对于它的价值,Python 2.7和Python 3.3的标准ziplib
明确表示它不支持多磁盘存档:
此模块当前不处理多磁盘ZIP文件。
如果您希望创建单个逻辑多文件存档,则可能被迫shell out to an operating system command。
答案 2 :(得分:0)
您可以先将文件压缩为一个巨大的文件,然后将其拆分为多个部分。经过测试,可以正常工作。
# 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)