我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用zipfile.is_zipfile()。
def check_input_conditions(self): if not self.filename: print "[ Error ] Please Provide Zip File Path " sys.exit(0) print "[+] Checking Zip File Condition ...", if not zipfile.is_zipfile(self.filename): print "[ Error ] Bad Zip file" sys.exit(0) print " Ok" if not self.dictionery and not self.crunch: print "[ Error ] Please Provide Dictonery Or Crunch Or Password Option" sys.exit(0) if self.dictionery and self.crunch: print "[ Error ] Please Choose Any One Option From Dict or Crunch" sys.exit(0) return
def find_package(self, package): for path in self.paths(): full = os.path.join(path, package) if os.path.exists(full): return package, full if not os.path.isdir(path) and zipfile.is_zipfile(path): zip = zipfile.ZipFile(path, 'r') try: zip.read(os.path.join(package, '__init__.py')) except KeyError: pass else: zip.close() return package, full zip.close() ## FIXME: need special error for package.py case: raise InstallationError( 'No package with the name %s found' % package)
def unpack_file(filename, location, content_type, link): filename = os.path.realpath(filename) if (content_type == 'application/zip' or filename.endswith('.zip') or filename.endswith('.pybundle') or filename.endswith('.whl') or zipfile.is_zipfile(filename)): unzip_file(filename, location, flatten=not filename.endswith(('.pybundle', '.whl'))) elif (content_type == 'application/x-gzip' or tarfile.is_tarfile(filename) or splitext(filename)[1].lower() in ('.tar', '.tar.gz', '.tar.bz2', '.tgz', '.tbz')): untar_file(filename, location) elif (content_type and content_type.startswith('text/html') and is_svn_page(file_contents(filename))): # We don't really care about this from pip.vcs.subversion import Subversion Subversion('svn+' + link.url).unpack(location) else: ## FIXME: handle? ## FIXME: magic signatures? logger.fatal('Cannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive format' % (filename, location, content_type)) raise InstallationError('Cannot determine archive format of %s' % location)
def get_client_version(my): '''API Function: get_client_version() @return: string - Version of TACTIC that this client came from''' # may use pkg_resources in 2.6 if '.zip' in __file__: import zipfile parts = __file__.split('.zip') zip_name = '%s.zip'%parts[0] if zipfile.is_zipfile(zip_name): z = zipfile.ZipFile(zip_name) version = z.read('pyasm/application/common/interpreter/tactic_client_lib/VERSION') version = version.strip() z.close() else: dir = os.path.dirname(__file__) f = open('%s/VERSION' % dir, 'r') version = f.readline().strip() f.close() return version
def get_client_api_version(my): '''API Function: get_client_api_version() @return: string - client api version''' # may use pkg_resources in 2.6 if '.zip' in __file__: import zipfile parts = __file__.split('.zip') zip_name = '%s.zip'%parts[0] if zipfile.is_zipfile(zip_name): z = zipfile.ZipFile(zip_name) version = z.read('pyasm/application/common/interpreter/tactic_client_lib/VERSION_API') version = version.strip() z.close() else: dir = os.path.dirname(__file__) f = open('%s/VERSION_API' % dir, 'r') version = f.readline().strip() f.close() return version
def find_xorpad(titleid, crc32): expectedname = "%s.%08lx.Main.exheader.xorpad" % (titleid, crc32) legacyname = titleid + ".Main.exheader.xorpad" xorpads = glob.glob(os.path.join("xorpads", "*.[xX][oO][rR][pP][aA][dD]")) xorpads += glob.glob(os.path.join("xorpads", "*.[zZ][iI][pP]")) for xorpad in xorpads: if zipfile.is_zipfile(xorpad): with zipfile.ZipFile(xorpad, "r") as e: for entry in e.infolist(): filename = os.path.join(tmpdir, expectedname) basename = os.path.basename(entry.filename) if basename.lower() == expectedname.lower(): source = e.open(entry, "r") target = file(filename, "wb") with source, target: shutil.copyfileobj(source, target) return filename else: basename = os.path.basename(xorpad) if basename.lower() == expectedname.lower() or \ basename.lower() == legacyname.lower(): return xorpad
def extract_file(self,compressed_file, target_path): """ extract_file(compressed_file, target_path) extracts the compressed_file to target_path """ if os.path.exists(compressed_file): print "Extracting %s to %s\n" % (compressed_file, target_path) if zipfile.is_zipfile(compressed_file): with zipfile.ZipFile(compressed_file, "r") as z: z.extractall(path=target_path) if tarfile.is_tarfile(compressed_file): tar = tarfile.open(compressed_file) tar.extractall(path=target_path) tar.close() print "Cleaning up\n" os.remove(compressed_file) print "Done!\n" else: print "%s does not exist, cannot extract" % compressed_file
def __init__(self, compressed_file, target_path): """ __init__(compressed_file, target_path) extracts the compressed_file to target_path """ if os.path.exists(compressed_file): print "Extracting %s to %s\n" % (compressed_file, target_path) if zipfile.is_zipfile(compressed_file): with zipfile.ZipFile(compressed_file, "r") as z: z.extractall(path=target_path) if tarfile.is_tarfile(compressed_file): tar = tarfile.open(compressed_file) tar.extractall(path=target_path) tar.close() print "Cleaning up\n" os.remove(compressed_file) print "Done!\n" else: print "%s does not exist, cannot extract" % compressed_file
def unzip(source, out_path): '''Unzip function. Arguments: source (str): path to zip file out_path (str): path to out_file ''' print 'Unzipping %s to %s' % (source, out_path) if not zipfile.is_zipfile(source): raise ValueError('%s is not a zipfile' % source) if not path.isdir(out_path): raise ValueError('%s is not a directory' % out_path) with zipfile.ZipFile(source) as zf: zf.extractall(out_path)
def tpn_raw_data(data_path): # return the paths of training and validation zip files train_set = sorted(glob.glob(osp.join(data_path, 'train/*'))) val_set = sorted(glob.glob(osp.join(data_path, 'val/*'))) valid_train_set = [] valid_val_set = [] for set_name, orig_set, valid_set in \ [('train', train_set, valid_train_set), ('val', val_set, valid_val_set)]: print "Checking {} set files...".format(set_name) for ind, orig_vid in enumerate(orig_set, start=1): if zipfile.is_zipfile(orig_vid): valid_set.append(orig_vid) elif osp.isdir(orig_vid): valid_set.append(orig_vid) else: print "{} is not a valid zip file or a directory".format(orig_vid) if ind % 1000 == 0: print "{} files checked.".format(ind) if ind % 1000 != 0: print "Totally {} files checked.".format(ind) return valid_train_set, valid_val_set
def tpn_test_iterator(track_path): """ return values: x: list of tracks """ temp_res = None tracks = [] # zipfile if zipfile.is_zipfile(track_path): zf = zipfile.ZipFile(track_path) track_list = zf.namelist() # print "Loading {} tracks...".format(len(track_list)) for track_name in track_list: tracks.append(cPickle.loads(zf.read(track_name))) zf.close() # folders elif osp.isdir(track_path): track_list = sorted(glob.glob(osp.join(track_path, '*'))) # print "Loading {} tracks...".format(len(track_list)) for track_name in track_list: tracks.append(cPickle.loads(open(track_name, 'rb').read())) else: raise NotImplementedError('Only zipfile and directories are supported.') return tracks
def store(self, project_id, function, data): """Store the function package data to local file system. :param project_id: Project ID. :param function: Function ID. :param data: Package data. """ LOG.info( 'Store package, function: %s, project: %s', function, project_id ) project_path = os.path.join(CONF.storage.file_system_dir, project_id) fileutils.ensure_tree(project_path) new_func_zip = os.path.join(project_path, '%s.zip.new' % function) func_zip = os.path.join(project_path, '%s.zip' % function) with open(new_func_zip, 'wb') as fd: fd.write(data) if not zipfile.is_zipfile(new_func_zip): fileutils.delete_if_exists(new_func_zip) raise exc.InputException("Package is not a valid ZIP package.") os.rename(new_func_zip, func_zip)
def test_is_zip_erroneous_file(self): """Check that is_zipfile() correctly identifies non-zip files.""" # - passing a filename with open(TESTFN, "w") as fp: fp.write("this is not a legal zip file\n") chk = zipfile.is_zipfile(TESTFN) self.assertFalse(chk) # - passing a file object with open(TESTFN, "rb") as fp: chk = zipfile.is_zipfile(fp) self.assertTrue(not chk) # - passing a file-like object fp = io.BytesIO() fp.write(b"this is not a legal zip file\n") chk = zipfile.is_zipfile(fp) self.assertTrue(not chk) fp.seek(0, 0) chk = zipfile.is_zipfile(fp) self.assertTrue(not chk)
def test_is_zip_valid_file(self): """Check that is_zipfile() correctly identifies zip files.""" # - passing a filename with zipfile.ZipFile(TESTFN, mode="w") as zipf: zipf.writestr("foo.txt", b"O, for a Muse of Fire!") chk = zipfile.is_zipfile(TESTFN) self.assertTrue(chk) # - passing a file object with open(TESTFN, "rb") as fp: chk = zipfile.is_zipfile(fp) self.assertTrue(chk) fp.seek(0, 0) zip_contents = fp.read() # - passing a file-like object fp = io.BytesIO() fp.write(zip_contents) chk = zipfile.is_zipfile(fp) self.assertTrue(chk) fp.seek(0, 0) chk = zipfile.is_zipfile(fp) self.assertTrue(chk)
def get_archive_handler(archive_name): if os.path.isfile(archive_name): if tarfile.is_tarfile(archive_name): return extract_tarfile elif zipfile.is_zipfile(archive_name): return extract_zipfile else: # look at the file name for ext in ('.tar', '.tar.gz', '.tgz', 'tar.bz2', '.tbz2', '.tbz'): if archive_name.endswith(ext): return extract_tarfile for ext in ('.zip', '.jar'): if archive_name.endswith(ext): return extract_zipfile
def _unpack_zipfile(filename, extract_dir): """Unpack zip `filename` to `extract_dir` """ try: import zipfile except ImportError: raise ReadError('zlib not supported, cannot unpack this archive.') if not zipfile.is_zipfile(filename): raise ReadError("%s is not a zip file" % filename) zip = zipfile.ZipFile(filename) try: for info in zip.infolist(): name = info.filename # don't extract absolute paths or ones with .. in them if name.startswith('/') or '..' in name: continue target = os.path.join(extract_dir, *name.split('/')) if not target: continue _ensure_directory(target) if not name.endswith('/'): # file data = zip.read(info.filename) f = open(target, 'wb') try: f.write(data) finally: f.close() del data finally: zip.close()
def unpack_file(filename, location, content_type, link): filename = os.path.realpath(filename) if (content_type == 'application/zip' or filename.lower().endswith(ZIP_EXTENSIONS) or zipfile.is_zipfile(filename)): unzip_file( filename, location, flatten=not filename.endswith('.whl') ) elif (content_type == 'application/x-gzip' or tarfile.is_tarfile(filename) or filename.lower().endswith( TAR_EXTENSIONS + BZ2_EXTENSIONS + XZ_EXTENSIONS)): untar_file(filename, location) elif (content_type and content_type.startswith('text/html') and is_svn_page(file_contents(filename))): # We don't really care about this from pip.vcs.subversion import Subversion Subversion('svn+' + link.url).unpack(location) else: # FIXME: handle? # FIXME: magic signatures? logger.critical( 'Cannot unpack file %s (downloaded from %s, content-type: %s); ' 'cannot detect archive format', filename, location, content_type, ) raise InstallationError( 'Cannot determine archive format of %s' % location )
def unpack_zipfile(filename, extract_dir, progress_filter=default_filter): """Unpack zip `filename` to `extract_dir` Raises ``UnrecognizedFormat`` if `filename` is not a zipfile (as determined by ``zipfile.is_zipfile()``). See ``unpack_archive()`` for an explanation of the `progress_filter` argument. """ if not zipfile.is_zipfile(filename): raise UnrecognizedFormat("%s is not a zip file" % (filename,)) with ContextualZipFile(filename) as z: for info in z.infolist(): name = info.filename # don't extract absolute paths or ones with .. in them if name.startswith('/') or '..' in name.split('/'): continue target = os.path.join(extract_dir, *name.split('/')) target = progress_filter(name, target) if not target: continue if name.endswith('/'): # directory ensure_directory(target) else: # file ensure_directory(target) data = z.read(info.filename) with open(target, 'wb') as f: f.write(data) unix_attributes = info.external_attr >> 16 if unix_attributes: os.chmod(target, unix_attributes)
def _isJarfile(self, filename): return filename.endswith('.jar') and zipfile.is_zipfile(filename)
def unpack_file(filename, location, content_type, link): filename = os.path.realpath(filename) if (content_type == 'application/zip' or filename.lower().endswith(ZIP_EXTENSIONS) or zipfile.is_zipfile(filename)): unzip_file( filename, location, flatten=not filename.endswith('.whl') ) elif (content_type == 'application/x-gzip' or tarfile.is_tarfile(filename) or filename.lower().endswith( TAR_EXTENSIONS + BZ2_EXTENSIONS + XZ_EXTENSIONS)): untar_file(filename, location) elif (content_type and content_type.startswith('text/html') and is_svn_page(file_contents(filename))): # We don't really care about this from pip._internal.vcs.subversion import Subversion Subversion('svn+' + link.url).unpack(location) else: # FIXME: handle? # FIXME: magic signatures? logger.critical( 'Cannot unpack file %s (downloaded from %s, content-type: %s); ' 'cannot detect archive format', filename, location, content_type, ) raise InstallationError( 'Cannot determine archive format of %s' % location )
def download_video(pathname, scene, image_md5_dict, calc_md5): scene_out_dir = pathname + 'videos' download_file = scene + '.mp4' print('\ndownloading video ' + download_file.split('/')[-1]) idd = id_download_dict[download_file] download_file_local = scene_out_dir + sep + scene + '.mp4' download_file_from_google_drive(idd, download_file_local) if(calc_md5): h_md5 = generate_file_md5(download_file_local) print('\nmd5 downloaded: ' + h_md5) print('md5 original: ' + video_md5_dict[scene]) md5_check = h_md5 == video_md5_dict[scene] if(not md5_check): print('\nWarning: MD5 does not match, delete file and restart' + ' download\n') else: if(unpack): extr_dir = scene_out_dir zip_file = scene_out_dir + sep + scene + '.zip' if(zipfile.is_zipfile(zip_file)): if not os.path.exists(extr_dir): os.makedirs(extr_dir) zip = zipfile.ZipFile(zip_file,'r') zip.extractall(extr_dir)
def download_image_sets(pathname, scene, image_md5_dict, calc_md5): scene_out_dir = pathname + 'image_sets' download_file = scene + '.zip' download_file_local = scene_out_dir + sep + scene + '.zip' print('\ndownloading image set ' + download_file.split('/')[-1]) idd = id_download_dict[download_file] download_file_from_google_drive(idd, download_file_local) if(calc_md5): h_md5 = generate_file_md5(download_file_local) print('\nmd5 downloaded: ' + h_md5) print('md5 original: ' + image_md5_dict[scene]) md5_check = h_md5 == image_md5_dict[scene] if(md5_check): if(unpack): extr_dir = scene_out_dir zip_file = scene_out_dir + sep + scene + '.zip' if(zipfile.is_zipfile(zip_file)): if not os.path.exists(extr_dir): os.makedirs(extr_dir) zip = zipfile.ZipFile(zip_file,'r') zip.extractall(extr_dir) else: print('\nWarning: MD5 does not match, delete file and restart' + ' download\n')