I can create and read archive with the Python3 package zipfile
without any problems. But I am not able to open that archives with other programms - not on Windows 10 (in-build extractor) nor GNU/Linux.
My goal is to create zip files with Python3 that can opened on nearly every machine and every zipper-programm.
The error messages using Windows10 archiver or XArchiver on XFCE are unspecific. But on the shell (Debian GNU/Linux 10) I can see this
$ unzip the_archive.zip
Archive: the_archive.zip
skipping: in_zip.txt need PK compat. v6.3 (can do v4.6)
This is Python3 code to create such an archive
#!/usr/bin/env python3
import zipfile
# the file that will be zipped
with open('in_zip.txt', 'w') as f:
f.write('foobar')
# create zip file
with zipfile.ZipFile('the_archive.zip', 'w',
compression=zipfile.ZIP_LZMA,
compresslevel=9) as zip_file: # maximum
zip_file.write('in_zip.txt')
The LZMA
compression method you are using is not widely supported. Below is taken from zipfile man page.
Note The ZIP file format specification has included support for bzip2 compression since 2001, and for LZMA compression since 2006. However, some tools (including older Python releases) do not support these compression methods, and may either refuse to process the ZIP file altogether, or fail to extract individual files
For maximum interoperability, stick to zipfile.ZIP_STORED
and zipfile.ZIP_DEFLATED
.
Next are the filenames you use in the zip file. Although zipfile
can correctly store Unicode filenames in the zip archive, encoded as UTF8, you want to stay with 7-bit ASCII if interoperability is a key factor. Also, on Windows be aware of characters (and filenames) that are to be avoided. See What characters are forbidden in Windows and Linux directory names? for more details.
If you have no choice and need to store filenames that are outside 7-bit ASCII, make sure they are properly encoded as UTF-8. The only portable character sets for zip files are 7-bit ASCII and UTF-8
The other feature to be careful with is ZIP64
support. Most archivers do support it these days, but there are libraries that don't. You will only trigger it if you are dealing with are >4Gig, in which case you don't have much choice.