Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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"]
1pwdZipFilePath = 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"]
4import pyminizip
pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
1pwdZipFilePath = 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"]
1import pyminizip
pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
3import pyminizip
pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
4import pyminizip
pyminizip.compress["dummy.txt", "myzip.zip", "noneshallpass", compression_level]
5Output:
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