Python zipfile setpassword không hoạt động

Định dạng tệp ZIP là một tiêu chuẩn lưu trữ và nén phổ biến. Mô-đun này cung cấp các công cụ để tạo, đọc, viết, nối thêm và liệt kê tệp ZIP. Bất kỳ việc sử dụng nâng cao nào của mô-đun này sẽ yêu cầu hiểu biết về định dạng, như được định nghĩa trong Ghi chú Ứng dụng PKZIP

Mô-đun này hiện không xử lý các tệp ZIP nhiều đĩa. Nó có thể xử lý các tệp ZIP sử dụng phần mở rộng ZIP64 (tức là các tệp ZIP có kích thước lớn hơn 4 GiB). 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

Mô-đun xác định các mục sau

ngoại lệ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
0

Đã xảy ra lỗi đối với các tệp ZIP xấu

Mới trong phiên bản 3. 2

ngoại lệ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2

Bí danh của , để tương thích với các phiên bản Python cũ hơn

Không dùng nữa kể từ phiên bản 3. 2

ngoại lệ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
5

Lỗi phát sinh khi tệp ZIP yêu cầu chức năng ZIP64 nhưng chức năng đó chưa được bật

lớp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
7

Lớp đọc và ghi tệp ZIP. Xem phần để biết chi tiết về nhà xây dựng

lớp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9

Lớp tạo kho lưu trữ ZIP chứa thư viện Python

lớp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
1 ( filename='NoName', date_time=(1980, 1, 1, 0, 0, 0) )

Lớp được sử dụng để biểu diễn thông tin về thành viên của kho lưu trữ. Các thể hiện của lớp này được trả về bởi các phương thức và của các đối tượng. Hầu hết người dùng của mô-đun sẽ không cần tạo những thứ này mà chỉ sử dụng những thứ được tạo bởi mô-đun này. tên tệp phải là tên đầy đủ của thành viên lưu trữ và date_time phải là một bộ chứa sáu trường mô tả thời gian sửa đổi lần cuối đối với tệp;

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50_______57 ( tên tệp )

Trả về

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
8 nếu tên tệp là tệp ZIP hợp lệ dựa trên số ma thuật của nó, nếu không thì trả về
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
9. tên tệp cũng có thể là tệp hoặc đối tượng giống như tệp

Đã thay đổi trong phiên bản 3. 1. Hỗ trợ tệp và các đối tượng giống như tệp.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile
1

Hằng số cho một thành viên lưu trữ không nén

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile
3

Hằng số cho phương pháp nén ZIP thông thường. Điều này yêu cầu mô-đun

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile
6

Hằng số cho phương pháp nén BZIP2. Điều này yêu cầu mô-đun

Mới trong phiên bản 3. 3

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile
9

Hằng số cho phương pháp nén LZMA. Điều này yêu cầu mô-đun

Mới trong phiên bản 3. 3

Ghi chú

Đặc tả định dạng tệp ZIP đã bao gồm hỗ trợ nén bzip2 từ năm 2001 và nén LZMA từ năm 2006. Tuy nhiên, một số công cụ (bao gồm cả các bản phát hành Python cũ hơn) không hỗ trợ các phương thức nén này và có thể từ chối xử lý toàn bộ tệp ZIP hoặc không giải nén được các tệp riêng lẻ

Xem thêm

Ghi chú ứng dụng PKZIP Tài liệu về định dạng tệp ZIP của Phil Katz, người tạo ra định dạng và thuật toán được sử dụng. Trang chủ Info-ZIPThông tin về các chương trình lưu trữ ZIP và thư viện phát triển của dự án Info-ZIP

13. 5. 1. Đối tượng ZipFile

lớp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
7 ( tệp, chế độ='r', nén=ZIP_STORED, allowZip64=True )

Mở tệp ZIP, trong đó tệp có thể là đường dẫn đến tệp (chuỗi), đối tượng giống như tệp hoặc. Tham số chế độ phải là

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43 để đọc tệp hiện có,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44 để cắt bớt và ghi tệp mới,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45 để nối thêm vào tệp hiện có hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 để tạo và ghi tệp mới. Nếu chế độ là
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 và tệp đề cập đến một tệp hiện có, a sẽ được nâng lên. Nếu chế độ là
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45 và tệp đề cập đến một tệp ZIP hiện có, thì các tệp bổ sung sẽ được thêm vào tệp đó. Nếu tệp không tham chiếu đến tệp ZIP thì tệp lưu trữ ZIP mới sẽ được thêm vào tệp. Điều này có nghĩa là để thêm tệp lưu trữ ZIP vào một tệp khác (chẳng hạn như
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
00). Nếu chế độ là
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45 và tệp hoàn toàn không tồn tại, nó sẽ được tạo. Nếu chế độ là
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45, tệp có thể tìm kiếm được. nén là phương pháp nén ZIP được sử dụng khi ghi tệp lưu trữ và phải là , , hoặc ; . Nếu , hoặc được chỉ định nhưng mô-đun tương ứng (, hoặc ) không khả dụng, sẽ được nâng lên. mặc định là. Nếu allowZip64 là tệp zip
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
8 (mặc định) sẽ tạo tệp ZIP sử dụng phần mở rộng ZIP64 khi tệp zip lớn hơn 4 GiB. Nếu sai sẽ đưa ra một ngoại lệ khi tệp ZIP yêu cầu phần mở rộng ZIP64

Nếu tệp được tạo với chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45 và sau đó không thêm bất kỳ tệp nào vào kho lưu trữ, cấu trúc ZIP thích hợp cho kho lưu trữ trống sẽ được ghi vào tệp

ZipFile cũng là một trình quản lý bối cảnh và do đó hỗ trợ câu lệnh. Trong ví dụ này, myzip bị đóng sau khi bộ câu lệnh kết thúc—ngay cả khi có ngoại lệ xảy ra

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
8

Mới trong phiên bản 3. 2. Đã thêm khả năng sử dụng làm trình quản lý ngữ cảnh.

Đã thay đổi trong phiên bản 3. 3. Đã thêm hỗ trợ và nén.

Đã thay đổi trong phiên bản 3. 4. Tiện ích ZIP64 được bật theo mặc định.

Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ ghi vào các luồng không thể truy cập. Đã thêm hỗ trợ cho chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46.

Đã thay đổi trong phiên bản 3. 6. Trước đây, một đồng bằng đã được nâng lên cho các giá trị nén không được công nhận.

Đã thay đổi trong phiên bản 3. 6. 2. Tham số tệp chấp nhận một.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
621 ()

Đóng tệp lưu trữ. Bạn phải gọi điện trước khi thoát khỏi chương trình của mình, nếu không hồ sơ thiết yếu sẽ không được ghi

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
624 ( tên )

Trả về một đối tượng có thông tin về tên thành viên lưu trữ. Gọi tên hiện không có trong kho lưu trữ sẽ gây ra một

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
629 ()

Trả về một danh sách chứa một đối tượng cho mỗi thành viên của kho lưu trữ. Các đối tượng có cùng thứ tự với các mục nhập của chúng trong tệp ZIP thực trên đĩa nếu một kho lưu trữ hiện có được mở

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
502 ()

Trả về danh sách các thành viên lưu trữ theo tên

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
504 ( name, mode='r', pwd=None, *, force_zip64=False )

Truy cập một thành viên của kho lưu trữ dưới dạng một đối tượng giống như tệp nhị phân. tên có thể là tên của một tệp trong kho lưu trữ hoặc một đối tượng. Tham số chế độ, nếu có, phải là

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43 (mặc định) hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44. pwd là mật khẩu được sử dụng để giải mã các tệp ZIP được mã hóa

cũng là một trình quản lý bối cảnh và do đó hỗ trợ tuyên bố

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())

Với chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43, đối tượng giống như tệp (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
01) ở chế độ chỉ đọc và cung cấp các phương thức sau. , , , ,. Các đối tượng này có thể hoạt động độc lập với ZipFile

Với

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07, một xử lý tệp có thể ghi được trả về, hỗ trợ phương thức. Trong khi phần xử lý tệp có thể ghi đang mở, việc cố gắng đọc hoặc ghi các tệp khác trong tệp ZIP sẽ làm tăng

Khi ghi tệp, nếu kích thước tệp không được biết trước nhưng có thể vượt quá 2 GiB, hãy vượt qua

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 để đảm bảo rằng định dạng tiêu đề có khả năng hỗ trợ các tệp lớn. Nếu kích thước tệp được biết trước, hãy tạo một đối tượng với tập hợp và sử dụng đối tượng đó làm tham số tên

Ghi chú

Các phương thức , và có thể lấy tên tệp hoặc đối tượng. Bạn sẽ đánh giá cao điều này khi cố gắng đọc tệp ZIP chứa các thành viên có tên trùng lặp

Đã thay đổi trong phiên bản 3. 6. Đã xóa hỗ trợ của

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17. Dùng để đọc file văn bản nén ở chế độ.

Đã thay đổi trong phiên bản 3. 6. hiện có thể được sử dụng để ghi tệp vào kho lưu trữ với tùy chọn

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07.

Đã thay đổi trong phiên bản 3. 6. Việc gọi một ZipFile đã đóng sẽ làm tăng. Trước đây, a đã được nâng lên.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
25 ( thành viên, path=None, pwd=None )

Trích xuất một thành viên từ kho lưu trữ vào thư mục làm việc hiện tại; . Thông tin tệp của nó được trích xuất chính xác nhất có thể. đường dẫn chỉ định một thư mục khác để giải nén vào. thành viên có thể là tên tệp hoặc đối tượng. pwd là mật khẩu được sử dụng cho các tệp được mã hóa

Trả về đường dẫn đã chuẩn hóa được tạo (một thư mục hoặc tệp mới)

Ghi chú

Nếu tên tệp thành viên là một đường dẫn tuyệt đối, một điểm chia sẻ ổ đĩa/UNC và dấu gạch chéo đầu (lưng) sẽ bị xóa, e. g.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
28 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
29 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
30 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
31 trên Windows. Và tất cả các thành phần
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
32 trong tên tệp thành viên sẽ bị xóa, e. g.
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
33 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
34. Trên Windows, các ký tự không hợp lệ (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
35,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
36,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
37,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
38,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
39,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
40 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41) được thay thế bằng dấu gạch dưới (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
42)

Đã thay đổi trong phiên bản 3. 6. Việc gọi một ZipFile đã đóng sẽ làm tăng. Trước đây, a đã được nâng lên.

Đã thay đổi trong phiên bản 3. 6. 2. Tham số đường dẫn chấp nhận một.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
47 ( path=None, members=None, pwd=None )

Trích xuất tất cả các thành viên từ kho lưu trữ vào thư mục làm việc hiện tại. đường dẫn chỉ định một thư mục khác để giải nén vào. thành viên là tùy chọn và phải là một tập hợp con của danh sách được trả về bởi. pwd là mật khẩu được sử dụng cho các tệp được mã hóa

Cảnh báo

Không bao giờ trích xuất tài liệu lưu trữ từ các nguồn không đáng tin cậy mà không kiểm tra trước. Có thể các tệp được tạo bên ngoài đường dẫn, e. g. các thành viên có tên tệp tuyệt đối bắt đầu bằng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
49 hoặc tên tệp có hai dấu chấm
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
32. Mô-đun này cố gắng ngăn chặn điều đó. Xem chú thích

Đã thay đổi trong phiên bản 3. 6. Việc gọi một ZipFile đã đóng sẽ làm tăng. Trước đây, a đã được nâng lên.

Đã thay đổi trong phiên bản 3. 6. 2. Tham số đường dẫn chấp nhận một.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
56 ()

In mục lục cho kho lưu trữ tới

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
57

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
59 ( pwd )

Đặt pwd làm mật khẩu mặc định để giải nén các tệp được mã hóa

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
61 ( tên, pwd=None )

Trả lại các byte của tên tệp trong kho lưu trữ. tên là tên của tệp trong kho lưu trữ hoặc đối tượng. Kho lưu trữ phải được mở để đọc hoặc nối thêm. pwd là mật khẩu được sử dụng cho các tệp được mã hóa và nếu được chỉ định, nó sẽ ghi đè mật khẩu mặc định được đặt bằng. Gọi một ZipFile sử dụng phương pháp nén khác với , , hoặc sẽ tăng. Lỗi cũng sẽ xuất hiện nếu mô-đun nén tương ứng không khả dụng

Đã thay đổi trong phiên bản 3. 6. Việc gọi một ZipFile đã đóng sẽ làm tăng. Trước đây, a đã được nâng lên.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
74 ()

Đọc tất cả các tệp trong kho lưu trữ và kiểm tra CRC của chúng và tiêu đề tệp. Trả về tên của tệp xấu đầu tiên, nếu không thì trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
75

Đã thay đổi trong phiên bản 3. 6. Gọi

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
76 trên ZipFile đã đóng sẽ tăng. Trước đây, a đã được nâng lên.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
80 ( tên tệp, tên cung=Không, nén_type=Không )

Ghi tệp có tên tệp vào kho lưu trữ, đặt cho nó tên kho lưu trữ là tên vòng cung (theo mặc định, tên này sẽ giống với tên tệp, nhưng không có ký tự ổ đĩa và đã xóa các dấu phân cách đường dẫn ở đầu). Nếu được cung cấp, nén_type sẽ ghi đè giá trị đã cung cấp cho tham số nén cho hàm tạo cho mục nhập mới. Kho lưu trữ phải được mở với chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45

Ghi chú

Không có mã hóa tên tệp chính thức cho các tệp ZIP. Nếu bạn có tên tệp unicode, bạn phải chuyển đổi chúng thành chuỗi byte theo cách mã hóa mong muốn trước khi chuyển chúng tới. WinZip diễn giải tất cả các tên tệp dưới dạng được mã hóa trong CP437, còn được gọi là DOS Latin

Ghi chú

Tên lưu trữ phải liên quan đến thư mục gốc, nghĩa là chúng không được bắt đầu bằng dấu phân cách đường dẫn

Ghi chú

Nếu

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
85 (hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
86, nếu không cung cấp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
85) chứa byte rỗng, tên của tệp trong kho lưu trữ sẽ bị cắt bớt ở byte rỗng

Đã thay đổi trong phiên bản 3. 6. Gọi một ZipFile được tạo bằng chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43 hoặc một ZipFile đã đóng sẽ tăng. Trước đây, a đã được nâng lên.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
93 ( zinfo_or_arcname, data [ , nén_type< ])

Ghi dữ liệu chuỗi vào kho lưu trữ; . Nếu đó là một phiên bản, ít nhất phải cung cấp tên tệp, ngày và giờ. Nếu đó là tên, ngày và giờ được đặt thành ngày và giờ hiện tại. Kho lưu trữ phải được mở với chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45

Nếu được cung cấp, kiểu nén sẽ ghi đè giá trị đã cung cấp cho tham số nén vào hàm tạo cho mục nhập mới hoặc trong zinfo_or_arcname (nếu đó là một ví dụ)

Ghi chú

Khi truyền một thể hiện dưới dạng tham số zinfo_or_arcname, phương pháp nén được sử dụng sẽ là phương thức được chỉ định trong thành viên nén_type của thể hiện đã cho. Theo mặc định, hàm tạo đặt thành viên này thành

Đã thay đổi trong phiên bản 3. 2. Đối số kiểu nén.

Đã thay đổi trong phiên bản 3. 6. Gọi một ZipFile được tạo bằng chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
43 hoặc một ZipFile đã đóng sẽ tăng. Trước đây, a đã được nâng lên.

Các thuộc tính dữ liệu sau đây cũng có sẵn

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
08

Tên của tệp ZIP

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
10

Mức đầu ra gỡ lỗi để sử dụng. Điều này có thể được đặt từ

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
11 (mặc định, không có đầu ra) thành
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
12 (đầu ra nhiều nhất). Thông tin gỡ lỗi được ghi vào
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
57

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
620
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
15

Văn bản nhận xét được liên kết với tệp ZIP. Nếu chỉ định nhận xét cho một phiên bản được tạo bằng chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
44,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
46 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
45, thì đây phải là một chuỗi không dài hơn 65535 byte. Nhận xét dài hơn mức này sẽ bị cắt bớt trong kho lưu trữ bằng văn bản khi được gọi

13. 5. 2. Đối tượng PyZipFile

Hàm tạo nhận các tham số giống như hàm tạo và một tham số bổ sung, tối ưu hóa

lớp
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9 ( tệp, chế độ='r', nén=ZIP_STORED, allowZip64=True, Optimize=-1 )

Mới trong phiên bản 3. 2. Tham số tối ưu hóa.

Đã thay đổi trong phiên bản 3. 4. Tiện ích ZIP64 được bật theo mặc định.

Các trường hợp có một phương thức ngoài các phương thức của các đối tượng

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
26 ( tên đường dẫn, tên cơ sở='', filterfunc=None )

Tìm kiếm tệp

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
27 và thêm tệp tương ứng vào kho lưu trữ

Nếu tham số tối ưu hóa không được cung cấp hoặc

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
29, tệp tương ứng là tệp
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
30, biên dịch nếu cần

Nếu tham số tối ưu hóa là

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
11,
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
33 hoặc
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
34, thì chỉ các tệp có mức tối ưu hóa đó (xem ) mới được thêm vào kho lưu trữ, biên dịch nếu cần

Nếu tên đường dẫn là một tệp, tên tệp phải kết thúc bằng

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
36 và chỉ tệp (tương ứng với
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
30) được thêm vào ở cấp cao nhất (không có thông tin đường dẫn). Nếu tên đường dẫn là một tệp không kết thúc bằng
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
36, a sẽ được nâng lên. Nếu đó là một thư mục và thư mục không phải là thư mục gói, thì tất cả các tệp
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
30 sẽ được thêm vào ở cấp cao nhất. Nếu thư mục là một thư mục gói, thì tất cả
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
30 được thêm vào bên dưới tên gói dưới dạng đường dẫn tệp và nếu bất kỳ thư mục con nào là thư mục gói, tất cả những thư mục này sẽ được thêm vào theo cách đệ quy

tên cơ sở chỉ dành cho sử dụng nội bộ

filterfunc, nếu được cung cấp, phải là một hàm nhận một đối số chuỗi đơn. Nó sẽ được chuyển qua từng đường dẫn (bao gồm từng đường dẫn tệp đầy đủ riêng lẻ) trước khi được thêm vào kho lưu trữ. Nếu filterfunc trả về giá trị sai, đường dẫn sẽ không được thêm vào và nếu đó là một thư mục thì nội dung của nó sẽ bị bỏ qua. Ví dụ: nếu tất cả các tệp thử nghiệm của chúng tôi đều nằm trong thư mục

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
42 hoặc bắt đầu bằng chuỗi
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
43, chúng tôi có thể sử dụng bộ lọcfunc để loại trừ chúng

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)

Phương pháp tạo các tệp lưu trữ có tên tệp như thế này

string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile

Mới trong phiên bản 3. 4. Tham số filterfunc.

Đã thay đổi trong phiên bản 3. 6. 2. Tham số tên đường dẫn chấp nhận một.

13. 5. 3. Đối tượng ZipInfo

Các thể hiện của lớp được trả về bởi các phương thức và đối tượng. Mỗi đối tượng lưu trữ thông tin về một thành viên của kho lưu trữ ZIP

Có một phương thức phân loại để tạo một thể hiện cho tệp hệ thống tệp

classmethod
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
51 ( filename, arcname=None )

Xây dựng một phiên bản cho một tệp trên hệ thống tệp, để chuẩn bị thêm tệp đó vào tệp zip

tên tệp phải là đường dẫn đến tệp hoặc thư mục trên hệ thống tệp

Nếu tên cung được chỉ định, nó sẽ được sử dụng làm tên trong kho lưu trữ. Nếu tên cung không được chỉ định, tên sẽ giống như tên tệp, nhưng với bất kỳ ký tự ổ đĩa và dấu tách đường dẫn đầu nào đã bị xóa

Mới trong phiên bản 3. 6

Đã thay đổi trong phiên bản 3. 6. 2. Tham số tên tệp chấp nhận một.

Các trường hợp có các phương thức và thuộc tính sau

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
54 ()

Trả lại

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
8 nếu thành viên lưu trữ này là một thư mục

Điều này sử dụng tên của mục nhập. các thư mục phải luôn kết thúc bằng

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
56

Mới trong phiên bản 3. 6

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
08

Tên của tập tin trong kho lưu trữ

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
60

Thời gian và ngày sửa đổi lần cuối đối với thành viên lưu trữ. Đây là một bộ sáu giá trị

Giá trị chỉ mục
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
11Năm (>= 1980)
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
33Tháng (dựa trên một)
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
34Ngày trong tháng (dựa trên một)
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
12Giờ (dựa trên 0)
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
65Phút (dựa trên 0)
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
66Giây (dựa trên 0)

Ghi chú

Định dạng tệp ZIP không hỗ trợ dấu thời gian trước năm 1980

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
68

Loại nén cho thành viên lưu trữ

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
15

Nhận xét cho thành viên lưu trữ cá nhân

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
72

Dữ liệu trường mở rộng. Ghi chú Ứng dụng PKZIP chứa một số nhận xét về cấu trúc bên trong của dữ liệu chứa trong chuỗi này

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
74

Hệ thống đã tạo kho lưu trữ ZIP

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
76

Phiên bản PKZIP đã tạo kho lưu trữ ZIP

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
78

Phiên bản PKZIP cần thiết để giải nén kho lưu trữ

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
80

phải bằng không

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
82

Bit cờ ZIP

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
84

Số lượng của tiêu đề tập tin

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
86

Thuộc tính nội bộ

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
88

Thuộc tính tệp bên ngoài

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
90

Bù byte cho tiêu đề tệp

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
92

CRC-32 của tệp không nén

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
94

Kích thước của dữ liệu nén

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
50
>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
..     fn = os.path.basename(s)
..     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)
96

Kích thước của tệp không nén

13. 5. 4. Giao diện dòng lệnh

Mô-đun cung cấp giao diện dòng lệnh đơn giản để tương tác với kho lưu trữ ZIP

Nếu bạn muốn tạo một kho lưu trữ ZIP mới, hãy chỉ định tên của nó sau tùy chọn và sau đó liệt kê (các) tên tệp cần được đưa vào

Làm cách nào để đặt mật khẩu cho tệp ZIP trong Python?

Làm cách nào để bảo vệ tệp zip bằng mật khẩu trong Python? .
đường dẫn tệp src (chuỗi)
đường dẫn tiền tố tệp src (chuỗi) hoặc Không có (đường dẫn để thêm vào tệp)
đườ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ó mật khẩu)
compress_level(int) between 1 to 9, 1 (more fast) <—> 9 (more compress) or 0 (default).

Tệp ZIP có được bao gồm trong Python không?

Chúng bao gồm các thư viện cấp thấp để nén và giải nén dữ liệu bằng các thuật toán nén cụ thể, chẳng hạn như zlib , bz2 , lzma và các thuật toán khác. Python cũng cung cấp một mô-đun cấp cao gọi là zipfile được thiết kế đặc biệt để tạo, đọc, viết, trích xuất và liệt kê nội dung của tệp ZIP.

Làm cách nào để cài đặt gói tệp ZIP trong Python?

Để biết thêm thông tin, hãy xem Python Setuptools. .
Tải xuống tệp zip pynrfjprog
Giải nén tệp zip đã nén và mở cửa sổ Dấu nhắc Lệnh trong thư mục đó
Nhập thiết lập python. py cài đặt tại Dấu nhắc Lệnh. Nội dung của gói sẽ được thêm vào thư mục mặc định của Python

Câu lệnh nào tạo thành công tệp ZIP bằng mô-đun tệp ZIP trong Python?

Tạo một đối tượng ZipFile bằng cách chuyển tên và chế độ tệp mới thành 'w' (chế độ ghi). Nó sẽ tạo một tệp zip mới và mở nó trong đối tượng ZipFile. Gọi hàm write() trên đối tượng ZipFile để thêm các tệp trong đó. gọi close() trên đối tượng ZipFile để đóng tệp zip