Hướng dẫn what is zipfile python? - zipfile python là gì?

Mã nguồn: lib/zipfile.py Lib/zipfile.py Lib/zipfile.py

Nội dung chính ShowShow

  • Trang chủ thông tin-Zip
  • Đối tượng đường dẫn Sở đối tượng
  • Đối tượng pyzipfile
  • Đối tượng Zipinfo
  • Zipinfo.crc¶
  • Giao diện dòng lệnh
  • Cạm bẫy giải nén
  • Từ tập tin chính nó
  • Giới hạn hệ thống tệp
  • Giới hạn tài nguyên
  • Sự gián đoạn
  • Tệp ZIP có phải là một phần của Python không?
  • Làm cách nào để cài đặt tệp zip trong Python?
  • Làm thế nào để bạn tạo một tệp zip trong Python?
  • Tệp Zip Zip trong Python là gì?


Đị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, ghi, nối và liệt kê một tệp zip. Bất kỳ việc sử dụng nâng cao của mô -đun này sẽ yêu cầu sự hiểu biết về định dạng, như được định nghĩa trong ứng dụng PKZIP Lưu ý.

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 các tiện ích mở rộng zip64 (đó là các tệp zip có kích thước cao 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 một tệp được mã hóa. Giải mã cực kỳ chậm vì nó được thực hiện trong Python bản địa chứ không phải C.

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

Exceptionzipfile.badzipfile¶zipfile.BadZipFile¶zipfile.BadZipFile

Lỗi được nêu ra cho các tệp zip xấu.

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

Exceptionzipfile.badzipfile¶zipfile.BadZipfile¶zipfile.BadZipfile

Bí danh của

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
82, để 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.

Exceptionzipfile.largezipfile¶zipfile.LargeZipFile¶zipfile.LargeZipFile

Lỗi được nêu ra khi tệp zip yêu cầu chức năng zip64 nhưng điều đó chưa được bật.

classzipfile.ZipFilezipfile.ZipFilezipfile.ZipFile

Lớp để đọc và viết các tập tin zip. Xem phần đối tượng Zipfile để biết chi tiết về hàm tạo.ZipFile Objects for constructor details.ZipFile Objects for constructor details.

classzipfile.Pathzipfile.Pathzipfile.Path

Một trình bao bọc tương thích pathlib cho các tệp zip. Xem phần đối tượng đường dẫn để biết chi tiết.Path Objects for details.Path Objects for details.

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

classzipfile.PyZipFilezipfile.PyZipFilezipfile.PyZipFile

Lớp để tạo tài liệu lưu trữ zip có chứa các thư viện Python.

classzipfile.zipinfo (fileName = 'noname', date_time = (1980,1,0,0,0,0)) ¶zipfile.ZipInfo(filename='NoName', date_time=(1980,1,1,0,0,0))¶zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))

Lớp được sử dụng để đại diện cho thông tin về một thành viên của một kho lưu trữ. Các trường hợp của lớp này được trả về bởi các phương thức

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
0 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
1 của các đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2. Hầu hết người dùng của mô -đun
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
3 sẽ không cần phải tạo các mô -đun này, mà chỉ sử dụng những người đượ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 cuối cùng của tệp; Các trường được mô tả trong phần đối tượng Zipinfo.ZipInfo Objects. zipfile.is_zipfile (tên tệp) ¶is_zipfile(filename)¶ZipInfo Objects. zipfile.is_zipfile (tên tệp) ¶is_zipfile(filename)

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 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, sẽ trả về
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
5. Tên tệp có thể là một đối tượng giống như tệp hoặc tệp.

Đã thay đổi trong phiên bản 3.1: Hỗ trợ cho các đối tượng giống như tệp và tệp.Support for file and file-like objects.Support for file and file-like objects.

zipfile.zip_stored¶ZIP_STORED¶ZIP_STORED

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

zipfile.zip_deflated¶ZIP_DEFLATED¶ZIP_DEFLATED

Hằng số 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())
6. zipfile.zip_bzip2¶ZIP_BZIP2¶zipfile.zip_bzip2¶ZIP_BZIP2

Hằng số số cho phương pháp nén BZIP2. Đ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())
7.

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

zipfile.zip_lzma¶ZIP_LZMA¶ZIP_LZMA

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

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.3.

