本文整理汇总了Python中zipfile.ZipFile.add方法的典型用法代码示例。如果您正苦于以下问题:Python ZipFile.add方法的具体用法?Python ZipFile.add怎么用?Python ZipFile.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipfile.ZipFile
的用法示例。
在下文中一共展示了ZipFile.add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def create(self, output_path, dry_run=False, output_format=None):
"""
Create the archive at output_file_path.
Type of the archive is determined either by extension of output_file_path or by output_format.
Supported formats are: gz, zip, bz2, tar, tgz
@param output_path: Output file path.
@type output_path: string
@param dry_run: Determines whether create should do nothing but print what it would archive.
@type dry_run: bool
@param output_format: Determines format of the output archive. If None, format is determined from extension
of output_file_path.
@type output_format: string
"""
if output_format is None:
file_name, file_ext = path.splitext(output_path)
output_format = file_ext[len(extsep):].lower()
self.LOG.debug("Output format is not explicitly set, determined format is {0}.".format(output_format))
if not dry_run:
if output_format == 'zip':
archive = ZipFile(path.abspath(output_path), 'w')
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))
elif output_format in ['tar', 'bz2', 'gz', 'tgz']:
if output_format == 'tar':
t_mode = 'w'
elif output_format == 'tgz':
t_mode = 'w:gz'
else:
t_mode = 'w:{0}'.format(output_format)
archive = tarfile.open(path.abspath(output_path), t_mode)
add_file = lambda file_path, arcname: archive.add(file_path, arcname)
else:
raise RuntimeError("Unknown format: {0}".format(output_format))
def archiver(file_path, arcname):
self.LOG.debug("Compressing {0} => {1}...".format(file_path, arcname))
add_file(file_path, arcname)
else:
archive = None
archiver = lambda file_path, arcname: self.LOG.info("{0} => {1}".format(file_path, arcname))
self.archive_all_files(archiver)
if archive is not None:
archive.close()
示例2: create
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def create(self, output_file):
"""
create(str output_file) -> None
Creates the archive, written to the given output_file
Filetype may be one of: gz, zip, bz2, tar, tgz
"""
#
# determine the format
#
_, _, format = output_file.rpartition(".")
format = format.lower()
if format == 'zip':
from zipfile import ZipFile, ZIP_DEFLATED
output_archive = ZipFile(path.abspath(output_file), 'w')
add = lambda name, arcname: output_archive.write(name, self.prefix + arcname, ZIP_DEFLATED)
elif format in ['tar', 'bz2', 'gz', 'tgz']:
import tarfile
if format == 'tar':
t_mode = 'w'
elif format == 'tgz':
t_mode = 'w:gz'
else:
t_mode = ('w:%s' % format)
output_archive = tarfile.open(path.abspath(output_file), t_mode)
add = lambda name, arcname: output_archive.add(name, self.prefix + arcname)
else:
raise RuntimeError("Unknown format: '%s'" % format)
#
# compress
#
# extra files first (we may change folder later)
for name in self.extra:
if self.verbose:
toPath = '=> %s%s' % (self.prefix, name) if self.prefix else ""
print 'Compressing %s %s ...' % (name, toPath)
add(name, name)
self._excludes = []
for name, arcname in self.listFiles(path.abspath('')):
if self.verbose:
toPath = '=> %s%s' % (self.prefix, arcname) if self.prefix else ""
print 'Compressing %s %s ...' % (arcname, toPath)
add(name, arcname)
output_archive.close()
示例3: _createZip
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def _createZip(self, outputFile):
"""
Helper method to create a ZIP file.
:param outputFile: The target location for the new zip file.
:type outputFile: `FilePath`
:return: `ZipFile`
"""
def zip_writer(dirpath, zippath):
basedir = os.path.dirname(dirpath) + os.sep
entry = ZipInfo()
entry.compress_type = compression
if os.path.isdir(dirpath):
for root, dirs, files in os.walk(dirpath):
if os.path.basename(root).startswith('.'):
# skip hidden directories
continue
dirname = root.replace(basedir, '')
for f in files:
if f[-1] == '~' or f.startswith('.'):
# skip backup files and all hidden files
continue
src = root + '/' + f
entry = ZipInfo()
entry.compress_type = compression
entry.filename = dirname + '/' + f
entry.date_time = localtime(os.path.getmtime(src))[:6]
# hacky
if dirname.startswith("html"):
if self.source == True:
entry.filename = dirname.replace('html', 'doc', 1) + "/" + f
else:
entry.filename = dirname.replace('html/', '', 1) + "/" + f
entry.filename = entry.filename.replace('html/', '', 1)
if entry.filename.startswith("examples"):
entry.filename = "tutorials/" + entry.filename
file_data = open( src, 'rb').read()
self.package.writestr(entry, file_data)
else:
# top files
entry.date_time = localtime(os.path.getmtime(dirpath))[:6]
entry.filename = os.path.basename(zippath)
file_data = open( dirpath, 'rb').read()
self.package.writestr(entry, file_data)
zipfile = ZipFile(outputFile.path, 'w')
zipfile.add = zip_writer
return zipfile
示例4: write_tarball
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def write_tarball(folder, wd, name, version, files, format, verbose=False, clobber=False):
"""
Write a new tarball to the designated folder
Also name, version, included files are passed
folder: output tarball in this folder; a string object.
wd: working directory, where the pkg.spec file is; a string object.
format: "gz", "bz2", "tar" or "zip" to choose output format; a string object.
"""
formats = {
"gz": (".tar.gz", "w:gz"),
"bz2" : (".tar.bz2", "w:bz2"),
"tar" : (".tar", "w:"),
"zip" : (".zip", "w")
}
try:
extension, compress_mode = formats[format]
except KeyError:
raise Exception("Error: No such format %s" % format)
basename = "%s-%s%s" % (name, version, extension)
out_file = path.join(folder, basename)
if not clobber:
if path.exists(out_file):
raise Exception("Error: Output %s already exists!" % basename)
if "zip" in format:
from zipfile import ZipFile, ZIP_DEFLATED
zip_file = ZipFile(out_file, mode=compress_mode, compression=ZIP_DEFLATED)
context = (zip_file, wd, verbose)
write_member = lambda abs, rel: write_zip_file(context, abs, rel)
else:
from tarfile import TarFile
zip_file = TarFile.open(out_file, mode=compress_mode)
write_member = lambda abs, rel: zip_file.add(abs, arcname=rel)
print "Creating", basename
for rel_path in files:
if verbose:
print "Writing", rel_path
abs_path = path.join(wd, rel_path)
write_member(abs_path, rel_path)
zip_file.close()
示例5: archive_git
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def archive_git(output_file,format='tar',repo_root='.', verbose=False):
def git_files(repo_root=repo_root,exclude=True,verbose=False,prefix=''):
excludes = []
for filepath in Popen('cd %s && git ls-files --cached --full-name --no-empty-directory'%(repo_root,), shell=True, stdout=PIPE).stdout.read().splitlines():
filename = path.join(filepath,prefix)
fullpath = path.join(repo_root,filepath)
# right now every time we find a .gitattributes, we
# overwrite the previous patterns. might be nice to
# use a dictionary that retains the patterns on a
# parent-path level. But this works fine for a top-level
# file in the main repo and top level of submodules
if exclude and filename == '.gitattributes':
excludes = []
fh = open(filepath, 'r')
for line in fh:
if not line: break
tokens = line.strip().split()
if 'export-ignore' in tokens[1:]:
excludes.append(tokens[0])
fh.close()
if not filename.startswith('.git') and not path.isdir(filepath):
# check the patterns first
ignore = False
for pattern in excludes:
if fnmatch(filepath, pattern) or fnmatch(filename, pattern):
if verbose: print 'Exclude pattern matched (%s): %s' % (pattern, filepath)
ignore = True
break
if ignore:
continue
# baselevel is needed to tell the arhiver where it have to extract file
yield fullpath,filename
# get paths for every submodule
for submodule in Popen("cd %s && git submodule --quiet foreach --recursive 'pwd'"%(repo_root,), shell=True, stdout=PIPE).stdout.read().splitlines():
#chdir(submodule)
# in order to get output path we need to exclude repository path from the submodule path
prefix = submodule[len(repo_root)+1:]
# recursion allows us to process repositories with more than one level of submodules
#for git_file in git_files(path.join(repo_root,submodule)):
for git_file in git_files(submodule,prefix=prefix):
yield git_file
if format == 'zip':
from zipfile import ZipFile, ZIP_DEFLATED
output_archive = ZipFile(path.abspath(output_file), 'w')
for name, arcname in git_files():
if verbose: print 'Compressing ' + arcname + '...'
output_archive.write(name, prefix + arcname, ZIP_DEFLATED)
elif format == 'tar':
from tarfile import TarFile
output_archive = TarFile(path.abspath(output_file), 'w')
for name, arcname in git_files():
if verbose: print 'Compressing ' + arcname + '...'
#output_archive.add(path.join(name), arcname)
#output_archive.add(path.join(name))
output_archive.add(name,arcname)
示例6: package
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import add [as 别名]
def package(target, source, env):
"""Builder action for packaging the distribution archives."""
# Print out.
print('')
print("#######################")
print("# Packaging the files #")
print("#######################")
# List of distribution files.
type_list = [env['DIST_TYPE']]
if type_list[0] == 'ALL':
type_list = ['zip', 'tar']
# Loop over the distribution files.
for dist_type in type_list:
# The file name.
if dist_type == 'zip':
file = env['DIST_FILE'] + '.zip'
elif dist_type == 'tar':
file = env['DIST_FILE'] + '.tar.bz2'
elif dist_type == 'dmg':
file = env['DIST_FILE'] + '.dmg'
# Print out.
print("\n\nCreating the package distribution " + repr(file) + ".\n")
# Create the special Mac OS X DMG file and then stop execution.
if dist_type == 'dmg':
# Create the Mac OS X universal application.
print("\n# Creating the Mac OS X universal application.\n\n")
cmd = '%s setup.py py2app' % sys.executable
print("%s\n" % cmd)
pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
waitpid(pipe.pid, 0)
# Create the dmg image.
print("\n\n# Creating the DMG image.\n\n")
cmd = 'hdiutil create -ov -fs HFS+ -volname "relax" -srcfolder dist/relax.app ../%s' % file
print("%s\n" % cmd)
pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
waitpid(pipe.pid, 0)
# Stop executing.
return
# Open the Zip distribution file.
if dist_type == 'zip':
archive = ZipFile(path.pardir + path.sep + file, 'w', compression=8)
# Open the Tar distribution file.
elif dist_type == 'tar':
if search('.bz2$', file):
archive = TarFile.bz2open(path.pardir + path.sep + file, 'w')
elif search('.gz$', file):
archive = TarFile.gzopen(path.pardir + path.sep + file, 'w')
else:
archive = TarFile.open(path.pardir + path.sep + file, 'w')
# Base directory.
base = getcwd() + sep
# Walk through the directories.
for root, dirs, files in walk(getcwd()):
# Skip the subversion directories.
if search("\.svn", root):
continue
# Add the files in the current directory to the archive.
for i in range(len(files)):
# Skip any '.sconsign' files, hidden files, byte-compiled '*.pyc' files, or binary objects '.o', '.os', 'obj', 'lib', and 'exp'.
if search("\.sconsign", files[i]) or search("^\.", files[i]) or search("\.pyc$", files[i]) or search("\.o$", files[i]) or search("\.os$", files[i]) or search("\.obj$", files[i]) or search("\.lib$", files[i]) or search("\.exp$", files[i]):
continue
# Create the file name (without the base directory).
name = path.join(root, files[i])
name = name[len(base):]
print('relax-' + version + path.sep + name)
# The archive file name.
arcname = 'relax-' + version + path.sep + name
# Zip archives.
if dist_type == 'zip':
archive.write(filename=name, arcname=arcname)
# Tar archives.
if dist_type == 'tar':
archive.add(name=name, arcname=arcname)
# Close the archive.
archive.close()
# Final printout.
print("\n\n\n")