def each(self, target): tmpdir = tempdir() rf = RarFile(target) namelist = rf.namelist() for name in namelist: try: rf.extract(name, tmpdir) filepath = os.path.join( tmpdir, name, ) if os.path.isfile(filepath): self.add_extracted_file(filepath) except RuntimeError: for password in ['virus', 'infected']: try: filepath = rf.extract(name, tmpdir, pwd=password) if os.path.isfile(filepath): self.add_extracted_file(filepath) break except RuntimeError: pass else: self.log('error', 'Could not extract {}'.format(name)) return True
def extract_rar(j): """ Extract a rar archive and update the job so that the extracted file is organized instead of the archive. :param dict j: organization job :return: extraction success :rtype: bool """ rf = RarFile(j['s']) media = None for f in rf.infolist(): _, ext = os.path.splitext(f.filename) if ext in MEDIA_EXTS: media = f break if not media: return False d, _ = os.path.split(j['s']) extract_path = os.path.join(d, media.filename) LOG.info(f"extract {j['s']} -> {extract_path}") rf.extract(rf.infolist()[0], path=extract_path) j['s'] = extract_path target_dir, _ = os.path.split(j['t']) j['t'] = get_target_path(extract_path, target_dir) return True
def unrar(self, file_name): zf = RarFile(join(self.open_path, file_name), 'r') to_extract = zf.namelist()[0] zf.extract( to_extract, path=self.open_path, ) return to_extract
def unRarFull(fias_object): """Распаковка из полной базы ФИАС""" rf = RarFile(fiases.fias_data.WORK_DIR + fiases.fias_data.FIAS_XML_RAR) objectMatcher = re.compile(fias_object.FILE) print('') for f in rf.infolist(): if objectMatcher.match(f.filename): fias_object.xml_file = f.filename fias_object.xml_file_size = f.file_size if (fias_object.xml_file_size > 0): rf.extract(fias_object.xml_file, fiases.fias_data.WORK_DIR)
def unrarUpdate(fias_object): """Распаковка обновления """ rf = RarFile(fiases.fias_data.WORK_DIR + fiases.fias_data.FIAS_DELTA_XML_RAR) fias_objectMatcher = re.compile(fias_object.FILE) for f in rf.infolist(): if fias_objectMatcher.match(f.filename): fias_object.xml_delta_file = f.filename fias_object.xml_delta_file_size = f.file_size if (fias_object.xml_delta_file_size > 0): rf.extract(fias_object.xml_delta_file, fiases.fias_data.WORK_DIR)
def __extract_cbr(self, file): """ extract first image in cbr file, cache it in local cache folder""" try: archive_rar = RarFile(file, 'r') extract_path = self.cache + path.basename(file) if self.mode is 'all': archive_rar.extractall(path=extract_path) else: first_file = archive_rar.namelist()[0] archive_rar.extract(member=first_file, path=extract_path) except BadRarFile as e: raise e finally: archive_rar.close() return extract_path
def run(self): for idx, file in enumerate(self.input()): ext = os.path.splitext(file.path)[-1] if ext == '.rar': arch = RarFile(file.path) elif ext == '.zip': arch = zipfile.ZipFile(file.path) f = arch.namelist()[0] target = os.path.join(os.getenv('TEMP_DIR'), f) arch.extract(f, os.path.join(os.getenv('TEMP_DIR'))) copyfile( target, os.path.join(os.getenv('TEMP_DIR'), os.path.basename(self.output()[idx].path)))
def extract_rar(complete_folder, rar_file, rar_name, data_name): """ Extract rar.gz file and get the data. """ rar_file_path = os.path.join(complete_folder, rar_file) rf = RarFile(rar_file_path) rar_inside = rf.namelist() for t_i in rar_inside: if rar_name in t_i: rf.extract(t_i, path=complete_folder) shutil.move(os.path.join(complete_folder, t_i), os.path.join(complete_folder, data_name)) break try: shutil.rmtree(os.path.join(complete_folder, t_i.split('/')[0])) except: pass
def run(self): ext = os.path.splitext(self.input().path)[-1] if ext == '.rar': arch = RarFile(self.input().path) elif ext == '.zip': arch = zipfile.ZipFile(self.input().path) for f in arch.namelist(): if os.path.basename(f) in [ os.path.basename(out.path) for out in self.output() ]: target = os.path.join(os.getenv('TEMP_DIR'), f) arch.extract(f, os.path.join(os.getenv('TEMP_DIR'))) copyfile( target, os.path.join(os.getenv('TEMP_DIR'), os.path.basename(f)))
def extract(entry, name): rfile = RarFile(localsettings.BASEDIR + name) start = datetime.now().replace(microsecond=0) try: extdir = localsettings.OUTDIR if entry[-4:] == ".rar": extdir = localsettings.BASEDIR + RARTEMP rfile.extract(entry, path=extdir) print name, "extracted" except: status = 'failed' print name, "failed extract" response = jsonify(status=status) response.status_code = 400 return response time = str(datetime.now().replace(microsecond=0) - start) return jsonify(time=time, file=entry, status='success')
def extract_by_wildcard(arch_fpath: str, directory: str = None, wildcard: str = '*.xlsx', names=None): """ Extract files from archive. Supports only zip and rar formats. """ frmt = identify_file_format(arch_fpath) # detect archive format if not frmt: raise ExternalSourceError("Not supported format") else: if frmt == 'rar': arch_obj = RarFile(arch_fpath) else: arch_obj = ZipFile(arch_fpath) if directory: _dir = directory else: _dir = os.path.abspath(os.path.dirname(arch_fpath)) # filter by wildcard _flist = fnmatch.filter(arch_obj.namelist(), wildcard) if names: _flist = _flist[:len(names)] extracted_files_list = [] # extracting for i, f in enumerate(_flist): _fname = os.path.basename(f) for _f in arch_obj.namelist(): if os.path.basename(_f) == _fname: arch_obj.extract(_f, _dir) src = os.path.join(_dir, _f).replace('/', os.sep) dest = os.path.join(_dir, _fname) if names: dest = os.path.join(_dir, names[i]) if _fname: move(src, dest) extracted_files_list.append(dest) return extracted_files_list
class ArchivedFile(object): def __init__(self,filename): if is_zipfile(filename): self._zfile = ZipFile(filename) elif is_rarfile(filename): self._zfile = RarFile(filename) def filelist(self): return self._zfile.namelist() def extract(self,srcfile,targetpath): "extract srcfile in archive to targetpath" for fullfilename in self.filelist(): if srcfile in fullfilename: fpath,fname = os.path.split(fullfilename) self._zfile.extract(fname.encode('gbk'),targetpath+fname) return True return None def extractall(self,targetpath): self._zfile.extractall(targetpath)
from rarfile import RarFile path = '/Users/username/Downloads/' filename = 'py_20200326_174202.rar' filepath = 'temp/' rf = RarFile(path + filename, mode='r') # mode的值只能为'r' rf_list = rf.namelist() # 得到压缩包里所有的文件 print('rar文件内容', rf_list) for f in rf_list: rf.extract(f, path + filepath) # 循环解压,将文件解压到指定路径 # 一次性解压所有文件到指定目录 # rf.extractall(path) # 不传path,默认为当前目录