Skip to content

Instantly share code, notes, and snippets.

@vincascm
Created April 1, 2026 14:42
Show Gist options
  • Select an option

  • Save vincascm/87c45cbc4d076760faf732e3fd69bef1 to your computer and use it in GitHub Desktop.

Select an option

Save vincascm/87c45cbc4d076760faf732e3fd69bef1 to your computer and use it in GitHub Desktop.
A script capable of correctly extracting ZIP archives—packaged on Windows and containing Chinese filenames—on macOS or Linux.
import zipfile
import sys
import os
def main():
if len(sys.argv) < 2:
print("Usage: python x.py <zip_file_path>")
sys.exit(1)
zip_path = sys.argv[1]
if not os.path.exists(zip_path):
print(f"Error: File {zip_path} does not exist.")
sys.exit(1)
output_dir = os.path.splitext(os.path.basename(zip_path))[0]
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"Extracting to: {output_dir}")
with zipfile.ZipFile(zip_path, "r") as zf:
for info in zf.infolist():
try:
name = info.filename.encode("cp437").decode("gbk")
except (UnicodeEncodeError, UnicodeDecodeError):
name = info.filename
name = name.replace("\\", "/")
info.filename = name
print(f" Extracting: {name}")
zf.extract(info, output_dir)
print("\nExtraction complete.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment