简体   繁体   English

如何在Python中使用Zipfile模块添加密码和输出目录?

[英]How to add a password and output directory using Zipfile module in Python?

I got below code from online and I am trying to add a password and I want to change the result directory to be "C:#SFTPDWN" (Final Zip file should be in this folder). 我从网上获得了下面的代码,尝试添加密码,并且想要将结果目录更改为“ C:#SFTPDWN”(最终的Zip文件应位于此文件夹中)。

I try to change it like below, it did not work. 我尝试将其更改为如下所示,但没有用。

with ZipFile('CC-Data.zip', 'w', 'pass word') as zip:

Can anybody please tell how to change this code to add password and change result folder? 有人可以告诉我如何更改此代码以添加密码和更改结果文件夹吗?

One last thing, currently it will zip #SFTPDWN folder, I just want to zip everything inside (Right now it will create two folders (CC-Data.zip and inside it #SFTPDWN )). 最后一件事,当前它将压缩#SFTPDWN文件夹,我只想将其中的所有内容压缩(现在它将创建两个文件夹(CC-Data.zip并在其中#SFTPDWN))。 Can anybody please tell me how to zip everything inside #SFTPDWN folder? 有人可以告诉我如何将所有内容压缩在#SFTPDWN文件夹中吗?

Code

from zipfile import ZipFile
import os


def get_all_file_paths(directory):
    file_paths = []

    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)

    return file_paths


def main():
    # path to folder which needs to be zipped
    directory = 'C:\#SFTPDWN'

    file_paths = get_all_file_paths(directory)

    print('Following files will be zipped:')
    for file_name in file_paths:
        print(file_name)

    with ZipFile('CC-Data.zip', 'w') as zip:
        # writing each file one by one
        for file in file_paths:
            zip.write(file)

    print('Zipped successfully!')


if __name__ == "__main__":
    main()

For the password question: from the documentation: 对于密码问题:来自文档:

This module [...] supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file . 此模块支持ZIP存档中加密文件的解密, 但是当前无法创建加密文件 Decryption is extremely slow as it is implemented in native Python rather than C. 解密非常慢,因为它是在本机Python中而不是C中实现的。

You would need to use a 3rd party library to create an encrypted zip, or encrypt the archive some other way. 您可能需要使用3rd party库来创建加密的zip或以其他方式加密存档。

For the second part, in ZipFile.write the documentation also mentions: 对于第二部分,在ZipFile.write ,文档还提到:

ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename , but without a drive letter and with leading path separators removed). 将名为filename的文件写入存档, 并为其指定存档名称arcname (默认情况下,该名称filename相同,但不带驱动器号,并且删除前导路径分隔符)。 [...] [...]

Note: Archive names should be relative to the archive root, that is, they should not start with a path separator. 注意:存档名称应相对于存档根目录,即,它们不应以路径分隔符开头。

So you would need to strip off whatever prefix of your file variable and pass that as the arcname parameter. 因此,您将需要剥离file变量的任何前缀,并将其作为arcname参数传递。 Using os.path.relpath may help, eg (I'm on Linux, but should work with Windows paths under Windows): 使用os.path.relpath可能会有所帮助,例如(我在Linux上,但应该在Windows下使用Windows路径):

>>> os.path.relpath("/folder/subpath/myfile.txt", "/folder/")
'subpath/myfile.txt'

Sidebar: a path like "C:\\Something" is an illegal string, as it has the escape \\S . 边栏:诸如"C:\\Something"类的路径是非法字符串,因为它具有转义符\\S Python kinda tolerates this (I think in 3.8 it will error) and rewrites them literally. Python可以忍受这一点(我认为在3.8中会出错)并按字面意义重写它们。 Either use "C:\\\\Something" , r"C:\\Something" , or "C:/Something" If you attempted something like "C:\\Users" it would actually throw an error, or "C:\\nothing" it might silently do something strange... 使用"C:\\\\Something"r"C:\\Something""C:/Something"如果尝试执行类似"C:\\Users"则实际上会引发错误,或者是"C:\\nothing"它可能会默默地做一些奇怪的事情...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM