Làm cách nào để giải nén tệp zip bằng python?

Trong Python, bạn có thể nén và giải nén tệp, tôi. e. , nén tệp thành tệp ZIP và giải nén tệp ZIP bằng mô-đun zipfile

  • zipfile — Làm việc với kho lưu trữ ZIP — Python 3. 10. 2 tài liệu

Ngoài ra, bạn có thể dễ dàng nén một thư mục [thư mục] và giải nén tệp ZIP bằng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
2 và
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
3 của mô-đun Shutil

Cả hai đều có trong thư viện chuẩn nên không cần cài đặt thêm

Bài viết này mô tả các nội dung sau

  • Zip một thư mục [thư mục].
    shutil.make_archive['archive_shutil_base', format='zip',
                        root_dir='.', base_dir='dir_zip']
    
    4
  • Giải nén một tập tin.
    shutil.make_archive['archive_shutil_base', format='zip',
                        root_dir='.', base_dir='dir_zip']
    
    5
  • Khái niệm cơ bản về mô-đun zipfile.
    shutil.make_archive['archive_shutil_base', format='zip',
                        root_dir='.', base_dir='dir_zip']
    
    6 đối tượng
  • Nén các tệp riêng lẻ thành một tệp ZIP
  • Thêm các tệp khác vào tệp ZIP hiện có
  • Kiểm tra danh sách các tệp trong tệp ZIP
  • Trích xuất các tệp riêng lẻ từ tệp ZIP
  • Đọc tệp trong tệp ZIP
  • ZIP có mật khẩu [mã hóa và giải mã]

Liên kết được tài trợ

Zip một thư mục [thư mục].
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
4

Bạn có thể nén một thư mục [folder], tôi. e. , tạo một tệp ZIP từ một thư mục có

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
4

Tham số đầu tiên

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
9 là đường dẫn không có phần mở rộng của tệp ZIP cần tạo, tham số thứ hai
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
0 là định dạng lưu trữ [
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
1,
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
2,
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
3,
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
4,
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
5] và tham số thứ ba
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
6 là đường dẫn của thư mục cần nén

Ví dụ: giả sử có một thư mục

shutil.unpack_archive['archive_shutil.zip', 'dir_out']
7 với cấu trúc sau trong thư mục hiện tại

dir_zip
├── dir_sub
│   └── file_sub.txt
└── file.txt

Nén thư mục này thành tệp ZIP

shutil.unpack_archive['archive_shutil.zip', 'dir_out']
8 trong thư mục hiện tại

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']

nguồn.

Trong trường hợp này, bản thân thư mục được chỉ định

shutil.unpack_archive['archive_shutil.zip', 'dir_out']
7 không được bao gồm trong
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
8

Nếu bạn muốn bao gồm chính thư mục đó, hãy chỉ định đường dẫn của thư mục trên của thư mục đích trong tham số thứ ba

shutil.unpack_archive['archive_shutil.zip', 'dir_out']
6 và đường dẫn tương đối của thư mục đích từ
shutil.unpack_archive['archive_shutil.zip', 'dir_out']
6 trong tham số thứ tư
dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
3

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']

nguồn.

Xem phần tiếp theo để biết kết quả giải nén

Giải nén một tập tin.
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
5

Bạn có thể giải nén một tập tin, tôi. e. , trích xuất tất cả nội dung của tệp ZIP bằng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
5

Tham số đầu tiên

dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
6 là đường dẫn của tệp ZIP và tham số thứ hai
dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
7 là đường dẫn của thư mục đích nơi tệp lưu trữ được trích xuất

________số 8_______

nguồn.

Nó được trích xuất như sau

dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt

Mặc dù tài liệu không chỉ định nó, nhưng nó dường như tạo một thư mục mới ngay cả khi

dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
7 không tồn tại [đã được xác nhận trong Python 3. 9. 9]

Tệp ZIP được tạo bởi

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
4 với
dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
3 được trích xuất như sau

shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']

nguồn.

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt

Khái niệm cơ bản về mô-đun zipfile.
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 đối tượng

Mô-đun zipfile cung cấp lớp

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 để tạo, đọc, viết, nối thêm và liệt kê một tệp ZIP

Các đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 được tạo bằng cách chỉ định tham số đầu tiên
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
4 [đường dẫn của tệp ZIP] và tham số thứ hai
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 [đọc
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
6, viết
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
7, nối thêm
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
8, v.v. ] cho nhà xây dựng
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
9

Đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 cần được đóng lại bằng phương thức
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
1, nhưng nếu bạn sử dụng câu lệnh
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
2, nó sẽ tự động được đóng lại khi khối kết thúc

Cách sử dụng cũng tương tự như đọc ghi file với hàm có sẵn

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3, chẳng hạn như chỉ định chế độ và sử dụng câu lệnh
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
2

Các ví dụ cụ thể được mô tả trong các phần sau

Nén các tệp riêng lẻ thành một tệp ZIP

Để nén các tệp riêng lẻ thành một tệp ZIP, hãy tạo một đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 mới và thêm các tệp bạn muốn nén bằng phương pháp
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6

Với

shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
9, chỉ định đường dẫn của tệp ZIP mới được tạo làm tham số đầu tiên
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
4 và đặt tham số thứ hai
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 thành
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
7 [ghi]

Ở chế độ ghi, bạn cũng có thể chỉ định phương pháp nén và mức độ với các tham số

import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
1 và
import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
2

Phương pháp nén

import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
1 như sau;

  • import zipfile
    
    with zipfile.ZipFile['archive_zipfile.zip', 'w',
                         compression=zipfile.ZIP_DEFLATED,
                         compresslevel=9] as zf:
        zf.write['dir_zip/file.txt', arcname='file.txt']
        zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
    
    4. Không nén [mặc định]
  • import zipfile
    
    with zipfile.ZipFile['archive_zipfile.zip', 'w',
                         compression=zipfile.ZIP_DEFLATED,
                         compresslevel=9] as zf:
        zf.write['dir_zip/file.txt', arcname='file.txt']
        zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
    
    5. Nén ZIP thông thường
  • import zipfile
    
    with zipfile.ZipFile['archive_zipfile.zip', 'w',
                         compression=zipfile.ZIP_DEFLATED,
                         compresslevel=9] as zf:
        zf.write['dir_zip/file.txt', arcname='file.txt']
        zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
    
    6. nén BZIP2
  • import zipfile
    
    with zipfile.ZipFile['archive_zipfile.zip', 'w',
                         compression=zipfile.ZIP_DEFLATED,
                         compresslevel=9] as zf:
        zf.write['dir_zip/file.txt', arcname='file.txt']
        zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
    
    7. Nén LZMA

Đối với

import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
8, mức độ nén
import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
2 tương ứng với
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
0 của
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
1. Mặc định là
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
2 [
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
3]

level là mức nén – một số nguyên từ 0 đến 9 hoặc -1. Giá trị 1 [Z_BEST_SPEED] là nhanh nhất và tạo ra ít nén nhất, trong khi giá trị 9 [Z_BEST_COMPRESSION] là chậm nhất và tạo ra nhiều nén nhất. 0 [Z_NO_COMPRESSION] không nén. Giá trị mặc định là -1 [Z_DEFAULT_COMPRESSION]. Z_DEFAULT_COMPRESSION thể hiện sự thỏa hiệp mặc định giữa tốc độ và độ nén [hiện tại tương đương với cấp 6].

Phương thức

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6 của đối tượng
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 ghi tệp có tên tham số đầu tiên là
dir_out
├── dir_sub
│   └── file_sub.txt
└── file.txt
6 vào một tệp ZIP, cung cấp cho nó tên lưu trữ [= tên trong ZIP] tham số thứ hai là
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
7. Nếu
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
7 bị bỏ qua, thì ____23_______6 được sử dụng làm tên lưu trữ. Bạn có thể chỉ định cấu trúc thư mục cho
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
7

import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']

nguồn.

Bạn cũng có thể chọn phương pháp nén và cấp độ cho từng tệp bằng cách chỉ định

with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    with zf.open['dir_sub/new_file.txt', 'w'] as f:
        f.write[b'text in new file']
1 và
import zipfile

with zipfile.ZipFile['archive_zipfile.zip', 'w',
                     compression=zipfile.ZIP_DEFLATED,
                     compresslevel=9] as zf:
    zf.write['dir_zip/file.txt', arcname='file.txt']
    zf.write['dir_zip/dir_sub/file_sub.txt', arcname='dir_sub/file_sub.txt']
2 trong phương thức
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6

Thêm các tệp khác vào tệp ZIP hiện có

Để thêm các tệp khác vào tệp ZIP hiện có, với

shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
9, hãy đặt tham số đầu tiên
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
4 thành đường dẫn của tệp ZIP hiện có và tham số thứ hai
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 thành
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
8 [nối thêm]

Thêm các tệp hiện có

Bạn có thể thêm các tệp hiện có bằng phương thức

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6 của đối tượng
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6

Sau đây là một ví dụ về việc thêm

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
00 vào thư mục hiện tại. Đối số
with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']
7 bị bỏ qua

with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    zf.write['another_file.txt']

nguồn.

Tạo và thêm một tệp mới

Bạn cũng có thể tạo một tệp mới và thêm nó. Sử dụng phương thức

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3 của đối tượng
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 với chế độ chắp thêm [
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
8]

Chỉ định đường dẫn của tệp mới tạo trong ZIP làm tham số đầu tiên và đặt tham số thứ hai

shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 thành
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
7

Bạn có thể viết nội dung bằng phương thức

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6 của đối tượng tệp đã mở

with zipfile.ZipFile['archive_zipfile.zip', 'a'] as zf:
    with zf.open['dir_sub/new_file.txt', 'w'] as f:
        f.write[b'text in new file']

nguồn.

Đối số của

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
6 nên được chỉ định là
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
09, không phải là
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
10. Để viết một văn bản, hãy sử dụng
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
11 hoặc chuyển đổi nó bằng phương pháp
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
12 của
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
10

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
0

nguồn.

Một ví dụ về việc đọc một tệp trong ZIP với

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3 của đối tượng
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 được mô tả sau

Liên kết được tài trợ

Kiểm tra danh sách các tệp trong tệp ZIP

Để kiểm tra nội dung của tệp ZIP hiện có, hãy tạo một đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 với tham số đầu tiên là
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
4 là đường dẫn của tệp ZIP hiện có và tham số thứ hai là
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 là
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
6 [đã đọc]. Có thể bỏ qua
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 vì giá trị mặc định là
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
6

Bạn có thể lấy danh sách các mục đã lưu trữ bằng phương thức

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
22 của đối tượng
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
1

nguồn.

Như bạn có thể thấy từ kết quả ở trên, các tệp ZIP được tạo bằng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
4 cũng liệt kê các thư mục riêng lẻ. Điều này cũng đúng với các tệp ZIP được nén bằng chức năng tiêu chuẩn của Finder trên máy Mac

Bạn có thể loại trừ các thư mục có khả năng hiểu danh sách

  • Danh sách hiểu trong Python

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
2

nguồn.

Trích xuất các tệp riêng lẻ từ tệp ZIP

Để giải nén tệp ZIP, hãy tạo một đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 ở chế độ đọc [
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
6, mặc định]

Nếu bạn chỉ muốn giải nén các tệp cụ thể, hãy sử dụng phương pháp

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
27

Tham số đầu tiên

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
28 là tên của tệp cần giải nén [bao gồm cả thư mục trong tệp zip] và tham số thứ hai
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
29 là đường dẫn đến thư mục cần giải nén

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
3

nguồn.

Nếu bạn muốn giải nén tất cả các tệp, hãy sử dụng phương pháp

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
30. Chỉ định đường dẫn của thư mục để trích xuất làm đối số đầu tiên
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
29

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
4

nguồn.

Trong cả hai trường hợp, nếu bỏ qua

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
29, tệp sẽ được trích xuất vào thư mục hiện tại. Mặc dù tài liệu không chỉ định nó, nhưng nó dường như tạo một thư mục mới ngay cả khi không tồn tại
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
29 [đã xác nhận trong Python 3. 9. 9]

Đọc tệp trong tệp ZIP

Bạn có thể đọc trực tiếp các tệp trong tệp ZIP

Tạo một đối tượng

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
6 ở chế độ đọc [mặc định] và mở tệp bên trong bằng phương thức
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3

Đối số đầu tiên của

dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3 là tên của tệp trong ZIP [có thể bao gồm thư mục]. Đối số thứ hai
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
5 có thể được bỏ qua vì giá trị mặc định là
shutil.unpack_archive['archive_shutil_base.zip', 'dir_out_base']
6 [đọc]

Có thể đọc nội dung bằng phương thức

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
39 của đối tượng tệp đã mở. Một chuỗi byte
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
09 được trả về, có thể được chuyển đổi thành chuỗi
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
10 bằng phương thức
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
42

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
5

nguồn.

Ngoài

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
39, có thể sử dụng
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
44 và
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
45 cũng như đối tượng tệp được mở bằng chức năng tích hợp sẵn
dir_out_base
└── dir_zip
    ├── dir_sub
    │   └── file_sub.txt
    └── file.txt
3

ZIP có mật khẩu [mã hóa và giải mã]

Mô-đun zipfile có thể giải mã ZIP bằng mật khẩu [ZIP được mã hóa], nhưng nó không thể mã hóa ZIP

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 tệp được mã hóa. Quá trình giải mã cực kỳ chậm vì nó được triển khai bằng Python gốc chứ không phải C. zipfile — Làm việc với kho lưu trữ ZIP — Python 3. 10. 2 tài liệu

Ngoài ra, AES không được hỗ trợ

Mô-đun zipfile từ thư viện chuẩn Python chỉ hỗ trợ các tệp zip được mã hóa CRC32 [xem tại đây. http. //hg. con trăn. org/cpython/file/71adf21421d9/Lib/zipfile. py#l420 ]. zip - Python giải nén tệp được mã hóa AES-128

Cả

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
2 và
shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
3 đều không hỗ trợ mã hóa và giải mã

dây kéo

Pyzipper được giới thiệu trong Stack Overflow ở trên hỗ trợ mã hóa và giải mã AES, đồng thời có thể được sử dụng theo cách tương tự như zipfile

  • danifus/pyzipper. Phần mở rộng zipfile Python

Để tạo tệp ZIP bằng mật khẩu, hãy chỉ định

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
49 với
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
50 và đặt mật khẩu bằng phương thức
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
51. Lưu ý rằng bạn cần chỉ định mật khẩu bằng chuỗi byte
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
09

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
6

nguồn.

Sau đây là một ví dụ về giải nén tệp ZIP bằng mật khẩu

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
7

nguồn.

Tất nhiên nếu sai mật khẩu thì không thể giải mã được

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
8

nguồn.

Mô-đun zipfile cũng cho phép bạn chỉ định mật khẩu, nhưng như đã đề cập ở trên, nó không hỗ trợ AES

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
9

nguồn.

Thực hiện lệnh với
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
53

Bạn cũng có thể sử dụng

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
53 nếu zipfile hoặc pyzipper không hoạt động, nhưng lệnh vẫn có thể xử lý nó

Sử dụng lệnh

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
55 của 7-zip [yêu cầu cài đặt] làm ví dụ

shutil.make_archive['archive_shutil_base', format='zip',
                    root_dir='.', base_dir='dir_zip']
0

nguồn.

Tương đương với các lệnh sau.

import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
56 là bản mở rộng. Lưu ý rằng
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
57 và
import shutil

shutil.make_archive['archive_shutil', format='zip', root_dir='dir_zip']
58 không yêu cầu dấu cách

Chủ Đề