How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module?

前端 未结 7 2115
甜味超标
甜味超标 2020-11-30 06:20

When I extract files from a ZIP file created with the Python zipfile module, all the files are not writable, read only etc.

The file is being created and extracted u

相关标签:
7条回答
  • 2020-11-30 07:13

    This seems to work (thanks Evan, putting it here so the line is in context):

    buffer = "path/filename.zip"  # zip filename to write (or file-like object)
    name = "folder/data.txt"      # name of file inside zip 
    bytes = "blah blah blah"      # contents of file inside zip
    
    zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED)
    info = zipfile.ZipInfo(name)
    info.external_attr = 0777 << 16L # give full access to included file
    zip.writestr(info, bytes)
    zip.close()
    

    I'd still like to see something that documents this... An additional resource I found was a note on the Zip file format: http://www.pkware.com/documents/casestudies/APPNOTE.TXT

    0 讨论(0)
提交回复
热议问题