Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置zip中的文件在Windows下的隐藏属性 #1

Open
alex-zhang opened this issue Nov 4, 2016 · 0 comments
Open

设置zip中的文件在Windows下的隐藏属性 #1

alex-zhang opened this issue Nov 4, 2016 · 0 comments
Milestone

Comments

@alex-zhang
Copy link
Owner

alex-zhang commented Nov 4, 2016

有的时候需要在mac或linux的server生成一写window用的zip资源,但是

https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx

··· python

#!/usr/bin/python

-- coding: utf-8 --

import sys
import os
import zipfile
import shutil

inputZipPath = os.path.normpath(str(sys.argv[1]))

print 'inputZipPath', inputZipPath
print '-----------------------------------'

inputZipDirPath = os.path.dirname(inputZipPath)
inputZipBasename = os.path.basename(inputZipPath)
inputTempZipPath = os.path.join(inputZipDirPath, '__' + inputZipBasename)

shutil.copyfile(inputZipPath, inputTempZipPath)
print 'copy temp file to', inputTempZipPath

with zipfile.ZipFile(inputTempZipPath, 'r', zipfile.ZIP_DEFLATED) as inputTargetZip:
with zipfile.ZipFile(inputZipPath, 'w', zipfile.ZIP_DEFLATED) as outputTargetZip:
for inputfileinfo in inputTargetZip.infolist():
inputfilename = inputfileinfo.filename
inputfileIsDir = inputfilename.endswith('/')
inputfilenameDepth = len(inputfilename.split('/'))
if inputfileIsDir: inputfilenameDepth -= 1
inputfilebasename = os.path.basename(inputfilename)
inputfilepurename = os.path.splitext(inputfilebasename)[0]
inputfileextname = os.path.splitext(inputfilebasename)[1]

  # print inputfilename
  # print inputfilenameDepth

  #we make the fake create_system
  inputfileinfo.create_system = 0

  if inputfilenameDepth == 2:
    if(inputfileIsDir or
      (inputfileextname != '.exe' and
      inputfileextname != '.pdf')):

      # http://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zip/6297838#6297838
      # http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute
      # https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx

      print inputfilename
      print hex(inputfileinfo.external_attr)

      #inputfileinfo.external_attr = inputfileinfo.external_attr | 0x2

      #print inputfileinfo.create_system

      if(inputfileIsDir):
        inputfileinfo.external_attr = 0x12
      else:
        inputfileinfo.external_attr = 0x2

      print 'change to'
      print hex(inputfileinfo.external_attr)

  outputTargetZip.writestr(inputfileinfo, inputTargetZip.read(inputfilename))

os.remove(inputTempZipPath)

···

@alex-zhang alex-zhang added this to the 2016 milestone Nov 4, 2016
Repository owner deleted a comment Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant