MeloS
MeloS

Reputation: 7938

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.

Upvotes: 16

Views: 13350

Answers (3)

rmuller
rmuller

Reputation: 12859

If on Linux:

echo 504b0506000000000000000000000000000000000000 | xxd -r -p > empty.zip

Upvotes: 1

Minions
Minions

Reputation: 5467

You can simply do:

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

Upvotes: 12

user1129665
user1129665

Reputation:

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.

Upvotes: 22

Related Questions