# pydoc > zipfile

---
type: CommandReference
command: zipfile
mode: pydoc
section: 
source: pydoc3
---

## Quick Reference
- `with ZipFile('file.zip') as zf: print(zf.namelist())` — List all file names in the ZIP.
- `ZipFile('file.zip').extract('member.txt', path='out/')` — Extract a member.
- `ZipFile('file.zip').extractall('out/')` — Extract all members.
- `with ZipFile('new.zip', 'w') as zf: zf.write('localfile.txt')` — Create a ZIP and add a file.
- `with ZipFile('new.zip', 'w') as zf: zf.writestr('hello.txt', 'Hello World')` — Write a string as a file.
- `ZipFile('file.zip').testzip()` — Test archive integrity (returns None if OK).
- `root = zipfile.Path('file.zip'); for item in root.iterdir(): print(item)` — Iterate over contents using Path.
- `zipfile.is_zipfile('file.zip')` — Check if a file is a ZIP.

## Name
zipfile - Read and write ZIP files.

## Synopsis
python
import zipfile
## Classes and Functions

### `zipfile.ZipFile`
- `ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None)` — Open or create a ZIP file.
- `close()` — Close the file and write final records.
- `extract(member, path=None, pwd=None)` — Extract a member to current directory.
- `extractall(path=None, members=None, pwd=None)` — Extract all members.
- `getinfo(name)` — Return `ZipInfo` for a member.
- `infolist()` — Return list of `ZipInfo` objects for all members.
- `namelist()` — Return list of member names.
- `open(name, mode='r', pwd=None, *, force_zip64=False)` — Return file-like object for a member.
- `printdir(file=None)` — Print table of contents.
- `read(name, pwd=None)` — Return bytes of a member.
- `setpassword(pwd)` — Set default password for encrypted files.
- `testzip()` — Check CRCs of all files; returns name of first bad file or None.
- `write(filename, arcname=None, compress_type=None, compresslevel=None)` — Write a file into the archive.
- `writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)` — Write a string or bytes as a file.
- `comment` — Comment text associated with the ZIP file.
- `fp` — File pointer (None if file is not open).

### `zipfile.PyZipFile`
- `PyZipFile(file, mode='r', compression=0, allowZip64=True, optimize=-1)` — Create ZIP archives with Python modules.
- `writepy(pathname, basename='', filterfunc=None)` — Add `.py` files from `pathname`, compiling to `.pyc` if needed.

### `zipfile.ZipInfo`
- `ZipInfo(filename='NoName', date_time=(1980,1,1,0,0,0))` — Information about a member.
- `FileHeader(zip64=None)` — Return per-file header as bytes.
- `is_dir()` — Return True if this member is a directory.
- `from_file(filename, arcname=None, *, strict_timestamps=True)` — Class method to construct `ZipInfo` from a filesystem file.
- Attributes: `CRC`, `comment`, `compress_size`, `compress_type`, `create_system`, `create_version`, `date_time`, `external_attr`, `extra`, `extract_version`, `file_size`, `filename`, `flag_bits`, `header_offset`, `internal_attr`, `orig_filename`, `reserved`, `volume`.

### `zipfile.Path`
- `Path(root, at='')` — A pathlib-compatible interface for zip files.
- `exists()` — Check if the path exists.
- `is_dir()` — Return True if the path is a directory.
- `is_file()` — Return True if the path is a file.
- `iterdir()` — Iterate over directory contents.
- `joinpath(*other)` — Join path components.
- `open(mode='r', *args, pwd=None, **kwargs)` — Open entry as text or binary.
- `read_bytes()` — Read entry as bytes.
- `read_text(*args, **kwargs)` — Read entry as text.
- `__truediv__` — Path join via `/` operator.
- Properties: `filename`, `name`, `parent`.

### Functions
- `is_zipfile(filename)` — Quickly check if a file is a ZIP file by its magic number.

### Constants
- `ZIP_STORED` = 0
- `ZIP_DEFLATED` = 8
- `ZIP_BZIP2` = 12
- `ZIP_LZMA` = 14

### Exceptions
- `BadZipFile` (aliases `error`, `BadZipfile`) — Raised for bad ZIP files.
- `LargeZipFile` — Raised when ZIP64 extensions are needed but disabled.

## Examples

Read a ZIP archive and list its contents:
python
with zipfile.ZipFile('archive.zip', 'r') as zf:
    for name in zf.namelist():
        print(name)
Extract a single file:
python
with zipfile.ZipFile('archive.zip') as zf:
    zf.extract('document.txt', path='extract_dir/')
Create a new ZIP and add files:
python
with zipfile.ZipFile('new.zip', 'w') as zf:
    zf.write('file1.txt')
    zf.writestr('hello.txt', 'Hello World')
Use the Path interface:
python
root = zipfile.Path('archive.zip')
for child in root.iterdir():
    print(child.name)
## See Also
[zipfile documentation](https://docs.python.org/3.10/library/zipfile.html)