Python lưu tệp zip đã tải xuống

Bài viết này giải thích cách một người có thể thực hiện các thao tác khác nhau trên tệp zip bằng chương trình python đơn giản

Tệp zip là gì?

ZIP là định dạng tệp lưu trữ hỗ trợ nén dữ liệu không mất dữ liệu. Bằng cách nén không mất dữ liệu, chúng tôi muốn nói rằng thuật toán nén cho phép dữ liệu gốc được tái tạo hoàn hảo từ dữ liệu đã nén. Vì vậy, tệp ZIP là một tệp duy nhất chứa một hoặc nhiều tệp nén, cung cấp một cách lý tưởng để làm cho các tệp lớn nhỏ hơn và giữ các tệp liên quan lại với nhau

Tại sao chúng ta cần tệp zip?

  • Để giảm yêu cầu lưu trữ
  • Để cải thiện tốc độ truyền qua các kết nối tiêu chuẩn

Để làm việc trên các tệp zip bằng python, chúng tôi sẽ sử dụng mô-đun python có sẵn có tên zipfile

1. Giải nén một tập tin zip




with ZipFile(file_name, 'r') as zip:
1

with ZipFile(file_name, 'r') as zip:
2
with ZipFile(file_name, 'r') as zip:
3
with ZipFile(file_name, 'r') as zip:
4
with ZipFile(file_name, 'r') as zip:
5

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
7

with ZipFile(file_name, 'r') as zip:
8
with ZipFile(file_name, 'r') as zip:
9
zip.printdir()
0

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
2

zip.printdir()
3
zip.printdir()
4____95
zip.printdir()
6
zip.printdir()
7

zip.printdir()
8
zip.printdir()
9

zip.printdir()
8
zip.printdir()
6____212

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
15

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
17____218
with ZipFile(file_name, 'r') as zip:
19
with ZipFile(file_name, 'r') as zip:
90

_______98____96____293

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
17____218
with ZipFile(file_name, 'r') as zip:
97
with ZipFile(file_name, 'r') as zip:
90

Chương trình trên trích xuất một tệp zip có tên “my_python_files. zip” trong cùng thư mục với tập lệnh python này.
Đầu ra của chương trình trên có thể như thế này.

Python lưu tệp zip đã tải xuống

Hãy để chúng tôi cố gắng hiểu đoạn mã trên

  • with ZipFile(file_name, 'r') as zip:
    8

    ZipFile là một lớp mô-đun zipfile để đọc và ghi các tệp zip. Ở đây chúng tôi chỉ nhập lớp ZipFile từ mô-đun zipfile

  • with ZipFile(file_name, 'r') as zip:

    Ở đây, một đối tượng ZipFile được tạo bằng cách gọi hàm tạo ZipFile chấp nhận tên tệp zip và tham số chế độ. Chúng tôi tạo một đối tượng ZipFile ở chế độ ĐỌC và đặt tên là zip

  • zip.printdir()

    Phương thức printdir() in mục lục cho kho lưu trữ

  • with ZipFile(file_name, 'r') as zip:
    1

    extractall() sẽ trích xuất tất cả nội dung của tệp zip vào thư mục làm việc hiện tại. Bạn cũng có thể gọi phương thức extract() để giải nén bất kỳ tệp nào bằng cách chỉ định đường dẫn của nó trong tệp zip.
    Ví dụ.

    with ZipFile(file_name, 'r') as zip:
    9

    Điều này sẽ chỉ trích xuất tệp được chỉ định

    Nếu bạn muốn đọc một số tệp cụ thể, bạn có thể đi như thế này

    with ZipFile(file_name, 'r') as zip:
    3

2. Ghi vào tệp zip

Hãy xem xét một thư mục (thư mục) với định dạng như vậy

Python lưu tệp zip đã tải xuống

Ở đây, chúng ta sẽ cần thu thập dữ liệu toàn bộ thư mục và các thư mục con của nó để lấy danh sách tất cả các đường dẫn tệp trước khi ghi chúng vào tệp zip.
Chương trình sau thực hiện việc này bằng cách thu thập dữ liệu thư mục sẽ được nén.




with ZipFile(file_name, 'r') as zip:
1

with ZipFile(file_name, 'r') as zip:
2
with ZipFile(file_name, 'r') as zip:
3
with ZipFile(file_name, 'r') as zip:
4
with ZipFile(file_name, 'r') as zip:
5

with ZipFile(file_name, 'r') as zip:
4
with ZipFile(file_name, 'r') as zip:
35

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
37
with ZipFile(file_name, 'r') as zip:
38

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8____561

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
63____09
with ZipFile(file_name, 'r') as zip:
65

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
68

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
50
with ZipFile(file_name, 'r') as zip:
51
with ZipFile(file_name, 'r') as zip:
52
with ZipFile(file_name, 'r') as zip:
53

with ZipFile(file_name, 'r') as zip:
54
with ZipFile(file_name, 'r') as zip:
50
with ZipFile(file_name, 'r') as zip:
56______652
with ZipFile(file_name, 'r') as zip:
58

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
50

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
52____09
with ZipFile(file_name, 'r') as zip:
54

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
56

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
59

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
61
with ZipFile(file_name, 'r') as zip:
62

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
37
with ZipFile(file_name, 'r') as zip:
65

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
67

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
69
with ZipFile(file_name, 'r') as zip:
9
with ZipFile(file_name, 'r') as zip:
01

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
04

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
63____09
with ZipFile(file_name, 'r') as zip:
08

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
11

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
17____218
with ZipFile(file_name, 'r') as zip:
15
with ZipFile(file_name, 'r') as zip:
90

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
50
with ZipFile(file_name, 'r') as zip:
8
with ZipFile(file_name, 'r') as zip:
52
with ZipFile(file_name, 'r') as zip:
21

with ZipFile(file_name, 'r') as zip:
54
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
24

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
27

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
29
with ZipFile(file_name, 'r') as zip:
30
with ZipFile(file_name, 'r') as zip:
31
with ZipFile(file_name, 'r') as zip:
32
zip.printdir()
5
zip.printdir()
6
zip.printdir()
7

with ZipFile(file_name, 'r') as zip:
54
with ZipFile(file_name, 'r') as zip:
37

with ZipFile(file_name, 'r') as zip:
54
with ZipFile(file_name, 'r') as zip:
50
with ZipFile(file_name, 'r') as zip:
40
with ZipFile(file_name, 'r') as zip:
52
with ZipFile(file_name, 'r') as zip:
21

with ZipFile(file_name, 'r') as zip:
59
zip.printdir()
6____045
with ZipFile(file_name, 'r') as zip:
40
with ZipFile(file_name, 'r') as zip:
90

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
17____218
with ZipFile(file_name, 'r') as zip:
52
with ZipFile(file_name, 'r') as zip:
53

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
56
with ZipFile(file_name, 'r') as zip:
57
with ZipFile(file_name, 'r') as zip:
9
with ZipFile(file_name, 'r') as zip:
9
with ZipFile(file_name, 'r') as zip:
60
zip.printdir()
7

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
63

Đầu ra của chương trình trên trông như thế này

Python lưu tệp zip đã tải xuống

Hãy để chúng tôi cố gắng hiểu đoạn mã trên bằng cách chia thành các đoạn

  • with ZipFile(file_name, 'r') as zip:
    6

    Trước hết, để lấy tất cả các đường dẫn tệp trong thư mục của chúng tôi, chúng tôi đã tạo chức năng này sử dụng lệnh os. walk() phương pháp. Trong mỗi lần lặp lại, tất cả các tệp có trong thư mục đó được thêm vào danh sách có tên là file_paths.
    Cuối cùng, chúng tôi trả lại tất cả các đường dẫn tệp.

  • with ZipFile(file_name, 'r') as zip:
    5

    Ở đây, chúng tôi chuyển thư mục được nén vào hàm get_all_file_paths() và lấy danh sách chứa tất cả các đường dẫn tệp

  • with ZipFile(file_name, 'r') as zip:
    5

    Ở đây, lần này chúng ta tạo một đối tượng ZipFile ở chế độ WRITE

  • with ZipFile(file_name, 'r') as zip:
    6

    Ở đây, chúng tôi ghi lần lượt tất cả các tệp vào tệp zip bằng phương pháp ghi

3. Nhận tất cả thông tin về một tệp zip




with ZipFile(file_name, 'r') as zip:
1

with ZipFile(file_name, 'r') as zip:
2
with ZipFile(file_name, 'r') as zip:
3
with ZipFile(file_name, 'r') as zip:
4
with ZipFile(file_name, 'r') as zip:
5

with ZipFile(file_name, 'r') as zip:
4
with ZipFile(file_name, 'r') as zip:
70

with ZipFile(file_name, 'r') as zip:
6

with ZipFile(file_name, 'r') as zip:
7

with ZipFile(file_name, 'r') as zip:
8
with ZipFile(file_name, 'r') as zip:
9
with ZipFile(file_name, 'r') as zip:
75

with ZipFile(file_name, 'r') as zip:
6

zip.printdir()
2

zip.printdir()
3
zip.printdir()
4____95
zip.printdir()
6
zip.printdir()
7

zip.printdir()
8
with ZipFile(file_name, 'r') as zip:
50
with ZipFile(file_name, 'r') as zip:
85
with ZipFile(file_name, 'r') as zip:
52
zip.printdir()
6
with ZipFile(file_name, 'r') as zip:
88

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
91

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
18
with ZipFile(file_name, 'r') as zip:
95
with ZipFile(file_name, 'r') as zip:
96
with ZipFile(file_name, 'r') as zip:
97
with ZipFile(file_name, 'r') as zip:
98
with ZipFile(file_name, 'r') as zip:
99
zip.printdir()
00

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
18
zip.printdir()
04
with ZipFile(file_name, 'r') as zip:
96
with ZipFile(file_name, 'r') as zip:
97
zip.printdir()
07
with ZipFile(file_name, 'r') as zip:
96
zip.printdir()
09
with ZipFile(file_name, 'r') as zip:
90

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
18
zip.printdir()
14
with ZipFile(file_name, 'r') as zip:
96
with ZipFile(file_name, 'r') as zip:
97
zip.printdir()
17

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
18
zip.printdir()
21
with ZipFile(file_name, 'r') as zip:
96
with ZipFile(file_name, 'r') as zip:
97
zip.printdir()
24
with ZipFile(file_name, 'r') as zip:
96
zip.printdir()
26
with ZipFile(file_name, 'r') as zip:
90

with ZipFile(file_name, 'r') as zip:
59
with ZipFile(file_name, 'r') as zip:
17
with ZipFile(file_name, 'r') as zip:
18
zip.printdir()
31
with ZipFile(file_name, 'r') as zip:
96
with ZipFile(file_name, 'r') as zip:
97
zip.printdir()
34
with ZipFile(file_name, 'r') as zip:
96
zip.printdir()
26
with ZipFile(file_name, 'r') as zip:
90

Đầu ra của chương trình trên có thể trông như thế này

Python lưu tệp zip đã tải xuống

with ZipFile(file_name, 'r') as zip:
0

Ở đây, phương thức infolist() tạo một thể hiện của lớp ZipInfo chứa tất cả thông tin về tệp zip.
Chúng tôi có thể truy cập tất cả thông tin như ngày sửa đổi cuối cùng của tệp, tên tệp, hệ thống tạo tệp, phiên bản Zip, kích thước của tệp ở dạng nén và không nén, v.v.

Bài viết này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác

Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên

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

Python3. # vào một địa điểm cụ thể. Nhập mô-đun zipfile Tạo đối tượng tệp zip bằng lớp ZipFile. Gọi phương thức extract() trên đối tượng tệp zip và chuyển tên của tệp cần giải nén và đường dẫn nơi tệp cần được giải nén và Giải nén tệp cụ thể có trong 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