本文整理匯總了Python中zipfile.ZIP_DEFLATED屬性的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.ZIP_DEFLATED屬性的具體用法?Python zipfile.ZIP_DEFLATED怎麽用?Python zipfile.ZIP_DEFLATED使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類zipfile
的用法示例。
在下文中一共展示了zipfile.ZIP_DEFLATED屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: pickle_load
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def pickle_load(path, compression=False):
"""Unpickle a possible compressed pickle.
Parameters
----------
path: str
path to the output file
compression: bool
if true assumes that pickle was compressed when created and attempts decompression.
Returns
-------
obj: object
the unpickled object
"""
if compression:
with zipfile.ZipFile(path, "r", compression=zipfile.ZIP_DEFLATED) as myzip:
with myzip.open("data") as f:
return pickle.load(f)
else:
with open(path, "rb") as f:
return pickle.load(f)
示例2: create_function_handler_zip
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def create_function_handler_zip(zip_location, main_exec_file, backend_location):
logger.debug("Creating function handler zip in {}".format(zip_location))
def add_folder_to_zip(zip_file, full_dir_path, sub_dir=''):
for file in os.listdir(full_dir_path):
full_path = os.path.join(full_dir_path, file)
if os.path.isfile(full_path):
zip_file.write(full_path, os.path.join('pywren_ibm_cloud', sub_dir, file))
elif os.path.isdir(full_path) and '__pycache__' not in full_path:
add_folder_to_zip(zip_file, full_path, os.path.join(sub_dir, file))
try:
with zipfile.ZipFile(zip_location, 'w', zipfile.ZIP_DEFLATED) as pywren_zip:
current_location = os.path.dirname(os.path.abspath(backend_location))
module_location = os.path.dirname(os.path.abspath(pywren_ibm_cloud.__file__))
main_file = os.path.join(current_location, 'entry_point.py')
pywren_zip.write(main_file, main_exec_file)
add_folder_to_zip(pywren_zip, module_location)
except Exception:
raise Exception('Unable to create the {} package: {}'.format(zip_location))
示例3: train
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def train(config):
c = Classifier.classifier(config)
pprint.pprint(c.tuples.all_tuples)
print('All tuples:',len(c.tuples.all_tuples))
model_path = config.get("train", "output")
model_dir = os.path.dirname(model_path)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
print('output to {}'.format(model_path))
dataroot = config.get("train", "dataroot")
dataset = config.get("train", "dataset")
dw = sutils.dataset_walker(dataset = dataset, dataroot=dataroot, labels=True)
c = Classifier.classifier(config)
c.cacheFeature(dw)
c.train(dw)
c.save(model_path)
with zipfile.ZipFile(os.path.join(model_dir, 'svm_multiwoz.zip'), 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(model_path)
示例4: ZipDir
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def ZipDir(sourceDirectory, outputFilePath, outputHandler):
currentDir = os.getcwd()
try:
os.chdir(sourceDirectory)
#relroot = os.path.abspath(os.path.join(sourceDirectory, os.pardir))
relroot = os.path.abspath(os.path.join(sourceDirectory))
#with zipfile.ZipFile(outputFilePath, "w", zipfile.ZIP_DEFLATED) as zip:
with zipfile.ZipFile(outputFilePath, "w") as zip:
for root, dirs, files in os.walk(sourceDirectory):
# add directory (needed for empty dirs)
# this is commented out because Tomcat 8 will reject WAR files with "./" in them.
#zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename): # regular files only
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)
return True
except Exception as e:
outputHandler.outputMessage('Error creating zip file "%s" from directory "%s" - %s' % (outputFilePath, sourceDirectory, e))
return False
os.chdir(currentDir)
示例5: create_zip_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def create_zip_file(files_for_export, filekey):
"""
Takes a list of dictionaries, each with a file object and a name, zips up all the files with those names and returns a zip file buffer.
"""
buffer = BytesIO()
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zip:
for f in files_for_export:
f[filekey].seek(0)
zip.writestr(f["name"], f[filekey].read())
zip.close()
buffer.flush()
zip_stream = buffer.getvalue()
buffer.close()
return zip_stream
示例6: get
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def get(self, *, tid: objectid.ObjectId):
tdoc, tsdocs = await contest.get_and_list_status(self.domain_id, document.TYPE_HOMEWORK, tid)
rnames = {}
for tsdoc in tsdocs:
for pdetail in tsdoc.get('detail', []):
rnames[pdetail['rid']] = 'U{}_P{}_R{}'.format(tsdoc['uid'], pdetail['pid'], pdetail['rid'])
output_buffer = io.BytesIO()
zip_file = zipfile.ZipFile(output_buffer, 'a', zipfile.ZIP_DEFLATED)
rdocs = record.get_multi(get_hidden=True, _id={'$in': list(rnames.keys())})
async for rdoc in rdocs:
zip_file.writestr(rnames[rdoc['_id']] + '.' + rdoc['lang'], rdoc['code'])
# mark all files as created in Windows :p
for zfile in zip_file.filelist:
zfile.create_system = 0
zip_file.close()
await self.binary(output_buffer.getvalue(), 'application/zip',
file_name='{}.zip'.format(tdoc['title']))
示例7: get
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def get(self, *, tid: objectid.ObjectId):
tdoc, tsdocs = await contest.get_and_list_status(self.domain_id, document.TYPE_CONTEST, tid)
rnames = {}
for tsdoc in tsdocs:
for pdetail in tsdoc.get('detail', []):
rnames[pdetail['rid']] = 'U{}_P{}_R{}'.format(tsdoc['uid'], pdetail['pid'], pdetail['rid'])
output_buffer = io.BytesIO()
zip_file = zipfile.ZipFile(output_buffer, 'a', zipfile.ZIP_DEFLATED)
rdocs = record.get_multi(get_hidden=True, _id={'$in': list(rnames.keys())})
async for rdoc in rdocs:
zip_file.writestr(rnames[rdoc['_id']] + '.' + rdoc['lang'], rdoc['code'])
# mark all files as created in Windows :p
for zfile in zip_file.filelist:
zfile.create_system = 0
zip_file.close()
await self.binary(output_buffer.getvalue(), 'application/zip',
file_name='{}.zip'.format(tdoc['title']))
示例8: _write_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def _write_file(self, archive, path: str, fpath: Path) -> None:
# write content into archive
archive.write(filename=str(fpath), arcname=path, compress_type=ZIP_DEFLATED)
# calculate hashsum
# https://stackoverflow.com/questions/22058048/hashing-a-file-in-python
digest = sha256()
with fpath.open('rb') as stream:
while True:
data = stream.read(65536)
if not data:
break
digest.update(data)
digest = urlsafe_b64encode(digest.digest()).decode().rstrip('=')
self._records.append((path, digest, fpath.stat().st_size))
示例9: write_json_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def write_json_file(self):
# get export, project and json path
export_path = bpy.path.abspath(self.scene.coa_export_path)
json_path = os.path.join(export_path, self.project_name + "_data.json")
zip_path = os.path.join(export_path, self.project_name + "_data.zip")
# write json file
if self.reduce_size:
json_file = json.dumps(self.json_data, separators=(',', ':'))
else:
json_file = json.dumps(self.json_data, indent=" ", sort_keys=False)
text_file = open(json_path, "w")
text_file.write(json_file)
text_file.close()
zip_file = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
zip_file.write(json_path, os.path.basename(json_path))
zip_file.close()
示例10: export_debug
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def export_debug(self, output_path):
"""
this method is used to generate a debug map for NEO debugger
"""
file = open(output_path, 'rb')
file_hash = hashlib.md5(file.read()).hexdigest()
file.close()
avm_name = os.path.splitext(os.path.basename(output_path))[0]
debug_info = self.generate_avmdbgnfo(avm_name, file_hash)
debug_json_filename = os.path.basename(output_path.replace('.avm', '.debug.json'))
avmdbgnfo_filename = output_path.replace('.avm', '.avmdbgnfo')
with zipfile.ZipFile(avmdbgnfo_filename, 'w', zipfile.ZIP_DEFLATED) as avmdbgnfo:
avmdbgnfo.writestr(debug_json_filename, debug_info)
示例11: _createArchiveFile
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def _createArchiveFile(archive_path, abs_base_dir, abs_paths):
with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED) as zipf:
for file in abs_paths:
if not os.path.exists(file):
zipf.close()
if os.path.isfile(archive_path):
os.remove(archive_path)
raise BenchExecException(
"Missing file '{0}', cannot run benchmark without it.".format(
os.path.normpath(file)
)
)
if os.path.isdir(file):
_zipdir(file, zipf, abs_base_dir)
else:
zipf.write(file, os.path.relpath(file, abs_base_dir))
示例12: _zip_files
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def _zip_files(dir_path, _size=1):
"""
If file size is greater then given _size, create zip of file on same location and
remove original one. Won't zip If zlib module is not available.
"""
for root, dirs, files in os.walk(dir_path):
for _file in files:
file_path = os.path.join(root, _file)
size_mb = (old_div(os.path.getsize(file_path), (1024 * 1024)))
if size_mb >= _size:
os.chdir(root)
try:
newzip = zipfile.ZipFile(
_file + ".zip", "w", zipfile.ZIP_DEFLATED)
newzip.write(_file)
newzip.close()
os.remove(_file)
except Exception as e:
print(e)
pass
示例13: savekmz
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def savekmz(self, path, format=True):
"""
Save the kml as a kmz file to the given file supplied by `path`.
Keyword arguments:
path (string) -- the path of the kmz file to be saved
format (bool) -- format the resulting kml "prettyprint" (default True)
"""
Kmlable._setkmz()
out = self._genkml(format).encode('utf-8')
kmz = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED)
kmz.writestr("doc.kml", out)
for image in Kmlable._getimages():
kmz.write(image, os.path.join('files', os.path.split(image)[1]))
kmz.close()
Kmlable._clearimages()
示例14: zip_directory
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def zip_directory(src_dir, dst):
assert os.path.isdir(src_dir), "Invalid, not a directory: '{}'".format(src_dir)
with Archive._open["zip"](dst, 'w', zipfile.ZIP_DEFLATED) as write_handler:
Archive.zip_stream(src_dir, write_handler)
示例15: dir2zip
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZIP_DEFLATED [as 別名]
def dir2zip(in_dir, zip_fname):
""" Make a zip file `zip_fname` with contents of directory `in_dir`
The recorded filenames are relative to `in_dir`, so doing a standard zip
unpack of the resulting `zip_fname` in an empty directory will result in
the original directory contents.
Parameters
----------
in_dir : str
Directory path containing files to go in the zip archive
zip_fname : str
Filename of zip archive to write
"""
z = zipfile.ZipFile(zip_fname, 'w',
compression=zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(in_dir):
for file in files:
in_fname = pjoin(root, file)
in_stat = os.stat(in_fname)
# Preserve file permissions, but allow copy
info = zipfile.ZipInfo(in_fname)
info.filename = relpath(in_fname, in_dir)
if os.path.sep == '\\':
# Make the path unix friendly on windows.
# PyPI won't accept wheels with windows path separators
info.filename = relpath(in_fname, in_dir).replace('\\', '/')
# Set time from modification time
info.date_time = time.localtime(in_stat.st_mtime)
# See https://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zip/48435482#48435482 # noqa: E501
# Also set regular file permissions
perms = stat.S_IMODE(in_stat.st_mode) | stat.S_IFREG
info.external_attr = perms << 16
with open_readable(in_fname, 'rb') as fobj:
contents = fobj.read()
z.writestr(info, contents, zipfile.ZIP_DEFLATED)
z.close()