Hướng dẫn how to make password protected zip file in python - cách tạo tệp zip được bảo vệ bằng mật khẩu trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnpyminizip module from python.

    Installation:

    ZIP là một định dạng tệp lưu trữ hỗ trợ nén dữ liệu không mất. Bằng cách nén không mất, chúng tôi có nghĩa là thuật toán nén cho phép dữ liệu gốc được xây dựng lại hoàn hảo từ dữ liệu được nén. Vì vậy, một tệp zip là một tệp duy nhất chứa một hoặc nhiều tệp nén, cung cấp một cách lý tưởng để tạo các tệp lớn nhỏ hơn và giữ các tệp liên quan lại với nhau.The pyminizip module can be installed using the below command:

    pip install pyminizip

    Trong bài viết này, chúng tôi sẽ tìm hiểu cách tạo zip được bảo vệ bằng mật khẩu của một tệp bằng Python. Đối với điều này, chúng tôi đang sử dụng mô -đun pyminizip từ Python.compress[] method from pyminizip. So, we discuss first its syntax and arguments.

    Syntax:

    & NBSP; Mô -đun PymInizip có thể được cài đặt bằng lệnh bên dưới:

    Arguments:

    • Để tạo zip, chúng tôi đang sử dụng phương thức nén [] từ pyminizip. Vì vậy, chúng tôi thảo luận đầu tiên cú pháp và lập luận của nó.
    • pyminizip.compress [Hồi/srcfile/path.txt ,, file_path_prefix,
    • Đường dẫn tệp SRC [Chuỗi]
    • Đường dẫn tiền tố SRC [chuỗi] hoặc không
    • Đường dẫn tệp DST [chuỗi]
       

    Mật khẩu [chuỗi] hoặc không có [để tạo zip không có thông tin]Always returns None

    Implementation:

    Compress_Level [int] trong khoảng từ 1 đến 9, 1 [nhanh hơn] 9 [nén nhiều hơn] hoặc 0 [mặc định] & nbsp;

    Program:

    Python3

    Giá trị trả lại: luôn trả về không

    Tệp đầu vào:

    import pyminizip

    inpt = "./Text.txt"

    pre =

    pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
    filePath = uploadFilePath
    
    if not os.path.exists[pwdZipFilePath]:        
          os.makedirs[pwdZipFilePath]
    
    #save csv file to a path
    fd, filePath = tempfile.mkstemp[suffix=source.name, dir=filePath]
    
    with open[filePath, 'wb'] as dest:
        shutil.copyfileobj[source, dest]
    
    #convert that csv to zip
    fd, pwdZipFilePath = tempfile.mkstemp[suffix=source.name + ".zip", dir=pwdZipFilePath]
    
    with zipfile.ZipFile[pwdZipFilePath, 'w'] as myzip:
        myzip.write[filePath]
    
        myzip.setpassword[b"tipi"]
    
    1

    pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
    filePath = uploadFilePath
    
    if not os.path.exists[pwdZipFilePath]:        
          os.makedirs[pwdZipFilePath]
    
    #save csv file to a path
    fd, filePath = tempfile.mkstemp[suffix=source.name, dir=filePath]
    
    with open[filePath, 'wb'] as dest:
        shutil.copyfileobj[source, dest]
    
    #convert that csv to zip
    fd, pwdZipFilePath = tempfile.mkstemp[suffix=source.name + ".zip", dir=pwdZipFilePath]
    
    with zipfile.ZipFile[pwdZipFilePath, 'w'] as myzip:
        myzip.write[filePath]
    
        myzip.setpassword[b"tipi"]
    
    2=
    pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
    filePath = uploadFilePath
    
    if not os.path.exists[pwdZipFilePath]:        
          os.makedirs[pwdZipFilePath]
    
    #save csv file to a path
    fd, filePath = tempfile.mkstemp[suffix=source.name, dir=filePath]
    
    with open[filePath, 'wb'] as dest:
        shutil.copyfileobj[source, dest]
    
    #convert that csv to zip
    fd, pwdZipFilePath = tempfile.mkstemp[suffix=source.name + ".zip", dir=pwdZipFilePath]
    
    with zipfile.ZipFile[pwdZipFilePath, 'w'] as myzip:
        myzip.write[filePath]
    
        myzip.setpassword[b"tipi"]
    
    4

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    1
    pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
    filePath = uploadFilePath
    
    if not os.path.exists[pwdZipFilePath]:        
          os.makedirs[pwdZipFilePath]
    
    #save csv file to a path
    fd, filePath = tempfile.mkstemp[suffix=source.name, dir=filePath]
    
    with open[filePath, 'wb'] as dest:
        shutil.copyfileobj[source, dest]
    
    #convert that csv to zip
    fd, pwdZipFilePath = tempfile.mkstemp[suffix=source.name + ".zip", dir=pwdZipFilePath]
    
    with zipfile.ZipFile[pwdZipFilePath, 'w'] as myzip:
        myzip.write[filePath]
    
        myzip.setpassword[b"tipi"]
    
    1
    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    3

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    4
    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    5

    Output:

    Tôi đang sử dụng mã sau để tạo tệp zip được bảo vệ mật khẩu, từ một tệp được tải lên bởi người dùng, trong ứng dụng Python34 của tôi bằng

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    6. Nhưng khi tôi mở tệp zip từ Windows, nó không yêu cầu mật khẩu. Tôi sẽ sử dụng cùng một mật khẩu để đọc các tệp zip từ Python sau này. Tôi đang làm gì sai?

    Đây là mã của tôi:

    pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
    filePath = uploadFilePath
    
    if not os.path.exists[pwdZipFilePath]:        
          os.makedirs[pwdZipFilePath]
    
    #save csv file to a path
    fd, filePath = tempfile.mkstemp[suffix=source.name, dir=filePath]
    
    with open[filePath, 'wb'] as dest:
        shutil.copyfileobj[source, dest]
    
    #convert that csv to zip
    fd, pwdZipFilePath = tempfile.mkstemp[suffix=source.name + ".zip", dir=pwdZipFilePath]
    
    with zipfile.ZipFile[pwdZipFilePath, 'w'] as myzip:
        myzip.write[filePath]
    
        myzip.setpassword[b"tipi"]
    

    Martineau

    Huy hiệu vàng 115K2525 gold badges160 silver badges284 bronze badges

    Đã hỏi ngày 29 tháng 11 năm 2017 lúc 7:51Nov 29, 2017 at 7:51

    Tahreem iqbaltahreem iqbalTahreem Iqbal

    9356 Huy hiệu vàng16 Huy hiệu bạc41 Huy hiệu đồng6 gold badges16 silver badges41 bronze badges

    3

    Mô-đun

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    7 tích hợp không hỗ trợ viết các tệp được mã hóa mật khẩu [chỉ đọc]. Hoặc bạn có thể sử dụng pyminizip:

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    

    Hoặc, nếu bạn đang ở trên Windows/MSysgit và Agnostic theo định dạng:

    import os
    os.system['tar cz dummy.txt | openssl enc -aes-256-cbc -e -k noneshallpass > mypacked.enc']
    os.remove['dummy.txt']
    os.system['openssl enc -aes-256-cbc -d -k noneshallpass -in mypacked.enc | tar xz']
    

    Đã trả lời ngày 29 tháng 11 năm 2017 lúc 8:33Nov 29, 2017 at 8:33

    Jonas Byströmjonas ByströmJonas Byström

    24.2K22 Huy hiệu vàng98 Huy hiệu bạc142 Huy hiệu đồng22 gold badges98 silver badges142 bronze badges

    4

    Tài liệu cho

    import pyminizip
    pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
    
    7 chỉ ra rằng
    import os
    os.system['tar cz dummy.txt | openssl enc -aes-256-cbc -e -k noneshallpass > mypacked.enc']
    os.remove['dummy.txt']
    os.system['openssl enc -aes-256-cbc -d -k noneshallpass -in mypacked.enc | tar xz']
    
    0 đặt "mật khẩu mặc định để trích xuất các tệp được mã hóa".

    Ở đầu tài liệu: "Nó hỗ trợ giải mã các tệp được mã hóa trong kho lưu trữ zip, nhưng hiện tại nó không thể tạo một tệp được mã hóa."

    Chỉnh sửa: Để tạo tệp zip được bảo vệ bằng mật khẩu, hãy thử một gói như pyminizip.

    Đã trả lời ngày 29 tháng 11 năm 2017 lúc 8:10Nov 29, 2017 at 8:10

    6

    Làm cách nào để mật khẩu bảo vệ một tệp zip trong python?

    Đặt mật khẩu trên một tệp zip trong câu trả lời mã Python..
    từ Zipfile nhập zipfile ..
    nhập zipfile ..
    myzip = zipfile ['test.zip'].
    myzip. setPassword [b "alixaprodev"].
    myzip. Trích xuất [Thành viên = 'Roughwork/Pretify.html', PWD = B "Alixaprodev"].
    Mã sao chép ..

    Bạn có thể tạo mật khẩu tệp zip được bảo vệ không?

    Nếu bạn đặt các tệp bạn muốn bảo vệ trong tệp zip, thì bạn có thể áp dụng mật khẩu.Trong Windows Explorer, tô sáng và nhấp chuột phải vào các tệp bạn muốn đặt vào một tệp bị kéo dài.Chọn Gửi đến, sau đó thư mục ZIP [nén].Bấm đúp vào tệp zipped, sau đó chọn tệp và thêm mật khẩu.you can then apply a password. In Windows Explorer, highlight and right-click on the files you would like to put into a zipped file. Select Send to, then Zip folder [compressed]. Double-click the zipped file, then select File and Add Password.

    Làm cách nào để tạo tệp zip được mã hóa?

    Xác định vị trí tệp hoặc thư mục bạn muốn zip, sau đó nhấp chuột phải vào nó và chọn Thêm vào Lưu trữ ..
    Nhấp vào Đặt Mật khẩu ..
    Đặt mật khẩu bạn muốn sử dụng.....
    Bây giờ, chọn ZIP trong phần Định dạng Lưu trữ trên mạng và sau đó bấm OK ..

    Tôi có thể mật khẩu bảo vệ tệp zip 7 không

    Để bảo vệ tệp lưu trữ được tạo bởi 7-zip, bạn phải chọn mật khẩu và nhập nó hai lần vào các hộp văn bản bên dưới mã hóa.D.you must choose a password and enter it twice in the text boxes beneath “Encryption.” D.

    Bài Viết Liên Quan

    Chủ Đề