Well that's odd. Python's zipfile defaults to the stored compression method, which does not compress! (Why would they do that?)
You need to specify a compression method. Use ZIP_DEFLATED, which is the most widely supported.
import zipfile
zip = zipfile.ZipFile("stuff.zip", "w", zipfile.ZIP_DEFLATED)
zip.write("test.txt")
zip.close()
Answer from Mark Adler on Stack OverflowPython
docs.python.org โบ 3 โบ library โบ zipfile.html
zipfile โ Work with ZIP archives
February 23, 2026 - ZipFile is also a context manager and therefore supports the with statement. In the example, myzip is closed after the with statementโs suite is finishedโeven if an exception occurs:
Videos
04:39
How to Zip a File and Extract from a Zip File in Python - Python ...
How to Zip All Files in a Directory in Python using ZipFile ...
02:10
How to Zip and Unzip Files Using Python's zipfile Module - YouTube
How to create and work with zip archives using Python
Zip a single file using Python | Zipfile Module | Part 1 - YouTube
Python Tutorial: Zip Files - Creating and Extracting Zip Archives
Pythonmania
pythonmania.net โบ en โบ 2017 โบ 06 โบ 25 โบ zip-files-in-python
Zip files in Python - Python Mania
June 25, 2017 - #!/usr/bin/env python import datetime import zipfile zf = zipfile.ZipFile("example.zip", "r") for info in zf.infolist(): print(info.filename) print(" Comment: " + str(info.comment)) print(" Modified: " + str(datetime.datetime(*info.date_time))) print(" System: " + str(info.create_system) + " (0=MS-DOS OS-2, 3=Unix)") print(" ZIP version: " + str(info.create_version)) print(" Compressed: " + str(info.compress_size) + " bytes") print(" Uncompressed: " + str(info.file_size) + " bytes") zf.close()
Top answer 1 of 3
13
Well that's odd. Python's zipfile defaults to the stored compression method, which does not compress! (Why would they do that?)
You need to specify a compression method. Use ZIP_DEFLATED, which is the most widely supported.
import zipfile
zip = zipfile.ZipFile("stuff.zip", "w", zipfile.ZIP_DEFLATED)
zip.write("test.txt")
zip.close()
2 of 3
6
From the https://docs.python.org/3/library/zipfile.html#zipfile-objects it suggest example:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
So your code will be
from zipfile import ZipFile
with ZipFile('compressedtextstuff.zip', 'w', zipfile.ZIP_DEFLATED) as myzip:
myzip.write('testtext.txt')
SQLPad
sqlpad.io โบ tutorial โบ python-zipfile
Python zipfile | SQLPad
April 29, 2024 - Learn how to create, read, extract, and modify ZIP archives with practical examples, by using this built-in standard library tool Python's zipfile module.
Python Module of the Week
pymotw.com โบ 3 โบ zipfile
zipfile โ ZIP Archive Access
Python can import modules from inside ZIP archives using zipimport, if those archives appear in sys.path. The PyZipFile class can be used to construct a module suitable for use in this way. The extra method writepy() tells PyZipFile to scan a directory for .py files and add the corresponding .pyo or .pyc file to the archive. If neither compiled form exists, a .pyc file is created and added. ... import sys import zipfile if __name__ == '__main__': with zipfile.PyZipFile('pyzipfile.zip', mode='w') as zf: zf.debug = 3 print('Adding python files') zf.writepy('.') for name in zf.namelist(): print(name) print() sys.path.insert(0, 'pyzipfile.zip') import zipfile_pyzipfile print('Imported from:', zipfile_pyzipfile.__file__)
W3Schools
w3schools.com โบ python โบ ref_module_zipfile.asp
Python zipfile Module
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... import zipfile with zipfile.ZipFile('example.zip', 'w') as myzip: myzip.writestr('hello.txt', 'Hello, Emil!') myzip.writestr('data.txt', ...
Read the Docs
python.readthedocs.io โบ fr โบ latest โบ library โบ zipfile.html
13.5. zipfile โ Work with ZIP archives โ documentation Python 3.7.0a0
Class for creating ZIP archives containing Python libraries. class zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))ยถ
FavTutor
favtutor.com โบ blogs โบ zipfile-python
How to Work with ZIP Files in Python? (Open, Read & Extract)
April 24, 2023 - The ZipFile class is one of the main classes in the module and provides methods for working with ZIP files. To open the file we use the open() function. The open() function is a built-in Python function that takes two arguments: the first argument is the name of the file to be opened, and the second argument specifies the mode in which the file should be opened.
DataCamp
datacamp.com โบ tutorial โบ zip-file
Python zipfile: Zip, Extract, Read & Create Zip Files | DataCamp
November 29, 2018 - If you are not familiar with the error handling, go to Pythons Error Handling documentation to learn the error handling. Let's see all exceptions in zipfile module. zipfile.BadZipFile is an exception in the zipfile module. This error will raise for Bad Zip files. See the example below.
Python Module of the Week
pymotw.com โบ 2 โบ zipfile
zipfile โ Read and write ZIP archive files - Python Module of the Week
import datetime import zipfile def print_info(archive_name): zf = zipfile.ZipFile(archive_name) for info in zf.infolist(): print info.filename print '\tComment:\t', info.comment print '\tModified:\t', datetime.datetime(*info.date_time) print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)' print '\tZIP version:\t', info.create_version print '\tCompressed:\t', info.compress_size, 'bytes' print '\tUncompressed:\t', info.file_size, 'bytes' print if __name__ == '__main__': print_info('example.zip') There are additional fields other than those printed here, but deciphering the values into anything useful requires careful reading of the PKZIP Application Note with the ZIP file specification. $ python zipfile_infolist.py README.txt Comment: Modified: 2007-12-16 10:08:52 System: 3 (0 = Windows, 3 = Unix) ZIP version: 23 Compressed: 63 bytes Uncompressed: 75 bytes
Stack Abuse
stackabuse.com โบ creating-a-zip-archive-of-a-directory-in-python
Creating a Zip Archive of a Directory in Python
September 14, 2023 - Let's take a look at how we can handle these exceptions while creating a zip file: import zipfile try: with zipfile.ZipFile('example.zip', 'w') as myzip: myzip.write('non_existent_file.txt') except FileNotFoundError: print('The file you are trying to zip does not exist.') except RuntimeError ...