brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m zipfile -l lists a zip archive when unzip is missing

Python
~/tips/tip-python-zipfile-cli-list

python3 -m zipfile -l backup.zip

Lists, tests, extracts or creates zip archives with the Python standard library.

A minimal server or a locked-down Windows machine might lack unzip and zip. If Python is there, it can do the job.

python3 -m zipfile -l backup.zip
python3 -m zipfile -e backup.zip ./restore
python3 -m zipfile -c site.zip index.html css/

-l lists the files in the archive, -e extracts it into a folder, -c creates an archive from the files and folders you name, and -t tests whether the archive is valid. The long forms are --list, --extract, --create and --test.

Listing first is a good habit before you extract an archive from someone else, so you see what it contains and where it will end up.

Since Python 3.11 you can add --metadata-encoding to set the encoding of file names for -l, -e and -t. That helps with archives from older Windows tools, where names with accents come out as nonsense.

The caveat is that it is a basic tool. It may not restore Unix permissions the way unzip does, and the command line has no options for passwords or compression levels. Use it as a fallback, not as the main way to handle zip files.

Further reading: Python zipfile module

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link