Hướng dẫn zipfile in python - zipfile trong python

Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt.

Tạo File Zip sử dụng Python

Đầu tiên là import thư viện filezip

import zipfile

Lựa chọn mode để nén file zip

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

Tạo file Zip với đường dẫn và mode (w – write, a – append)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

Thêm file vào file zip vừa tạo

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression

Close file

# Don't forget to close the file!
zf.close()

Vậy đoạn code đầy đủ sẽ là:

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()

Kết quả

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip

Hướng dẫn zipfile in python - zipfile trong python

Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây.

Nguồn vinasupport.com

Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt.

Nội dung chính

  • Tạo File Zip sử dụng Python
  • Tạo File Zip sử dụng Python
  • Nén các Tập tin Riêng lẻ
  • Nén nhiều Tập tin
  • Giải nén tất cả các Tập tin
  • Giải nén từng Tập tin
  • Đọc các Tập tin Zip
  • Tổng kết

Tạo File Zip sử dụng Python

Nén các Tập tin Riêng lẻ

import zipfile

Nén nhiều Tập tin

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

Giải nén tất cả các Tập tin

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

Giải nén từng Tập tin

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression

Đọc các Tập tin Zip

# Don't forget to close the file!
zf.close()

Đầu tiên là import thư viện filezip

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()

Lựa chọn mode để nén file zip

Tạo file Zip với đường dẫn và mode (w – write, a – append)

Thêm file vào file zip vừa tạo

Close file

Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt.

Tạo File Zip sử dụng Python

Nén các Tập tin Riêng lẻ

import zipfile

Nén nhiều Tập tin

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

Giải nén tất cả các Tập tin

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

Giải nén từng Tập tin

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression

Đọc các Tập tin Zip

# Don't forget to close the file!
zf.close()

Đầu tiên là import thư viện filezip

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()

Lựa chọn mode để nén file zip

Tạo file Zip với đường dẫn và mode (w – write, a – append)

Thêm file vào file zip vừa tạo

Close file

Vậy đoạn code đầy đủ sẽ là:Dai Phong (you can also view the original English article)

Kết quả

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip

Nén các Tập tin Riêng lẻ

Nén nhiều Tập tin

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
8

Giải nén tất cả các Tập tin

Bạn có thể chỉ định các phương thức nén khác nhau để nén các tập tin. Các phương thức mới hơn

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
5 và
# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
6 được bổ sung trong Python phiên bản 3.3, và còn có một số công cụ khác mà không hỗ trợ hai phương thức nén này. Vì lý do này, yên tâm khi chỉ cần sử dụng phương thức
# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
7. Bạn cũng nên thử những phương thức này để thấy sự khác biệt trong kích thước của các tập tin nén.

Nén nhiều Tập tin

Điều này hơi phức tạp một chút vì bạn cần lặp qua tất cả các tập tin. Code dưới đây sẽ nén tất cả các tập tin với phần mở rộng tập tin pdf trong một thư mục nhất định:

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
9

Lần này, chúng ta đã import mô-đun

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
8 và sử dụng phương thức
# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
9 của nó để duyệt qua tất cả các tập tin và thư mục con trong thư mục gốc của chúng ta. Tôi chỉ nén các tập tin pdf trong thư mục. Bạn cũng có thể tạo các tập tin lưu trữ khác cho mỗi định dạng bằng cách sử dụng câu lệnh
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
0.

Nếu bạn không muốn giữ lại cấu trúc thư mục, bạn có thể gom tất cả các tập tin với nhau bằng cách sử dụng dòng code sau:

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
0

Phương thức

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
1 chấp nhận ba tham số. Tham số đầu tiên là tên tập tin mà chúng ta muốn nén. Tham số thứ hai là không bắt buộc và cho phép bạn chỉ định một tên tập tin khác cho tập tin được nén. Nếu không được chỉ định, thì tên ban đầu được sử dụng.

Giải nén tất cả các Tập tin

Bạn có thể sử dụng phương thức

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
2 để giải nén tất cả các tập tin và thư mục từ một tập tin nén vào thư mục hiện tại. Bạn cũng có thể truyền một tên thư mục vào
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
2 để giải nén các tập tin và thư mục trong một thư mục cụ thể. Nếu thư mục mà bạn truyền vào không tồn tại, phương thức này sẽ tạo nó cho bạn. Dưới đây là đoạn code bạn có thể sử dụng để giải nén các tập tin:

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
1

Nếu bạn muốn giải nén nhiều tập tin, bạn sẽ phải cung cấp tên của các tập tin mà bạn muốn giải nén dưới dạng một danh sách.

Giải nén từng Tập tin

