本文整理汇总了Python中zipfile.ZipInfo.create_system方法的典型用法代码示例。如果您正苦于以下问题:Python ZipInfo.create_system方法的具体用法?Python ZipInfo.create_system怎么用?Python ZipInfo.create_system使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipfile.ZipInfo
的用法示例。
在下文中一共展示了ZipInfo.create_system方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_path
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def _add_path(self, path, version, myzip):
mtime = os.path.getmtime(path)
info = ZipInfo("versions/%d/%s"%(version, path), Archive.unixtime_to_utcziptime(mtime))
info.create_system = 3
info.extra += struct.pack('<HHBl', 0x5455, 5, 1, mtime)
# http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute
# make mode without file type, which may be system-specific
clean_mode = os.stat(path).st_mode & 0007777
if (os.path.islink(path)):
# set zip file type to link
info.external_attr = (Archive.ZIP_EXT_ATTR_LINK | clean_mode) << 16L
myzip.writestr(info, os.readlink(path))
elif (os.path.isdir(path)):
# set zip file type to dir
info.external_attr = (Archive.ZIP_EXT_ATTR_DIR | clean_mode) << 16L
# dos directory flag
info.external_attr |= 0x10
# it seems we should have a trailing slash for dirs
if not(info.filename.endswith('/')): info.filename = "%s/"%(info.filename)
myzip.writestr(info, '')
for name in os.listdir(path):
self._add_path(os.path.join(path, name), version, myzip)
elif (os.path.isfile(path)):
info.external_attr = (Archive.ZIP_EXT_ATTR_FILE | clean_mode) << 16L
myzip.writestr(info, open(path).read())
else:
raise Exception()
示例2: _write
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def _write (self, zname, str) :
now = datetime.utcnow ().timetuple ()
info = ZipInfo (zname, date_time = now)
info.create_system = 0 # pretend to be fat
info.compress_type = ZIP_DEFLATED
self.ozip.writestr (info, str)
self.written [zname] = 1
示例3: add_file
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def add_file(file_path, arcname):
if not path.islink(file_path):
archive.write(file_path, arcname, ZIP_DEFLATED)
else:
i = ZipInfo(arcname)
i.create_system = 3
i.external_attr = 0xA1ED0000
archive.writestr(i, readlink(file_path))
示例4: make_dir_entry
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def make_dir_entry(name=None, date_time=None, mode=MODE_DIRECTORY):
tt = date_time.timetuple()
dir = ZipInfo()
dir.filename = name+('/' if name[-1] != '/' else '')
dir.orig_filename = dir.filename
dir.date_time = date_time.isocalendar() + (tt.tm_hour,
tt.tm_min, tt.tm_sec)
dir.compress_type = 0
dir.create_system = 0
dir.create_version = 20
dir.extract_version = 10
dir.external_attr = mode
return dir
示例5: make_file_entry
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def make_file_entry(name=None, date_time=None, mode=MODE_FILE | MODE_ARCHIVE):
tt = date_time.timetuple()
file = ZipInfo()
file.filename = name
file.orig_filename = file.filename
file.date_time = date_time.isocalendar() + (tt.tm_hour,
tt.tm_min, tt.tm_sec)
file.compress_type = 8
file.create_system = 0
file.create_version = 20
file.extract_version = 20
file.flag_bits = 2
file.external_attr = mode
return file
示例6: zip_them
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def zip_them(str, data_content):
dimap_file = str + 'dim'
dimap_data_dir = str + 'data'
zip_file_name = 'zipped/' + str + 'zip'
if not exists(zip_file_name):
dest_zip_file = ZipFile(zip_file_name, 'w')
print(("\nZip file created: ", zip_file_name, "."))
data_content = glob(dimap_data_dir + '/*') # find content inside the data dir
zip_info = ZipInfo(dimap_data_dir + '/') # create the directory inside
zip_info.external_attr = 0o775 << 16 # the zipfile, set permissions for the directory
zip_info.create_system = 3 # tell the zip file that we are on UNIX
dest_zip_file.writestr(zip_info, '') # write the info to it
print(("Writing to zip: ", dimap_file, "..."))
dest_zip_file.write(dimap_file, dimap_file, ZIP_DEFLATED) # write the dimap file into
for item in data_content:
print(("Writing to zip: ", item, "..."))
dest_zip_file.write(item, item, ZIP_DEFLATED) # write all dimap data to the zip file
dest_zip_file.close()
else:
print(("Zip file ", zip_file_name, " exists already; skipping."))
示例7: build_zip
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def build_zip():
from contextlib import closing
from itertools import chain
from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED
distpath = join(thisdir, 'dist')
zip_file = '%s-v%s-%s.zip' % (appname, version, gitrev)
print('packaging for distribution: %s' % zip_file)
zip = ZipFile(join(distpath, zip_file), "w", ZIP_DEFLATED)
with closing(zip):
zip.write(join(thisdir, 'changelog.md'), 'changelog.md')
zip.write(join(thisdir, 'COPYING'), 'COPYING')
zip.write(join(thisdir, 'README.md'), 'README.md')
zip.write(join(thisdir, 'bin/xt.py'), 'xt')
app_path = join(thisdir, 'dist', appname + '.app')
trimlen = len(distpath) + 1
for dirpath, dirnames, filenames in os.walk(app_path):
zpath = dirpath[trimlen:]
if filenames or dirnames:
dirs = ((item, True) for item in dirnames)
files = ((item, False) for item in filenames)
for filename, isdir in chain(dirs, files):
filepath = join(dirpath, filename)
zip_path = join(zpath, filename)
if os.path.islink(filepath):
info = ZipInfo(zip_path)
info.create_system = 3
info.external_attr = 2716663808
linkdest = os.readlink(filepath)
assert not os.path.isabs(linkdest), linkdest
zip.writestr(info, linkdest)
elif not isdir:
zip.write(filepath, zip_path)
else:
# write empty directory
info = ZipInfo(zpath + os.path.sep)
zip.writestr(info, '')
return zip.filename
示例8: decryptBook
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def decryptBook(userkey, inpath, outpath):
if AES is None:
raise ADEPTError(u"PyCrypto or OpenSSL must be installed.")
rsa = RSA(userkey)
with closing(ZipFile(open(inpath, 'rb'))) as inf:
namelist = set(inf.namelist())
if 'META-INF/rights.xml' not in namelist or \
'META-INF/encryption.xml' not in namelist:
print u"{0:s} is DRM-free.".format(os.path.basename(inpath))
return 1
for name in META_NAMES:
namelist.remove(name)
try:
rights = etree.fromstring(inf.read('META-INF/rights.xml'))
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
expr = './/%s' % (adept('encryptedKey'),)
bookkey = ''.join(rights.findtext(expr))
if len(bookkey) != 172:
print u"{0:s} is not a secure Adobe Adept ePub.".format(os.path.basename(inpath))
return 1
bookkey = rsa.decrypt(bookkey.decode('base64'))
# Padded as per RSAES-PKCS1-v1_5
if bookkey[-17] != '\x00':
print u"Could not decrypt {0:s}. Wrong key".format(os.path.basename(inpath))
return 2
encryption = inf.read('META-INF/encryption.xml')
decryptor = Decryptor(bookkey[-16:], encryption)
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
with closing(ZipFile(open(outpath, 'wb'), 'w', **kwds)) as outf:
zi = ZipInfo('mimetype')
zi.compress_type=ZIP_STORED
try:
# if the mimetype is present, get its info, including time-stamp
oldzi = inf.getinfo('mimetype')
# copy across fields to be preserved
zi.date_time = oldzi.date_time
zi.comment = oldzi.comment
zi.extra = oldzi.extra
zi.internal_attr = oldzi.internal_attr
# external attributes are dependent on the create system, so copy both.
zi.external_attr = oldzi.external_attr
zi.create_system = oldzi.create_system
except:
pass
outf.writestr(zi, inf.read('mimetype'))
for path in namelist:
data = inf.read(path)
zi = ZipInfo(path)
zi.compress_type=ZIP_DEFLATED
try:
# get the file info, including time-stamp
oldzi = inf.getinfo(path)
# copy across useful fields
zi.date_time = oldzi.date_time
zi.comment = oldzi.comment
zi.extra = oldzi.extra
zi.internal_attr = oldzi.internal_attr
# external attributes are dependent on the create system, so copy both.
zi.external_attr = oldzi.external_attr
zi.create_system = oldzi.create_system
except:
pass
outf.writestr(zi, decryptor.decrypt(path, data))
except:
print u"Could not decrypt {0:s} because of an exception:\n{1:s}".format(os.path.basename(inpath), traceback.format_exc())
return 2
return 0
示例9: decryptBook
# 需要导入模块: from zipfile import ZipInfo [as 别名]
# 或者: from zipfile.ZipInfo import create_system [as 别名]
def decryptBook(keyb64, inpath, outpath):
if AES is None:
raise IGNOBLEError(u"PyCrypto or OpenSSL must be installed.")
key = keyb64.decode("base64")[:16]
aes = AES(key)
with closing(ZipFile(open(inpath, "rb"))) as inf:
namelist = set(inf.namelist())
if "META-INF/rights.xml" not in namelist or "META-INF/encryption.xml" not in namelist:
print u"{0:s} is DRM-free.".format(os.path.basename(inpath))
return 1
for name in META_NAMES:
namelist.remove(name)
try:
rights = etree.fromstring(inf.read("META-INF/rights.xml"))
adept = lambda tag: "{%s}%s" % (NSMAP["adept"], tag)
expr = ".//%s" % (adept("encryptedKey"),)
bookkey = "".join(rights.findtext(expr))
if len(bookkey) != 64:
print u"{0:s} is not a secure Barnes & Noble ePub.".format(os.path.basename(inpath))
return 1
bookkey = aes.decrypt(bookkey.decode("base64"))
bookkey = bookkey[: -ord(bookkey[-1])]
encryption = inf.read("META-INF/encryption.xml")
decryptor = Decryptor(bookkey[-16:], encryption)
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
with closing(ZipFile(open(outpath, "wb"), "w", **kwds)) as outf:
zi = ZipInfo("mimetype")
zi.compress_type = ZIP_STORED
try:
# if the mimetype is present, get its info, including time-stamp
oldzi = inf.getinfo("mimetype")
# copy across fields to be preserved
zi.date_time = oldzi.date_time
zi.comment = oldzi.comment
zi.extra = oldzi.extra
zi.internal_attr = oldzi.internal_attr
# external attributes are dependent on the create system, so copy both.
zi.external_attr = oldzi.external_attr
zi.create_system = oldzi.create_system
except:
pass
outf.writestr(zi, inf.read("mimetype"))
for path in namelist:
data = inf.read(path)
zi = ZipInfo(path)
zi.compress_type = ZIP_DEFLATED
try:
# get the file info, including time-stamp
oldzi = inf.getinfo(path)
# copy across useful fields
zi.date_time = oldzi.date_time
zi.comment = oldzi.comment
zi.extra = oldzi.extra
zi.internal_attr = oldzi.internal_attr
# external attributes are dependent on the create system, so copy both.
zi.external_attr = oldzi.external_attr
zi.create_system = oldzi.create_system
except:
pass
outf.writestr(zi, decryptor.decrypt(path, data))
except:
print u"Could not decrypt {0:s} because of an exception:\n{1:s}".format(
os.path.basename(inpath), traceback.format_exc()
)
return 2
return 0