Adding file to existing zipfile
Asked Answered
R

2

25

I'm using python's zipfile module.
Having a zip file located in a path of:
/home/user/a/b/c/test.zip
And having another file created under /home/user/a/b/c/1.txt I want to add this file to existing zip, I did:

zip = zipfile.ZipFile('/home/user/a/b/c/test.zip','a')
zip.write('/home/user/a/b/c/1.txt')
zip.close()`

And got all the subfolders appears in path when unzipping the file, how do I just enter the zip file without path's subfolders?

I tried also : zip.write(os.path.basename('/home/user/a/b/c/1.txt')) And got an error that file doesn't exist, although it does.

Radar answered 14/12, 2015 at 15:21 Comment(0)
O
35

You got very close:

zip.write(path_to_file, os.path.basename(path_to_file))

should do the trick for you.

Explanation: The zip.write function accepts a second argument (the arcname) which is the filename to be stored in the zip archive, see the documentation for zipfile more details.

os.path.basename() strips off the directories in the path for you, so that the file will be stored in the archive under just it's name.

Note that if you only zip.write(os.path.basename(path_to_file)) it will look for the file in the current directory where it (as the error says) does not exist.

Ocotillo answered 14/12, 2015 at 15:38 Comment(0)
B
40
import zipfile

# Open a zip file at the given filepath. If it doesn't exist, create one.
# If the directory does not exist, it fails with FileNotFoundError
filepath = "/home/user/a/b/c/test.zip"
with zipfile.ZipFile(filepath, "a", compression=zipfile.ZIP_DEFLATED) as zipf:
    # Add a file located at the source_path to the destination within the zip
    # file. It will overwrite existing files if the names collide, but it
    # will give a warning
    source_path = '/home/user/a/b/c/1.txt'
    destination = 'foobar.txt'
    zipf.write(source_path, destination)

WARNING: The ZipFile class has a compression parameter. By default it is ZIP_STORED (no compression)! If you want compression, set compression=zipfile.ZIP_DEFLATED.

Bitch answered 29/4, 2019 at 7:25 Comment(2)
I would give you more upvotes if I could. Furthermore, this needs to be the accepted answer because of completeness.Mon
please note, this example does not include any compression. before you copy\paste please read #4166947Durr
O
35

You got very close:

zip.write(path_to_file, os.path.basename(path_to_file))

should do the trick for you.

Explanation: The zip.write function accepts a second argument (the arcname) which is the filename to be stored in the zip archive, see the documentation for zipfile more details.

os.path.basename() strips off the directories in the path for you, so that the file will be stored in the archive under just it's name.

Note that if you only zip.write(os.path.basename(path_to_file)) it will look for the file in the current directory where it (as the error says) does not exist.

Ocotillo answered 14/12, 2015 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.