zipfile.write() : relative path of files, reproduced in the zip archive
Asked Answered
T

4

14

Using zip file, I indicate files locate within an other folder for example: './data/2003-2007/metropolis/Matrix_0_1_0.csv'

My problem is that, when I extract it, the files are found in ./data/2003-2007/metropolis/Matrix_0_1_0.csv, while I would like it to be extract in ./

Here is my code:

def zip_files(src, dst):
    zip_ = zipfile.ZipFile(dst, 'w')

    print src, dst

    for src_ in src:
        zip_.write(src_, os.path.relpath(src_, './'), compress_type = zipfile.ZIP_DEFLATED)

    zip_.close()

Here is the print of src and dst:

    ['./data/2003-2007/metropolis/Matrix_0_1_0.csv', './data/2003-2007/metropolis/Matrix_0_1_1.csv'] ./data/2003-2007/metropolis/csv.zip
Tomas answered 29/5, 2013 at 8:47 Comment(4)
See #4917784Pugging
Thanks but, it doesn't seem to be linked. He is trying to extract, I'm trying to compress. He doesn't use ZipFile.write()Tomas
For writing without preserving the directory structure, see #7008368Pugging
Yep. I had read it, but I didn't get it clearlyTomas
T
11

As shown in: Python: Getting files into an archive without the directory?

The solution is:

     ''' 
    zip_file:
        @src: Iterable object containing one or more element
        @dst: filename (path/filename if needed)
        @arcname: Iterable object containing the names we want to give to the elements in the archive (has to correspond to src) 
'''
def zip_files(src, dst, arcname=None):
    zip_ = zipfile.ZipFile(dst, 'w')

    print src, dst
    for i in range(len(src)):
        if arcname is None:
            zip_.write(src[i], os.path.basename(src[i]), compress_type = zipfile.ZIP_DEFLATED)
        else:
            zip_.write(src[i], arcname[i], compress_type = zipfile.ZIP_DEFLATED)

    zip_.close()
Tomas answered 29/5, 2013 at 9:29 Comment(0)
M
5
import os
import zipfile

def zipdir(src, dst, zip_name):
    """
    Function creates zip archive from src in dst location. The name of archive is zip_name.
    :param src: Path to directory to be archived.
    :param dst: Path where archived dir will be stored.
    :param zip_name: The name of the archive.
    :return: None
    """
    ### destination directory
    os.chdir(dst)
    ### zipfile handler
    ziph = zipfile.ZipFile(zip_name, 'w')
    ### writing content of src directory to the archive
    for root, dirs, files in os.walk(src):
        for file in files:
            ### In this case the structure of zip archive will be:
            ###       C:\Users\BO\Desktop\20200307.zip\Audacity\<content of Audacity dir>
            # ziph.write(os.path.join(root, file), arcname=os.path.join(root.replace(os.path.split(src)[0], ""), file))

            ### In this case the structure of zip archive will be:
            ###       C:\Users\BO\Desktop\20200307.zip\<content of Audacity dir>
            ziph.write(os.path.join(root, file), arcname=os.path.join(root.replace(src, ""), file))
    ziph.close()


if __name__ == '__main__':
    zipdir("C:/Users/BO/Documents/Audacity", "C:/Users/BO/Desktop", "20200307.zip")
Malkin answered 7/3, 2020 at 10:14 Comment(0)
E
1

Maybe better solution in this case to use tarfile:

with tarfile.open(output, "w:gz") as tar:
    # if we do not provide arcname, archive will include full paths
    arcname = path.split('/')[-1]
    tar.add(path, arcname)
    tar.close()
Eructate answered 15/8, 2018 at 11:28 Comment(0)
K
0

As written in the documentation there is a parameter of ZipFile.write called arcname.

So, you can use is to name your file(s) as you want. Note: to be able to make it dynamical, you should consider to import pathlib library. In your case:

from pathlib import Path
src = Path('./data/2003-2007/metropolis')
file = Path('./data/2003-2007/metropolis/Matrix_0_1_0.csv')
zip_.write(file, arcname=file.relative_to(src) , compress_type = zipfile.ZIP_DEFLATED)

If you want to get every files under a directory, and then make a zip from those files, you can do something like this:

from pathlib import Path
# source: https://mcmap.net/q/150221/-listing-of-all-files-in-directory 
p = Path(src).glob('**/*') # lists all files and directories under your src path
file_paths = [x for x in p if x.is_file()]  # filter for the files only
for src_ in file_paths:
        zip_.write(src_, arcname=src_.relative_to(src) , compress_type = zipfile.ZIP_DEFLATED)

I know it was years ago, but maybe it will be useful for someone.

Kowalczyk answered 13/12, 2022 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.