|
| 1 | +--- |
| 2 | +title: Python Zipfile Module - Python Cheatsheet |
| 3 | +description: The random module is a built-in module that allow us to generate random elements. |
| 4 | +--- |
| 5 | + |
| 6 | +# Python Zipfile Module |
| 7 | + |
| 8 | +<base-disclaimer> |
| 9 | + <base-disclaimer-title> |
| 10 | + From the <a target="_blank" href="https://docs.python.org/3/library/zipfile.html">Python 3 documentation</a> |
| 11 | + </base-disclaimer-title> |
| 12 | + <base-disclaimer-content> |
| 13 | + This module provides tools to create, read, write, append, and list a ZIP file. |
| 14 | + </base-disclaimer-content> |
| 15 | +</base-disclaimer> |
| 16 | + |
| 17 | +## Reading ZIP files |
| 18 | + |
| 19 | +```python |
| 20 | +>>> with zipfile.ZipFile('example.zip') as example_zip: |
| 21 | +... print(example_zip.namelist()) |
| 22 | +... spam_info = example_zip.getinfo('spam.txt') |
| 23 | +... print(spam_info.file_size) |
| 24 | +... print(spam_info.compress_size) |
| 25 | +... print('Compressed file is %sx smaller!' % (round(spam_info.file_size / spam_info.compress_size, 2))) |
| 26 | +... |
| 27 | +# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg'] |
| 28 | +# 13908 |
| 29 | +# 3828 |
| 30 | +# 'Compressed file is 3.63x smaller!' |
| 31 | +``` |
| 32 | + |
| 33 | +## Extracting from ZIP Files |
| 34 | + |
| 35 | +The `extractall()` method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory. |
| 36 | + |
| 37 | +```python |
| 38 | +>>> with zipfile.ZipFile('example.zip') as example_zip: |
| 39 | +... example_zip.extractall() |
| 40 | +``` |
| 41 | + |
| 42 | +The `extract()` method for ZipFile objects will extract a single file from the ZIP file: |
| 43 | + |
| 44 | +```python |
| 45 | +>>> with zipfile.ZipFile('example.zip') as example_zip: |
| 46 | +... print(example_zip.extract('spam.txt')) |
| 47 | +... print(example_zip.extract('spam.txt', 'C:\\some\\new\\folders')) |
| 48 | +... |
| 49 | +# 'C:\\spam.txt' |
| 50 | +# 'C:\\some\\new\\folders\\spam.txt' |
| 51 | +``` |
| 52 | + |
| 53 | +## Creating and Adding to ZIP Files |
| 54 | + |
| 55 | +```python |
| 56 | +>>> import zipfile |
| 57 | +>>> |
| 58 | +>>> with zipfile.ZipFile('new.zip', 'w') as new_zip: |
| 59 | +... new_zip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED) |
| 60 | +``` |
| 61 | + |
| 62 | +This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt. |
| 63 | + |
| 64 | +## Reading metadata of ZIP files |
| 65 | + |
| 66 | +### Getting the filenames with namelist() |
| 67 | + |
| 68 | +```python |
| 69 | +>>> import zipfile |
| 70 | +>>> |
| 71 | +>>> with zipfile.ZipFile('example.zip', 'r') as zf: |
| 72 | +... print(zf.namelist()) |
| 73 | +... |
| 74 | +# ['README.txt'] |
| 75 | +``` |
| 76 | + |
| 77 | +### Getting all metadata with infolist() |
| 78 | + |
| 79 | +```python |
| 80 | +>>> import datetime |
| 81 | +>>> import zipfile |
| 82 | +>>> |
| 83 | +>>> with zipfile.ZipFile(archive_name) as zf: |
| 84 | +... for info in zf.infolist(): |
| 85 | +... system = 'Windows' if info.create_system == 0 else 'Unix' |
| 86 | +... modified = datetime.datetime(*info.date_time) |
| 87 | +... print(info.filename) |
| 88 | +... print(f'Comment : {info.comment}') |
| 89 | +... print(f'Modified : {modified}') |
| 90 | +... print(f'System : {system}') |
| 91 | +... print(f'ZIP version : {info.create_version}') |
| 92 | +... print(f'Compressed : {info.compress_size} bytes') |
| 93 | +... print(f'Uncompressed: {info.file_size} bytes') |
| 94 | +... |
| 95 | +# README.txt |
| 96 | +# Comment : b'' |
| 97 | +# Modified : 2022-11-15 06:48:02 |
| 98 | +# System : Unix |
| 99 | +# ZIP version : 30 |
| 100 | +# Compressed : 65 bytes |
| 101 | +# Uncompressed: 76 bytes |
| 102 | +``` |
0 commit comments