Set permissions on a compressed file in python

浪子不回头ぞ 提交于 2019-11-29 14:06:24

I had a similar problem to you, so here is the code spinet from my stuff, this I believe should help here.

# extract all of the zip
for file in zf.filelist:
    name = file.filename
    perm = ((file.external_attr >> 16L) & 0777)
    if name.endswith('/'):
        outfile = os.path.join(dir, name)
        os.mkdir(outfile, perm)
    else:
        outfile = os.path.join(dir, name)
        fh = os.open(outfile, os.O_CREAT | os.O_WRONLY , perm)
        os.write(fh, zf.read(name))
        os.close(fh)
    print "Extracting: " + outfile

You might do something similar, but insert your own logic to calculate your perm value. I should note that I'm using Python 2.5 here, I'm aware of a few incompatibilities with some versions of Python's zipfile support.

Per the documentation, unzip sets the permissions to those stored, under unix. Also, the shell umask is not used. Your best bet is to make sure the perms are set before you zip the file.

Since you can't do that, you will have to try and do what you were trying to do (and get it to work under Debian.)

There have been a number of issues with Pythons zipfile library, including setting the mode of writestr to that of the file being written on some systems, or setting the zip systm to windows instead of unix. So your inconsistent results may mean that nothing has changed.

So you may be completely out of luck.

Extract to stdout (unzip -p) and redirect to a file? If there's more than one file in the zip, you could list the zip contents, and then extract one at a time.

for n in `unzip -l test.zip | awk 'NR > 3 && NF == 4 { print $4 }'`; do unzip -p test.zip $n > $n; done

(yeah, I know you tagged this 'python' :-) )

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!