views:

89

answers:

1

Hello.

I'm looking for a way to preserve the file attributes (eg. read-only) of a file that gets written to a zipfile.ZipFile instance.

The files I add to the zip archive gets their file attributes reset, eg. the read-only flag is gone when inspecting the archive with zip applications and after unzip.

My current environment is Windows and I'm having problems with the ZipInfo.external_attr method.

Surely there must be an standard way of preserving file attributes when writing to ZipFile?

A: 

The problem I had was the heavily undocumented zipfile.ZipInfo.external_attr. All examples I found of this object refeered to the *nix file permission style.

My implementation will run on windows.

So I went about some "reverse engineering". Heh.

The magic number for windows read-only ZipInfo.external_attr is 33.

As in:

z = zipfile.ZipFile(targetFile, 'w')
(path, filename) = os.path.split(sourceFile)
bytes = file(sourceFile, 'rb')
info = zipfile.ZipInfo(filename)
info.external_attr = 33
z.writestr(info, bytes.read())
bytes.close()
z.close()

If you need to find the correct value for another type of attribute create the zipfile as you want it with some windows zip app and run this on it:

z = zipfile.ZipFile(sourceFile, 'r')
info = z.getinfo('fileToTest.ext')
print ("create_system", info.create_system)
print ("external_attr", info.external_attr)
print ("internal_attr", info.internal_attr)

Cheers!

Elijah