简体   繁体   中英

How to create an empty zip file?

I'm using zipfile and under some circumstance I need to create an empty zip file for some placeholder purpose. How can I do this?

I know this:

Changed in version 2.7.1: If the file is created with mode 'a' or 'w' and then closed without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.

but my server uses a lower version as 2.6.

You can create an empty zip file without the need to zipfile as:

empty_zip_data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

with open('empty.zip', 'wb') as zip:
    zip.write(empty_zip_data)

empty_zip_data is the data of an empty zip file.

You can simply do:

from zipfile import ZipFile
archive_name = 'test_file.zip'
with ZipFile(archive_name, 'w') as file:
  pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM