#!/usr/bin/env python3 import os import shutil import tempfile import zipfile def not_seekable(*args, **kwargs): raise AttributeError('not seekable') with tempfile.TemporaryDirectory() as tmpd: input_name = 'test.txt' input_path = os.path.join(tmpd, input_name) with open(input_path, 'w') as f: f.write('hello world') zf = open('test.zip', 'wb+') zf.tell = not_seekable zip_file = zipfile.ZipFile(zf, mode='x', compression=zipfile.ZIP_STORED, allowZip64=True) zip_info = zipfile.ZipInfo( filename=input_name, date_time=(2016, 1, 1, 1, 1, 1), ) zip_info.external_attr = 0o600 << 16 # ?rw------- zip_info.compress_type = zipfile.ZIP_DEFLATED with zip_file.open(zip_info, mode='w') as dst: with open(input_path, 'rb') as src: src.tell = not_seekable shutil.copyfileobj(src, dst) zip_file.close() zf.close()