Điều này cũng tương tự như giải nén nhiều tập tin. Một sự khác biệt là lần này, bạn cần phải cung cấp tên tập tin đầu tiên và đường dẫn để giải nén chúng vào đó. Ngoài ra, bạn cần phải sử dụng phương thức

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
4 thay vì
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
2. Dưới đây là một đoạn code cơ bản để giải nén từng tập tin.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
2

Đọc các Tập tin Zip

Hãy xem xét một trường hợp mà bạn cần xem một tập tin lưu trữ zip có chứa một tập tin cụ thể nào đó hay không. Đến thời điểm này, lựa chọn duy nhất của bạn để làm điều đó là bằng cách giải nén tất cả các tập tin trong kho lưu trữ. Tương tự, bạn có thể cần phải giải nén chỉ những tập tin đó mà lớn hơn một kích thước cụ thể. Mô-đun

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
6 cho phép chúng ta kiểm tra nội dung của một tập tin nén mà không cần giải nén nó.

Sử dụng phương thức

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
7 của đối tượng ZipFile sẽ trả về một danh sách tất cả các thành phần của một tập tin nén theo tên. Để có được thông tin về một tập tin cụ thể trong tập tin nén, bạn có thể sử dụng phương thức
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
8 của đối tượng ZipFile. Điều này sẽ cho phép bạn truy cập vào thông tin cụ thể đối với tập tin đó, giống như kích thước nén và chưa nén của tập tin hoặc lần sửa đổi cuối cùng của nó. Chúng ta sẽ quay trở lại điều đó sau.

Gọi phương thức

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
8 từng cái một trên tất cả các tập tin có thể là một quá trình nhọc nhằn khi có rất nhiều tập tin cần phải được xử lý. Trong trường hợp này, bạn có thể sử dụng phương thức
# Don't forget to close the file!
zf.close()
0 để trả về một danh sách có chứa một đối tượng ZipInfo cho mỗi thành phần trong tập tin nén. Thứ tự của các đối tượng này trong danh sách là giống với các tập tin zip thật sự.

Bạn cũng có thể trực tiếp đọc nội dung của một tập tin cụ thể từ tập tin nén bằng cách sử dụng phương thức

# Don't forget to close the file!
zf.close()
1, trong đó
# Don't forget to close the file!
zf.close()
2 là tên của tập tin bạn muốn đọc. Để làm điều này, tập tin nén phải được mở trong chế độ đọc hoặc nối thêm.

Để có được kích thước nén của một tập tin riêng biệt từ tập tin nén, bạn có thể sử dụng thuộc tính

# Don't forget to close the file!
zf.close()
3. Tương tự như vậy, để biết kích thước chưa nén, bạn có thể sử dụng thuộc tính
# Don't forget to close the file!
zf.close()
4.

Code sau đây sử dụng các thuộc tính và phương thức mà chúng ta vừa mới thảo luận để giải nén chỉ những tập tin có kích thước dưới 1MB.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
3

Để biết ngày và giờ khi một tập tin cụ thể từ tập tin nén bị sửa đổi gần đây nhất, bạn có thể sử dụng thuộc tính

# Don't forget to close the file!
zf.close()
5. Điều này sẽ trả về một tuple gồm sáu giá trị. Các giá trị sẽ là năm, tháng, ngày, giờ, phút và giây, theo thứ tự cụ thể đó. Năm sẽ luôn luôn được lớn hơn hoặc bằng năm 1980, và giờ, phút và giây là bắt đầu từ 0.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
4

Thông tin về kích thước tập tin gốc và kích thước tập tin nén có thể giúp bạn quyết định có nên nén một tập tin hay không. Tôi chắc rằng nó cũng có thể được sử dụng trong một số trường khác.

Tổng kết

Rõ ràng, sử dụng mô-đun

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
6 để nén tập tin cung cấp cho bạn rất nhiều tính linh hoạt. Bạn có thể nén các tập tin khác nhau trong một thư mục thành các tập tin nén khác nhau dựa trên loại, tên hoặc kích thước của chúng. Bạn cũng có thể quyết định xem bạn có muốn giữ lại cấu trúc thư mục hay không. Tương tự, khi giải nén các tập tin, bạn có thể giải nén chúng đến vị trí bạn muốn, dựa trên tiêu chí riêng của bạn như kích thước, vv.

Thật tình, tôi thấy cũng khá thú vị khi nén và giải nén tập tin bằng cách viết code của riêng mình. Tôi hy vọng bạn thích hướng dẫn này, và nếu bạn có bất kỳ câu hỏi nào, xin vui lòng cho tôi biết trong phần bình luận nhé.