zipfile.zip_lzma¶ZIP_LZMA¶

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

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

zipfile.zip_lzma¶

Ghi chú

Thông số kỹ thuật định dạng tệp ZIP đã bao gồm hỗ trợ nén BZIP2 kể 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 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ý tệp ZIP hoàn toàn hoặc hoặc Không trích xuất các tệp riêng lẻ.

Xem thêm

Ghi chú ứng dụng PKZIP

Trang chủ thông tin-Zip

Tài liệu về định dạng tệp zip của Phil Katz, người tạo định dạng và thuật toán được sử dụng.zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)

Thông tin về các chương trình lưu trữ và thư viện phát triển dự án thông tin-ZIP.zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)¶path-like object.

Đối tượng Zipfilepath-like object.

classzipfile.zipfile (file, mode = '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 đối tượng giống như đường dẫn.

Tham số chế độ phải là

Tham số nén kiểm soát mức nén để sử dụng khi ghi tệp vào kho lưu trữ. Khi sử dụng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13, nó không có tác dụng. Khi sử dụng số nguyên
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
49 đến
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50 được chấp nhận (xem
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
6 để biết thêm thông tin). Khi sử dụng số nguyên
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
53 đến
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
50 được chấp nhận (xem
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
7 để biết thêm thông tin).

Đối số StricT_Timestamp, khi được đặt thành

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
5, cho phép zip các tệp cũ hơn 1980-01-01 với chi phí đặt dấu thời gian thành 1980-01-01. Hành vi tương tự xảy ra với các tệp mới hơn 2107-12-31, dấu thời gian cũng được đặt ở giới hạn.

Khi chế độ là

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9, metadata_encoding có thể được đặt thành tên của một codec, sẽ được sử dụng để giải mã siêu dữ liệu như tên của các thành viên và nhận xét zip.

Nếu tệp được tạo bằng chế độ

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

ZipFile cũng là một người quản lý bối cảnh và do đó hỗ trợ câu lệnh

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
62. Trong ví dụ, Myzip bị đóng sau khi bộ câu lệnh
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
62 đã hoàn thành, ngay cả khi một ngoại lệ xảy ra:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9

Ghi chú

Metadata_encoding là một cài đặt toàn thể cho zipfile. Hiện tại không thể thiết lập điều này trên cơ sở mỗi thành viên.

Thuộc tính này là một cách giải quyết cho các triển khai kế thừa tạo ra tài liệu lưu trữ với tên trong trang mã hóa hoặc mã hiện tại (chủ yếu là trên Windows). Theo tiêu chuẩn .zip, việc mã hóa siêu dữ liệu có thể được chỉ định là trang mã IBM (mặc định) hoặc UTF-8 bằng cờ trong tiêu đề lưu trữ. Cờ đó được ưu tiên hơn metadata_encoding, đây là phần mở rộng cụ thể của Python.

Mới trong phiên bản 3.2: Đã thêm khả năng sử dụng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2 làm trình quản lý ngữ cảnh.Added the ability to use

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

Đã thay đổi trong phiên bản 3.3: Đã thêm hỗ trợ cho nén

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
65 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
8.Added support for

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

Đã thay đổi trong phiên bản 3.4: Tiện ích mở rộng ZIP64 được bật theo mặc định.ZIP64 extensions are enabled by default.ZIP64 extensions are enabled by default.

Đã thay đổi trong phiên bản 3.5: Đã thêm hỗ trợ để viết vào các luồng không thể nhìn thấy. Đã thêm hỗ trợ cho chế độ

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12.Added support for writing to unseekable streams. Added support for the

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

Đã thay đổi trong phiên bản 3.6: Trước đây, một

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đơn giản đã được nâng lên cho các giá trị nén không được công nhận.Previously, a plain

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised for unrecognized compression values.

Đã thay đổi trong phiên bản 3.6.2: Tham số tệp chấp nhận một đối tượng giống như đường dẫn.The file parameter accepts a path-like object.The file parameter accepts a path-like object.

Đã thay đổi trong phiên bản 3.7: Thêm tham số nén.Add the compresslevel parameter.Add the compresslevel parameter.

Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_TimestampsThe strict_timestamps keyword-only argumentThe strict_timestamps keyword-only argument

Đã thay đổi trong phiên bản 3.11: Đã thêm hỗ trợ để chỉ định mã hóa tên thành viên để đọc siêu dữ liệu trong thư mục và tiêu đề tệp Zipfile.Added support for specifying member name encoding for reading metadata in the zipfile’s directory and file headers.Added support for specifying member name encoding for reading metadata in the zipfile’s directory and file headers.

Zipfile.close ()close()¶close()

Đóng tệp lưu trữ. Bạn phải gọi

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
69 trước khi thoát khỏi chương trình hoặc hồ sơ cần thiết của bạn sẽ không được viết. Zipfile.getInfo (tên) ¶getinfo(name)¶Zipfile.getInfo (tên) ¶getinfo(name)

Trả về một đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 với thông tin về tên thành viên lưu trữ. Gọi
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
0 cho một tên hiện không có trong kho lưu trữ sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12. Zipfile.infast () ¶infolist()¶Zipfile.infast () ¶infolist()

Trả về một danh sách chứa một đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 cho mỗi thành viên của kho lưu trữ. Các đối tượng theo thứ tự giống như các mục của chúng trong tệp zip thực tế trên đĩa nếu một kho lưu trữ hiện có được mở. Zipfile.namelist ()namelist()¶Zipfile.namelist ()namelist()

Trả lại một danh sách các thành viên lưu trữ theo tên.

Zipfile.open (name, mode = 'r', pwd = none, *, force_zip64 = false)open(name, mode='r', pwd=None, *, force_zip64=False)¶open(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 đố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 đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. Tham số chế độ, nếu được bao gồm, phải là
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9 (mặc định) hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. NKT là mật khẩu được sử dụng để giải mã các tệp zip được mã hóa.
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 cũng là người quản lý bối cảnh và do đó hỗ trợ câu lệnh
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
62:
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())
9 đối tượng giống như tệp (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
20) chỉ đọc và cung cấp các phương pháp sau:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
21,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
22,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
23,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
24,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
25,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
26,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
27. 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())
28, một tay cầm tệp có thể ghi được trả về, hỗ trợ phương thức
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
29. Mặc dù xử lý tệp có thể ghi được mở, việc cố gắng đọc hoặc ghi các tệp khác trong tệp zip sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820.

