简体   繁体   中英

How to add files from multiple zip files into the single zip file

I want to put files in the multiple zip files that have common substring into a single zipfile

I have a folder "temp" containing some.zip files and some other files

filename1_160645.zip
filename1_165056.zip
filename1_195326.zip
filename2_120528.zip
filename2_125518.zip
filename3_171518.zip
test.xlsx
filename19_161518.zip

I have following dataframe df_filenames containing the prefixes of filename

filename_prefix

filename1
filename2
filename3

if there are multiple.zip files in the temp folder with the same prefix that exists in the dataframe df_filenames,i want to merge the contents of those files

for example filename1_160645.zip contains following contents

1a.csv
1b.csv

and filename1_165056.zip contains following contents

1d.csv

and filename1_195326.zip contains following contents

1f.csv

after merging the contents of above 2 files into the filename1_160645.zip the contents of filename1_160645.zip will be

1a.csv
1b.csv
1d.csv
1f.csv

At the end only following files will remain the temp folder

filename1_160645.zip
filename2_120528.zip
filename3_171518.zip
test.xlsx
filename19_161518.zip

I have written the following code but it's not working


import os
import zipfile as zf
import pandas as pd

df_filenames=pd.read_excel('filename_prefix.xlsx')
#Get the list of all the filenames in the temp folder
lst_fnames=os.listdir(r'C:\Users\XYZ\Downloads\temp')
#take only .zip files
lst_fnames=[fname for fname in lst_fnames if fname.endswith('.zip')]

#take distinct prefixes in the dataframe
df_prefixes=df_filenames['filename_prefix'].unique()

for prefix in df_prefixes:
    #this list will contain zip files with the same prefixes
    lst=[]

    #total count of files in the lst
    count=0
    for fname in lst_fnames:
        if prefix in fname:
            #print(prefix)
            lst.append(fname)
            #print(lst)
    #if the list has more than 1 zip files,merge them
    if len(lst)>1:
        print(lst)
        with zf.ZipFile(lst[0], 'a') as f1:
            print(f1.filename)
            for f in lst[1:]:

                with zf.ZipFile(path+'\\'+f, 'r') as f:
                    print(f.filename) #getting entire path of the file here,not just filename
                    [f1.writestr(t[0], t[1].read()) for t in ((n, f.open(n)) for n in f.namelist())]
                    print(f1.namelist())

after merging the contents of the files with the filename containing filename1 into the filename1_160645.zip, the contents of ``filename1_160645.zip``` should be

1a.csv
1b.csv
1d.csv
1f.csv

but nothing has changed when I double click filename1_160645.zip Basically, 1a.csv,1b.csv,1d.csv,1f.csv are not part of filename1_160645.zip

I would use shutil for a higher level view for dealing with archive files. Additionally using pathlib gives nice methods/attributes for a given filepath. Combined with a groupby , we can easily extract target files that are related to each other.

import itertools
import shutil
from pathlib import Path
import pandas as pd

filenames = pd.read_excel('filename_prefix.xlsx')
prefixes = filenames['filename_prefix'].unique()

path = Path.cwd()  # or change to Path('path/to/desired/dir/')
zip_files = (file for file in path.iterdir() if file.suffix == '.zip')
target_files = sorted(file for file in zip_files 
                      if any(file.stem.startswith(pre) for pre in prefixes))

file_groups = itertools.groupby(target_files, key=lambda x: x.stem.split('_')[0])
for _, group in file_groups:
    first, *rest = group
    if not rest:
        continue

    temp_dir = path / first.stem
    temp_dir.mkdir()

    shutil.unpack_archive(first, extract_dir=temp_dir)
    for item in rest:
        shutil.unpack_archive(item, extract_dir=temp_dir)
        item.unlink()

    shutil.make_archive(temp_dir, 'zip', temp_dir)
    shutil.rmtree(temp_dir)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM