목차

, , , ,

python -m zipfile

Python의 ZIP 아카이브 기본 모듈인 zipfile을 명령줄에서 실행하여 파일 목록 확인, 생성, 압축 해제, 무결성 검사를 수행한다.

Summary

Installation

zipfile은 Python 기본 모듈이므로 별도 설치가 필요하지 않다. Python이 설치되어 있으면 다음 명령으로 모듈과 CLI 사용 가능 여부를 확인한다.

python --version
python -c "import zipfile; print(zipfile.__file__)"
python -m zipfile --help

환경에 따라 Python 실행 파일 이름이 python3인 경우 아래처럼 바꿔 실행한다.

python3 -m zipfile --help

Usage

python -m zipfile -l archive.zip
python -m zipfile -t archive.zip
python -m zipfile -c archive.zip file.txt directory/
python -m zipfile -e archive.zip output-directory/

Options

Examples

목록과 무결성 확인

압축을 풀기 전에 파일 목록과 무결성을 먼저 확인한다.

python -m zipfile --list release.zip
python -m zipfile --test release.zip

ZIP 아카이브 생성

파일 여러 개를 하나의 아카이브로 만든다.

python -m zipfile --create release.zip README.txt dist/

디렉터리를 전달하면 해당 디렉터리와 하위 항목이 포함된다.

지정 디렉터리에 압축 해제

mkdir -p extracted
python -m zipfile --extract release.zip extracted/
신뢰할 수 없는 ZIP 파일은 먼저 목록과 크기를 확인한다. 압축 해제는 기존 파일을 별도 확인 없이 덮어쓸 수 있으며, ZIP bomb는 디스크 공간이나 메모리를 고갈시킬 수 있다. 중요한 파일이 없는 새 디렉터리를 대상으로 사용하는 것이 안전하다.

레거시 파일 이름 인코딩 지정

UTF-8 플래그가 없는 오래된 ZIP의 멤버 이름이 깨질 때 원래 코드 페이지를 지정한다.

python -m zipfile --metadata-encoding cp949 --list legacy.zip
python -m zipfile --metadata-encoding cp949 --extract legacy.zip extracted/

ZIP에 기록된 UTF-8 플래그가 있으면 그 값이 –metadata-encoding보다 우선한다.

Help

python3 -m zipfile --help (Python 3.12.13)

Compatibility

See Also

History