Khi viết một 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())
821 để đảm bảo đị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 xây dựng một đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 với
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
823 và sử dụng nó làm tham số tên.

Ghi chú

Metadata_encoding là một cài đặt toàn thể cho zipfile. Hiện tại không thể thiết lập điều này trên cơ sở mỗi thành viên.

Thuộc tính này là một cách giải quyết cho các triển khai kế thừa tạo ra tài liệu lưu trữ với tên trong trang mã hóa hoặc mã hiện tại (chủ yếu là trên Windows). Theo tiêu chuẩn .zip, việc mã hóa siêu dữ liệu có thể được chỉ định là trang mã IBM (mặc định) hoặc UTF-8 bằng cờ trong tiêu đề lưu trữ. Cờ đó được ưu tiên hơn metadata_encoding, đây là phần mở rộng cụ thể của Python.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
828 can now be used to write files into the archive with the
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
828 can now be used to write files into the archive with the

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

Đã 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())
17 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đã được nâng lên.Calling

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 on a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised.

Trích xuất một thành viên từ kho lưu trữ đến thư mục làm việc hiện tại; Thành viên phải là tên đầy đủ của nó hoặc đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. 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 nhau để trích xuất. Thành viên có thể là một tên tệp hoặc đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.

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

Ghi chú

Metadata_encoding là một cài đặt toàn thể cho zipfile. Hiện tại không thể thiết lập điều này trên cơ sở mỗi thành viên.

Thuộc tính này là một cách giải quyết cho các triển khai kế thừa tạo ra tài liệu lưu trữ với tên trong trang mã hóa hoặc mã hiện tại (chủ yếu là trên Windows). Theo tiêu chuẩn .zip, việc mã hóa siêu dữ liệu có thể được chỉ định là trang mã IBM (mặc định) hoặc UTF-8 bằng cờ trong tiêu đề lưu trữ. Cờ đó được ưu tiên hơn metadata_encoding, đây là phần mở rộng cụ thể của Python.
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
828 can now be used to write files into the archive with the

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

Đã 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())
17 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đã được nâng lên.Calling

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 on a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised.The path parameter accepts a path-like object.

Trích xuất một thành viên từ kho lưu trữ đến thư mục làm việc hiện tại; Thành viên phải là tên đầy đủ của nó hoặc đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. 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 nhau để trích xuất. Thành viên có thể là một tên tệp hoặc đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.

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

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 on a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised.The path parameter accepts a path-like object.

Trích xuất một thành viên từ kho lưu trữ đến thư mục làm việc hiện tại; Thành viên phải là tên đầy đủ của nó hoặc đối tượng printdir()

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

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ: setpassword(pwd)

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Đã 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())
826 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một
read(name, pwd=None)

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đã được nâng lên.Calling
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
826 on a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a

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

Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.The path parameter accepts a path-like object.Zipfile.read (tên, pwd = none) ¶testzip()

Trích xuất tất cả các thành viên từ kho lưu trữ đến thư mục làm việc hiện tại. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Các 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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
23. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.

Đã 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())
21 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đã được nâng lên.Calling

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
40 on a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised. Zipfile.testzip ()write(filename, arcname=None, compress_type=None, compresslevel=None)¶Zipfile.testzip ()write(filename, arcname=None, compress_type=None, compresslevel=None)

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

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

Ghi chú

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Ghi chú

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Ghi chú

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Ghi chú

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Đã 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())
826 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 đã được nâng lên.Calling

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
29 on a ZipFile created with mode
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9 or a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised. Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)¶Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)

Trích xuất tất cả các thành viên từ kho lưu trữ đến thư mục làm việc hiện tại. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Các 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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
23. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10). Tương tự, nén sẽ ghi đè hàm tạo nếu được đưa ra.

Ghi chú

Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn (trở lại) sẽ bị tước, ví dụ:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
05 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
06 trên Unix và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
07 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
08 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())
09 trong tên tệp thành viên sẽ bị xóa, ví dụ:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 trở thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11. Trên các ký tự bất hợp pháp của Windows (
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
13,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
14,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
15,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
16,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
17 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
18) đượ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())
19).

Đã 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())
826 trên Zipfile đóng sẽ tăng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Trước đây, một The compress_type argument.

Đã 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())
41 đã được nâng lên.Calling

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
29 on a ZipFile created with mode
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
9 or a closed ZipFile will raise a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
820. Previously, a
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
41 was raised. Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)¶Zipfile.mkdir (zinfo_or_directory, mode = 511) ¶mkdir(zinfo_or_directory, mode=511)

Trích xuất tất cả các thành viên từ kho lưu trữ đến thư mục làm việc hiện tại. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Các 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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
23. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11.

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

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

Zipfile.filename¶filename¶filename

Tên của tệp zip.

Zipfile.debug¶debug¶ debug

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
49 (mặc định, không có đầu ra) thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
74 (đầ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())
27.

Nhận xét liên quan đến tệp zip dưới dạng đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
54. Nếu gán một nhận xét cho một thể hiện
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2 được tạo với chế độ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
12 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
11, nó sẽ không dài hơn 65535 byte. Nhận xét lâu hơn điều này sẽ bị cắt ngắn.

Đối tượng đường dẫn Sở đối tượng

classzipfile.path (root, at = '') ¶zipfile.Path(root, at='')¶zipfile.Path(root, at='')

Xây dựng một đối tượng đường dẫn từ một zipfile

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
81 (có thể là một ví dụ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
83 phù hợp để chuyển đến hàm tạo
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2).
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
85 Chỉ định vị trí của đường dẫn này trong zipfile, ví dụ: ‘DIR/FILE.TXT,‘ Dir/xông, hoặc ‘. Mặc định cho chuỗi trống, chỉ ra gốc.

Các đối tượng đường dẫn hiển thị các tính năng sau của các đối tượng

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

Các đối tượng đường dẫn có thể đi qua bằng toán tử

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
87 hoặc
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
88. Path.Name¶name¶Path.Name¶name

Thành phần đường dẫn cuối cùng.

Path.open (mode = 'r', *, pwd, **) ¶open(mode='r', *, pwd, **)¶open(mode='r', *, pwd, **)

Gọi

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
828 trên con đường hiện tại. Cho phép mở để đọc hoặc viết, văn bản hoặc nhị phân thông qua các chế độ được hỗ trợ: ‘R,‘ W, ‘RB,‘ WB. Đối số vị trí và từ khóa được chuyển qua

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
90 khi được mở dưới dạng văn bản và bị bỏ qua.
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
91 là tham số
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
91 thành
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
828.

Đã thay đổi trong phiên bản 3.9: Đã thêm hỗ trợ cho các chế độ văn bản và nhị phân để mở. Chế độ mặc định bây giờ là văn bản.Added support for text and binary modes for open. Default mode is now text.Added support for text and binary modes for open. Default mode is now text.

Path.iterdir () ¶iterdir()¶iterdir()

Liệt kê những đứa trẻ của thư mục hiện tại.

Path.is_dir ()is_dir()¶is_dir()

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham khảo một thư mục. Path.is_file () ¶is_file()¶Path.is_file () ¶is_file()

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham khảo một thư mục. Path.is_file () ¶is_file()¶Path.exists () ¶exists()

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham khảo một thư mục. Path.is_file () ¶is_file()¶Path.Suffix¶suffix

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham khảo một tệp. Path.exists () ¶exists()¶

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham chiếu một tệp hoặc thư mục trong tệp zip. Path.Suffix¶suffix¶

Phần mở rộng tập tin của thành phần cuối cùng.

Mới trong phiên bản 3.11: Đã thêm thuộc tính Path.stem¶stem

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 Nếu bối cảnh hiện tại tham chiếu một tệp hoặc thư mục trong tệp zip. Path.Suffix¶suffix¶

Phần mở rộng tập tin của thành phần cuối cùng.

Mới trong phiên bản 3.11: Đã thêm thuộc tính Path.Suffixes¶suffixes

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
97 property. Path.stem¶stem¶
read_text(*, **)

Thành phần đường dẫn cuối cùng, không có hậu tố của nó.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
98.Added Path.read_bytes () ¶read_bytes()

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
98 property. Path.Suffixes¶suffixes¶

Một danh sách các phần mở rộng tệp đường dẫn.joinpath(*other)

Path.read_text (*, **) ¶read_text(*, **)¶

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

Đọc tệp hiện tại dưới dạng văn bản Unicode. Các đối số vị trí và từ khóa được truyền qua

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
90 (ngoại trừ
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
100, được ngụ ý bởi bối cảnh). Path.read_bytes () ¶read_bytes()¶

Đọc tệp hiện tại dưới dạng byte.

Path.joinpath (*khác) ¶joinpath(*other)¶

Trả về một đối tượng đường dẫn mới với mỗi đối số khác đã tham gia. Sau đây là tương đương:

Đã thay đổi trong phiên bản 3.10: Trước 3.10,

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
88 không được giấy tờ và được chấp nhận chính xác một tham số.Prior to 3.10,

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
88 was undocumented and accepted exactly one parameter.classzipfile.pyzipfile (file, mode = 'r', nén = zip_stored, allowzip64 = true, tối ưu hóa = -1) ¶zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=- 1)

Dự án ZIPP cung cấp backports của chức năng đối tượng đường dẫn mới nhất cho Pythons cũ hơn. Sử dụng The optimize parameter.

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
102 thay cho
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
103 để truy cập sớm vào các thay đổi.ZIP64 extensions are enabled by default.

Đối tượng pyzipfile

Chất xây dựng writepy (pathName, basename = '', filterFunc = none) ¶(pathname, basename='', filterfunc=None)

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
104 có các tham số giống như hàm tạo
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2 và một tham số bổ sung, tối ưu hóa. classzipfile.pyzipfile (file, mode = 'r', nén = zip_stored, allowzip64 = true, tối ưu hóa = -1) ¶zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=-1)¶

Mới trong phiên bản 3.2: Tham số tối ưu hóa.The optimize parameter.

Đã thay đổi trong phiên bản 3.4: Tiện ích mở rộng ZIP64 được bật theo mặc định.ZIP64 extensions are enabled by default.

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

Đã thay đổi trong phiên bản 3.4: Tiện ích mở rộng ZIP64 được bật theo mặc định.ZIP64 extensions are enabled by default.

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2: writepy (pathName, basename = '', filterFunc = none) ¶(pathname, basename='', filterfunc=None)¶

Tìm kiếm các tệp

Basename chỉ dành cho sử dụng nội bộ.

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

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
122 hoặc bắt đầu với chuỗi
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
123, chúng tôi có thể sử dụng FilterFunc để loại trừ chúng:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
1

Phương thức

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
124 tạo ra tài liệu lưu trữ với tên tệp như thế này:
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4

Mới trong phiên bản 3.4: Tham số FilterFunc.The filterfunc parameter.The filterfunc parameter.

Đã thay đổi trong phiên bản 3.6.2: Tham số PathName chấp nhận một đối tượng giống như đường dẫn.The pathname parameter accepts a path-like object.The pathname parameter accepts a path-like object.

Đã thay đổi trong phiên bản 3.7: Các mục nhập thư mục sắp xếp đệ quy.Recursion sorts directory entries.Recursion sorts directory entries.

Đối tượng Zipinfo

Các phiên bản của lớp

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 được trả về bởi các phương thức ____10 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
1 của các đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
2. Mỗi đối tượng lưu trữ thông tin về một thành viên duy nhất của kho lưu trữ zip.

Có một ClassMethod để tạo phiên bản

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 cho tệp hệ thống tập tin: classMethodzipInfo.from_file (fileName, arcName = none, *, strict_timestamp = true) ¶ZipInfo.from_file(filename, arcname=None, *, strict_timestamps=True)¶classMethodzipInfo.from_file (fileName, arcName = none, *, strict_timestamp = true) ¶ZipInfo.from_file(filename, arcname=None, *, strict_timestamps=True)

Xây dựng một thể hiện

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
10 cho một tệp trên hệ thống tập tin, để chuẩn bị thêm nó vào tệp zip.

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

Nếu ArcName được chỉ định, nó được sử dụng làm tên trong kho lưu trữ. Nếu ArcName 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 nào và các dấu phân cách đường dẫn hàng đầu bị xóa.

Đối số StricT_Timestamp, khi được đặt thành

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
5, cho phép zip các tệp cũ hơn 1980-01-01 với chi phí đặt dấu thời gian thành 1980-01-01. Hành vi tương tự xảy ra với các tệp mới hơn 2107-12-31, dấu thời gian cũng được đặt ở giới hạn.

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 đối tượng giống như đường dẫn.The filename parameter accepts a path-like object.The filename parameter accepts a path-like object.

Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_TimestampsThe strict_timestamps keyword-only argumentThe strict_timestamps keyword-only argument

Các phiên bản có các phương thức và thuộc tính sau:

Zipinfo.is_dir ()is_dir()¶is_dir()

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 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 mục nhập tên: Các thư mục phải luôn kết thúc bằng

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

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 đối tượng giống như đường dẫn.The filename parameter accepts a path-like object.filename

Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_TimestampsThe strict_timestamps keyword-only argument

Các phiên bản có các phương thức và thuộc tính sau:date_time

Zipinfo.is_dir ()is_dir()¶

Trả về

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
4 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 mục nhập tên: Các thư mục phải luôn kết thúc bằng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
87.
Đã thay đổi trong phiên bản 3.6.2: Tham số tên tệp chấp nhận một đối tượng giống như đường dẫn.filename¶

Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_Timestamps

Các phiên bản có các phương thức và thuộc tính sau:date_time¶

Zipinfo.is_dir ()

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

Zipinfo.filename¶

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

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

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

Zipinfo.date_time¶

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

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

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

Mục lục

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

Giá trị

Năm (> = 1980)

Tháng (dựa trên một)

Ngày trong tháng (dựa trên một)compress_type¶create_system

Giờ (không dựa trên)

Biên bản (không dựa trên)create_version

Giây (dựa trên không)

Ghi chúcreate_system¶

Định dạng tệp ZIP không hỗ trợ dấu thời gian trước năm 1980.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 có trong đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
54 này.
reserved

Zipinfo.compress_type¶create_version¶

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

Nhận xét cho thành viên lưu trữ riêng lẻ là đối tượng

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
54. 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 có trong đối tượng
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
54 này.reserved¶
volume

Zipinfo.create_system¶

Hệ thống tạo ra kho lưu trữ zip.flag_bits¶ internal_attr

Zipinfo.create_version¶

Phiên bản PKZIP tạo ra kho lưu trữ zip.volume¶external_attr

Phiên bản PKZIP cần thiết để trích xuất Lưu trữ.

Zipinfo.reserved¶internal_attr¶

Phải bằng không.CRC

Zipinfo.flag_bits¶external_attr¶

BIT cờ zip.compress_size

Zipinfo.volume¶

Số lượng tiêu đề tập tin.CRC¶file_size

Zipinfo.Inalal_attr¶

Thuộc tính nội bộ.compress_size¶

Zipinfo.external_attr¶

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

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

Byte bù vào tiêu đề tập tin.

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

Zipinfo.crc¶

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

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

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

Zipinfo.compress_size¶

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

Zipinfo.file_size¶

Kích thước của tập tin không nén.Vượt qua một thư mục cũng được chấp nhận: ... --create ...

Nếu bạn muốn trích xuất kho lưu trữ zip vào thư mục được chỉ định, hãy sử dụng tùy chọn

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

Trích xuất zipfile vào thư mục đích.

-T¶ -Test¶¶ --test¶ --test

Kiểm tra xem zipfile có hợp lệ hay không.

Chỉ định mã hóa tên thành viên cho

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
145,
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
144 và
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())
148.

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

Cạm bẫy giải nén

Việc trích xuất trong mô -đun Zipfile có thể thất bại do một số cạm bẫy được liệt kê dưới đây.

Từ tập tin chính nó

Giải quyết có thể thất bại do mật khẩu / định dạng kiểm tra CRC / ZIP không chính xác hoặc phương pháp nén / giải mã không được hỗ trợ.

Giới hạn hệ thống tệp

Vượt quá giới hạn trên các hệ thống tệp khác nhau có thể gây ra giải nén không thành công. Chẳng hạn như các ký tự được phép trong các mục nhập thư mục, độ dài của tên tệp, độ dài của tên đường dẫn, kích thước của một tệp và số lượng tệp, v.v.

Giới hạn tài nguyên

Việc thiếu bộ nhớ hoặc khối lượng đĩa sẽ dẫn đến giải nén không thành công. Ví dụ, bom giải nén (còn gọi là bom zip) áp dụng cho thư viện zipfile có thể gây cạn kiệt khối lượng đĩa.

Sự gián đoạn

Sự gián đoạn trong quá trình giải nén, chẳng hạn như cấp kiểm soát-C hoặc tiêu diệt quá trình giải nén có thể dẫn đến giải nén không hoàn toàn của kho lưu trữ.

Python đã bảo vệ bạn khi tạo các tệp zip có thể nhập. Mô -đun zipfile từ thư viện tiêu chuẩn bao gồm một lớp gọi là zipfile để thao tác các tệp zip. Nó cũng bao gồm một lớp chuyên dụng hơn gọi là pyzipfile, tạo điều kiện cho việc tạo các tệp zip có thể nhập.The zipfile module from the standard library includes a class called ZipFile for manipulating ZIP files. It also includes a more specialized class called PyZipFile , which facilitates the creation of importable ZIP files.The zipfile module from the standard library includes a class called ZipFile for manipulating ZIP files. It also includes a more specialized class called PyZipFile , which facilitates the creation of importable ZIP files.

Tạo một kho lưu trữ zip từ nhiều tệp trong Python Tạo một đối tượng zipfile bằng cách chuyển tên tệp và chế độ mới dưới dạng 'w' (chế độ ghi).Nó sẽ tạo một tệp zip mới và mở nó trong đối tượng zipfile.Chức năng ghi () trên đối tượng ZipFile để thêm các tệp trong đó.Gọi Đóng () trên đối tượng ZipFile để đóng tệp zip.Create a ZipFile object by passing the new file name and mode as 'w' (write mode). It will create a new zip file and open it within ZipFile object. Call write() function on ZipFile object to add the files in it. call close() on ZipFile object to Close the zip file.Create a ZipFile object by passing the new file name and mode as 'w' (write mode). It will create a new zip file and open it within ZipFile object. Call write() function on ZipFile object to add the files in it. call close() on ZipFile object to Close the zip file.

Đị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, ghi, nối và liệt kê một tệp zip.Bất kỳ việc sử dụng nâng cao của mô -đun này sẽ yêu cầu sự hiểu biết về định dạng, như được định nghĩa trong ứng dụng PKZIP Lưu ý.a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.