Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid adding directories to encrypted zip #8

Closed
blueteamzone opened this issue Apr 3, 2020 · 1 comment
Closed

Avoid adding directories to encrypted zip #8

blueteamzone opened this issue Apr 3, 2020 · 1 comment

Comments

@blueteamzone
Copy link

I have got a list of file paths and I wanted to create an encrypted zip file using them.

>>> import pyzipper
>>> with pyzipper.AESZipFile('test.zip' ,'a') as zf:
...     zf.pwd = b'test'
...     for f_path in listfiles:
...         zf.write(f_path)

The above code creates the encrypted zip file, however it also creates the directories. For example when I extract the content of the zip.

/home/bob/files/testfile_1.csv
/home/bob/files/testfile_2.csv
/home/bob/files/testfile_3.csv

How can I avoid this? I want to find only the files testfile_1.csv, testfile_2.csv, testfile_3.csv when I extract the zip.

@danifus
Copy link
Owner

danifus commented Apr 3, 2020

The solution to this is the same as the using Python's zipfile module ZipFile.write

You can get the results you want by specifying the name you would like the file to have in the archive (here I'm using os.path.basename() to get the last part, the filename in your example, from the full path):

zf.write(f_path, arcname=os.path.basename(f_path))

@danifus danifus closed this as completed Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants