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
The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.
extractall() method will extract all the contents of the zip file to the current working directory. You can also call extract() method to extract any file by specifying its path in the zip file. This will extract only the specified file.
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()
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With