Rename files in subdirectories with the root directory name in an zipfile

Viewed 12

I have the following directory structure within my zip file:

myzip.zip
    - directory 1
        - subdirectory 1
            - imageA.jpg
            - imageB.jpg
    - directory 2 
        - subdirectory 2
            - imageA.jpg
            - imageB.jpg

And my goal is to rename the .jpg files to main directory name like so:

myzip.zip
    - directory 1
        - subdirectory 1
            - directory 1-1.jpg
            - directory 1-2.jpg
    - directory 2 
        - subdirectory 2
            - directory 2-1.jpg
            - directory 2-2.jpg

Thereby taking in account that an subdirectory can contain multiple .jpg files adding an incremental number after each newly renamed .jpg file starting from 1 (hence the new filename directory 1-1.jpg).

And lastly I would like to write these changes to an new zipfile, keeping the same structure with the only difference the changed names from the .jpg files.

My idea in code:

import zipfile

source = zipfile.ZipFile("myzip.zip", 'r')
target = zipfile.ZipFile(source.filename+"_renamed"+".zip", 'w', zipfile.ZIP_DEFLATED)

for file in source.infolist():
    filename = file.filename #directory 1/subdirectory 1/imageA.jpg
    rootname, image_name = filename.split("/subdirectory")
    # rootname results in: directory 1 
    # image_name results in /subdirectory/image_name.jpg
    new_image = image_name.replace(image_name, "/subdirectory/"+rootname+image_name[4:])
    target.write(rootname+new_image)

I though (haven't really tested it) about using zipfile.Zipfile and something of using the above code, but to be honest I have not really an idea how to solve this.

Any ideas or examples?

1 Answers

Here's some pseudocode representing how you could implement this:

unzip myzip.zip
for directory in unzipped:
    for subdirectory in directory:
        i = 0
        for file in subdirectory:
            file.rename(f"{directory.name}-{i}.jpg"
            i += 1
zip unzipped
Related