def download_demo(demo, event_folder): print('Downloading %s' % demo['url']) with requests.get(demo['url'], allow_redirects=not DEBUG, headers={'User-Agent': 'joder'}, stream=True) as r: r.raise_for_status() if DEBUG: print(r.headers['Location']) else: local_filename = os.path.join(event_folder, 'rars', r.url.split('/')[-1]) with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): if chunk: f.write(chunk) print('Downloaded %s' % local_filename) rar = RarFile(local_filename) rar.extractall(event_folder) for filename in rar.namelist(): old = os.path.join(event_folder, filename) new = os.path.join(event_folder, str(demo['date']) + '#' + filename) os.rename(old, new)
def convert_to_cbz(self, comic_file, comic_file_name): ### This function only converts comic's archive to cbz without deleting files or folders ### I do not know what will happen if there would be two folders inside archive. Maybe one day I'll find out. cbz_comic_archive = ZipFile(self.comic_save_location + comic_file_name + ".cbz", mode="w", compression=ZIP_STORED, allowZip64=True) # cbz_comic_archive = ZipFile(comic_save_location + comic_file_name + ".cbz", mode="w", compression=ZIP_STORED, allowZip64=True, compresslevel=None, strict_timestamps=True) # Commented out, because on python3.6 compresslevel and strict_timestamps are not supported with TemporaryDirectory() as dir: ## Opens temporary directory named dir, that will be deleted when everything inside with statement is finished comic = RarFile(comic_file, mode="r") # Opening rar comic archive in read mode comic.extractall( path=dir) # Extracting every file to temporary directory base = "" # To save folder if one exists. for folder in walk(dir): ## Looping thought all folders/files if folder[1] != []: base = folder[1][0] else: for page in folder[2]: # Adding every file in temporary dir to the archive cbz_comic_archive.write(join(folder[0], page), arcname=join(base + sep + page))
def extract(rfile: RarFile, pwd: str) -> bool: # extract函数返回的是bool类型 try: rfile.extractall(path='.', pwd=pwd.encode('utf-8')) # 密码输入错误的时候会报错 now = time.time() # 故使用 try - except 语句 print(f"Password is: {pwd}") # 将正确的密码输出到控制台 return True except: return False
def unrar_files(path_zipped_file, tmp_dir=TEMP_DIR): """ Uncompress zipped file (rar extension) by saving in a temporary file. Args: path_zipped_file ([str]): respective file path in a rar format. tmp_dir ([str], optional): temporary file path. """ rar = RarFile(path_zipped_file) temp = tmp_dir rar.extractall(path=temp)
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 unarchive(self, fname): ftype = self.ft.get_type(fname) if ftype == "rar": ext = RarFile(fname) elif ftype == "tar": ext = tarfile.open(fname) elif ftype == "zip": ext = ZipFile(fname) new_path = fname[:fname.rfind(".")] + "_extracted" if not os.path.exists(new_path): os.makedirs(new_path) ext.extractall(path=new_path) return new_path
def unarchive(fname): ftype = ft.get_type(fname) if ftype == "rar": ext = RarFile(fname) elif ftype == "tar": ext = tarfile.open(fname) elif ftype == "zip": ext = ZipFile(fname) new_path = get_new_name(fname[:fname.rfind(".")] + "_extracted") if not os.path.exists(new_path): os.makedirs(new_path) ext.extractall(path=new_path) return new_path
class RarArchive(BaseArchive): def __init__(self, file): self._archive = RarFile(file) def namelist(self): return self._archive.namelist() def filenames(self): return self._archive.namelist() def close(self): return self._archive.close() def is_encrypted(self): return self._archive.needs_password() def extraxt(self): rarfile = rarfile.RarFile.self._archive rarfile.extraxt(self._archive._rarfile) def extractall(self, file): return self._archive.extractall(file) def list(self, *args, **kwargs): self.printdir(*args, **kwargs) def printdir(self, file=None): """Print a table of contents for the zip file.""" print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"), file=file) for file_ in self._archive._file_parser.infolist(): date = "%d-%02d-%02d %02d:%02d:%02d" % file_.date_time[:6] print("%-46s %s %12d" % (file_.filename, date, file_.file_size), file=file)
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)
def __unzip_file(self, zipped_fp): if zipped_fp.endswith('.zip'): tool.write_to_log("Unzipping '%s' file" % basename(zipped_fp)) with ZipFile(zipped_fp, 'r') as zip_ref: zip_ref.extractall(self.__upload_dest_dir) elif zipped_fp.endswith('.rar'): tool.write_to_log("Unrarring '%s' file" % basename(zipped_fp)) rar = RarFile(zipped_fp) rar.extractall(self.__upload_dest_dir) elif zipped_fp.endswith('.7z'): tool.write_to_log("Un7zipping '%s' file" % basename(zipped_fp)) z = Popen('7z e "{0}" -o"{1}"'.format(zipped_fp, self.__upload_dest_dir), shell=True) z.wait() z.kill() else: tool.write_to_log("Migrating '%s' file" % basename(zipped_fp)) rename(zipped_fp, join(self.__upload_dest_dir, basename(zipped_fp)))
def extract_rar(self,tarfile_fullpath, delete_tar_file=DELETE,Path=True): tarfile_name = os.path.basename(tarfile_fullpath) parent_dir = os.path.dirname(tarfile_fullpath) extract_folder_name = tarfile_name[:-1*len(self.fileExtension(tarfile_name))-1] if Path: if self.extract_path: extract_folder_fullpath = os.path.join(self.extract_path, extract_folder_name) else: extract_folder_fullpath = os.path.join(parent_dir,"archivedFiles", extract_folder_name) else: extract_folder_fullpath = os.path.join(parent_dir, extract_folder_name) try: rar = RarFile(tarfile_fullpath, "r") rar.extractall(extract_folder_fullpath) rar.close() if delete_tar_file: os.remove(tarfile_fullpath) return extract_folder_fullpath except Exception as e: # Exceptions can occur while opening a damaged tar file. print 'Error occured while extracting %s\n'\ 'Reason: %s' %(tarfile_fullpath, e) return
def unrar(self, path, rar_files, force=False): """ Extract RAR files. :param path: Path to look for files in :param rar_files: Names of RAR files :param force: process currently processing items :return: List of unpacked file names """ unpacked_files = [] if app.UNPACK and rar_files: self.log_and_output('Packed files detected: {rar_files}', level=logging.DEBUG, **{'rar_files': rar_files}) for archive in rar_files: self.log_and_output('Unpacking archive: {archive}', level=logging.DEBUG, **{'archive': archive}) failure = None try: rar_handle = RarFile(os.path.join(path, archive)) # check that the rar doesnt need a password if rar_handle.needs_password(): raise ValueError('Rar requires a password') # Skip extraction if any file in archive has previously been extracted skip_extraction = False for file_in_archive in [ os.path.basename(each.filename) for each in rar_handle.infolist() if not each.isdir() ]: if not force and self.already_postprocessed( file_in_archive): self.log_and_output( 'Archive file already post-processed, extraction skipped: {file_in_archive}', level=logging.DEBUG, **{'file_in_archive': file_in_archive}) skip_extraction = True break if app.POSTPONE_IF_NO_SUBS and os.path.isfile( os.path.join(path, file_in_archive)): self.log_and_output( 'Archive file already extracted, extraction skipped: {file_in_archive}', level=logging.DEBUG, **{'file_in_archive': file_in_archive}) skip_extraction = True break if not skip_extraction: # raise an exception if the rar file is broken rar_handle.testrar() rar_handle.extractall(path=path) for each in rar_handle.infolist(): if not each.isdir(): basename = os.path.basename(each.filename) unpacked_files.append(basename) del rar_handle except (BadRarFile, Error, NotRarFile, RarCannotExec, ValueError) as error: failure = (ex(error), 'Unpacking failed with a Rar error') except Exception as error: failure = (ex(error), 'Unpacking failed for an unknown reason') if failure is not None: self.log_and_output( 'Failed unpacking archive {archive}: {failure}', level=logging.WARNING, **{ 'archive': archive, 'failure': failure[0] }) self.missed_files.append( '{0}: Unpacking failed: {1}'.format( archive, failure[1])) self.result = False continue self.log_and_output('Extracted content: {unpacked_files}', level=logging.DEBUG, **{'unpacked_files': unpacked_files}) return unpacked_files
def unrar(path, rar_files, force, result): # pylint: disable=too-many-branches,too-many-statements """ Extracts RAR files :param path: Path to look for files in :param rar_files: Names of RAR files :param force: process currently processing items :param result: Previous results :return: List of unpacked file names """ unpacked_dirs = [] if sickbeard.UNPACK == 1 and rar_files: result.output += log_helper("Packed Releases detected: {0}".format(rar_files), logger.DEBUG) for archive in rar_files: failure = None rar_handle = None try: archive_path = ek(os.path.join, path, archive) if already_processed(path, archive, force, result): result.output += log_helper( "Archive file already post-processed, extraction skipped: {0}".format (archive_path), logger.DEBUG) continue if not helpers.is_rar_file(archive_path): continue result.output += log_helper("Checking if archive is valid and contains a video: {0}".format(archive_path), logger.DEBUG) rar_handle = RarFile(archive_path) if rar_handle.needs_password(): # TODO: Add support in settings for a list of passwords to try here with rar_handle.set_password(x) result.output += log_helper('Archive needs a password, skipping: {0}'.format(archive_path)) continue # rar_handle.testrar() # If there are no video files in the rar, don't extract it rar_media_files = filter(helpers.is_media_file, rar_handle.namelist()) if not rar_media_files: continue rar_release_name = archive.rpartition('.')[0] # Choose the directory we'll unpack to: if sickbeard.UNPACK_DIR and os.path.isdir(sickbeard.UNPACK_DIR): # verify the unpack dir exists unpack_base_dir = sickbeard.UNPACK_DIR else: unpack_base_dir = path if sickbeard.UNPACK_DIR: # Let user know if we can't unpack there result.output += log_helper('Unpack directory cannot be verified. Using {0}'.format(path), logger.DEBUG) # Fix up the list for checking if already processed rar_media_files = [os.path.join(unpack_base_dir, rar_release_name, rar_media_file) for rar_media_file in rar_media_files] skip_rar = False for rar_media_file in rar_media_files: check_path, check_file = os.path.split(rar_media_file) if already_processed(check_path, check_file, force, result): result.output += log_helper( "Archive file already post-processed, extraction skipped: {0}".format (rar_media_file), logger.DEBUG) skip_rar = True break if skip_rar: continue rar_extract_path = ek(os.path.join, unpack_base_dir, rar_release_name) result.output += log_helper("Unpacking archive: {0}".format(archive), logger.DEBUG) rar_handle.extractall(path=rar_extract_path) unpacked_dirs.append(rar_extract_path) except RarCRCError: failure = ('Archive Broken', 'Unpacking failed because of a CRC error') except RarWrongPassword: failure = ('Incorrect RAR Password', 'Unpacking failed because of an Incorrect Rar Password') except PasswordRequired: failure = ('Rar is password protected', 'Unpacking failed because it needs a password') except RarOpenError: failure = ('Rar Open Error, check the parent folder and destination file permissions.', 'Unpacking failed with a File Open Error (file permissions?)') except RarExecError: failure = ('Invalid Rar Archive Usage', 'Unpacking Failed with Invalid Rar Archive Usage. Is unrar installed and on the system PATH?') except BadRarFile: failure = ('Invalid Rar Archive', 'Unpacking Failed with an Invalid Rar Archive Error') except NeedFirstVolume: continue except (Exception, Error) as e: failure = (ex(e), 'Unpacking failed') finally: if rar_handle: del rar_handle if failure: result.output += log_helper('Failed to extract the archive {0}: {1}'.format(archive, failure[0]), logger.WARNING) result.missed_files.append('{0} : Unpacking failed: {1}'.format(archive, failure[1])) result.result = False continue return unpacked_dirs
log.error("Error on Find with: %s" % ret) # str_err = "Error on Find command, rerun" + torrent_name # push = pb.push_note("[Tor Complete]",str_err) sys.exit(-2) ret = ret.rstrip() log.debug("Find Returned : %s" % ret) # cmd = 'mkdir "'+STAGING_PATH+path+torrent_id+'/" && cd "'+STAGING_PATH+path+torrent_id+'/" && easy_extract --force '+root # log.debug('Shelling out : %s' % cmd) # ret = subprocess.call(cmd, shell=True) # THIS IS THE RARFILE method rar_file = ret out_path = STAGING_PATH + path + torrent_id + "/" log.debug("file=%s dest=%s" % (rar_file, out_path)) rf = RarFile(rar_file) try: rf.extractall(path=out_path) unrar_success = True except: e = sys.exc_info()[0] log.error("Error on Extraction with %s" % e) # str_err = "Error on Extraction, error: " + e # push = pb.push_note("[Tor Complete]",str_err) sys.exit(-2) # with RarFile(rar_file,path=out_path) as rf: # rf.extractall() # cmd='/home/bsmith/src/unrar/unrar x -o+ "'+ret+'" "'+STAGING_PATH+path+torrent_id+'/"' # log.debug('Shelling out : %s' % cmd) # ret = subprocess.call(cmd, shell=True) # if ret != 0: # log.warning('Unrar command returned non-zero value %d.' % ret) # sys.exit(-1)
def downloader(code, key): newDir() error = exist = 0 isError = isExist = '' nameList = listdir('PPT') extensions = [] print('读取成功!开始下载') pools = Semaphore(4) threads = [] def down(e): with pools: downFilename = key + code[e] if judgeFile(downFilename, nameList) == -1: return 0 try: downloadUrl = processor(code[e]) extensions.append(downloadUrl[len(downloadUrl) - 4:len(downloadUrl)]) with open('temp\\{}.temp'.format(downFilename), 'wb') as f: f.write(_get(downloadUrl, verify=False).content) except: pass for each in range(len(code)): threads.append(Thread(target=down, args=(each, ))) for each in threads: each.start() for each in range(len(threads)): showBar(each + 1, len(code) + 1) threads[each].join() print('\n下载完毕!正在解压') for each in range(len(code)): showBar(each + 1, len(code) + 1) filename = key + code[each] judgeCode = judgeFile(filename, nameList, False) if judgeCode == -1: exist = exist + 1 continue extension = extensions[each] try: move('temp\\{}.temp'.format(filename), 'PPT\\{}{}'.format(filename, extension)) if extension == '.rar': package = RarFile('PPT\\{}.rar'.format(filename)) elif extension == '.zip': package = ZipFile('PPT\\{}.zip'.format(filename)) else: print('\r{}{}解压失败,格式不支持。{}'.format(filename, extension, ' ' * 5)) error = error + 1 continue name = package.namelist()[0] package.extractall('PPT') package.close() try: rename('PPT\\{}'.format(name), 'PPT\\{}{}'.format(filename, name)) except: pass except: print('\r{}{}解压失败,找不到文件。{}'.format(filename, extension, ' ' * 5)) error = error + 1 print('\n下载成功!处理文件中。。。') for each in listdir('PPT'): if path.splitext(each)[1] != '.ppt' and path.splitext( each)[1] != '.pptx' and not path.isdir('PPT\\' + each): remove('PPT\\' + each) if error > 0: isError = ',{}个错误'.format(error) if exist > 0: isExist = ',{}个文件已存在。'.format(exist) print('PPT下载成功,共{}个{}{}'.format(len(code), isError, isExist)) while True: pass
def unrar(path, rar_files, force, result): # pylint: disable=too-many-branches,too-many-statements """ Extracts RAR files :param path: Path to look for files in :param rar_files: Names of RAR files :param force: process currently processing items :param result: Previous results :return: List of unpacked file names """ unpacked_dirs = [] if sickbeard.UNPACK == 1 and rar_files: result.output += log_helper("Packed Releases detected: {0}".format(rar_files), logger.DEBUG) for archive in rar_files: failure = None rar_handle = None try: archive_path = ek(os.path.join, path, archive) if already_processed(path, archive, force, result): result.output += log_helper( "Archive file already post-processed, extraction skipped: {0}".format (archive_path), logger.DEBUG) continue if not helpers.is_rar_file(archive_path): continue result.output += log_helper("Checking if archive is valid and contains a video: {0}".format(archive_path), logger.DEBUG) rar_handle = RarFile(archive_path) if rar_handle.needs_password(): # TODO: Add support in settings for a list of passwords to try here with rar_handle.set_password(x) result.output += log_helper('Archive needs a password, skipping: {0}'.format(archive_path)) continue # rar_handle.testrar() # If there are no video files in the rar, don't extract it rar_media_files = filter(helpers.is_media_file, rar_handle.namelist()) if not rar_media_files: continue rar_release_name = archive.rpartition('.')[0] # Choose the directory we'll unpack to: if sickbeard.UNPACK_DIR and os.path.isdir(sickbeard.UNPACK_DIR): # verify the unpack dir exists unpack_base_dir = sickbeard.UNPACK_DIR else: unpack_base_dir = path if sickbeard.UNPACK_DIR: # Let user know if we can't unpack there result.output += log_helper('Unpack directory cannot be verified. Using {0}'.format(path), logger.DEBUG) # Fix up the list for checking if already processed rar_media_files = [os.path.join(unpack_base_dir, rar_release_name, rar_media_file) for rar_media_file in rar_media_files] skip_rar = False for rar_media_file in rar_media_files: check_path, check_file = os.path.split(rar_media_file) if already_processed(check_path, check_file, force, result): result.output += log_helper( "Archive file already post-processed, extraction skipped: {0}".format (rar_media_file), logger.DEBUG) skip_rar = True break if skip_rar: continue rar_extract_path = ek(os.path.join, unpack_base_dir, rar_release_name) result.output += log_helper("Unpacking archive: {0}".format(archive), logger.DEBUG) rar_handle.extractall(path=rar_extract_path) unpacked_dirs.append(rar_extract_path) except RarCRCError: failure = ('Archive Broken', 'Unpacking failed because of a CRC error') except RarWrongPassword: failure = ('Incorrect RAR Password', 'Unpacking failed because of an Incorrect Rar Password') except PasswordRequired: failure = ('Rar is password protected', 'Unpacking failed because it needs a password') except RarOpenError: failure = ('Rar Open Error, check the parent folder and destination file permissions.', 'Unpacking failed with a File Open Error (file permissions?)') except RarExecError: failure = ('Invalid Rar Archive Usage', 'Unpacking Failed with Invalid Rar Archive Usage. Is unrar installed and on the system PATH?') except BadRarFile: failure = ('Invalid Rar Archive', 'Unpacking Failed with an Invalid Rar Archive Error') except NeedFirstVolume: continue except (Exception, Error) as e: failure = (ex(e), 'Unpacking failed') finally: if rar_handle: del rar_handle if failure: result.output += log_helper('Failed to extract the archive {0}: {1}'.format(archive, failure[0]), logger.WARNING) result.missed_files.append('{0} : Unpacking failed: {1}'.format(archive, failure[1])) result.result = False continue return unpacked_dirs
def add(self, archive_password=None, audio_file=None, session=None, artist_name_fallback=None): cache_key = LibraryUpload.CACHE_KEY % (cherrypy.request.user.id, session) all_tracks = None if not cache.has(cache_key): raise cherrypy.HTTPError(status=409) else: all_tracks = cache.get(cache_key) if audio_file is not None and len(audio_file) == 0: audio_file = None if archive_password is not None and len(archive_password) == 0: archive_password = None content_disposition = cherrypy.request.headers.get('content-disposition') filename = content_disposition[content_disposition.index('filename=') + 9:] if filename.startswith('"') and filename.endswith('"'): filename = filename[1:-1] filename = unquote(filename) ext = os.path.splitext(filename)[1].lower()[1:] basename = os.path.splitext(filename)[0] cache_path = os.path.join(cherrypy.config['opmuse'].get('cache.path'), 'upload') if not os.path.exists(cache_path): os.mkdir(cache_path) tempdir = tempfile.mkdtemp(dir=cache_path) path = os.path.join(tempdir, filename) paths = [] rarfile.PATH_SEP = '/' messages = [] with open(path, 'wb') as fileobj: fileobj.write(cherrypy.request.rfile.read()) # this file is a regular file that belongs to an audio_file if audio_file is not None: track = None tries = 0 # try and sleep until we get the track.. this will almost always # be needed because of the async upload. while track is None and tries < 10: track = library_dao.get_track_by_filename(audio_file.encode('utf8')) tries += 1 if track is None: time.sleep(3) if track is None: messages.append(('warning', ("<strong>%s</strong>: Skipping <strong>%s</strong>, timeout trying to " + "find its track.") % (audio_file, filename))) else: track_structure = TrackStructureParser(track) track_path = track_structure.get_path(absolute=True) relative_track_path = track_structure.get_path(absolute=False).decode('utf8', 'replace') new_path = os.path.join(track_path, filename.encode('utf8')) if os.path.exists(new_path): messages.append(('warning', ("<strong>%s</strong>: Skipping <strong>%s</strong>, already exists " + "in <strong>%s</strong>.") % (audio_file, filename, relative_track_path))) else: shutil.move(path.encode('utf8'), new_path) messages.append(('info', ("<strong>%s</strong>: Uploaded <strong>%s</strong> to " + "<strong>%s</strong>.") % (audio_file, filename, relative_track_path))) elif ext == "zip": # set artist name fallback to zip's name so if it's missing artist tags # it's easily distinguishable and editable so it can be fixed after upload. artist_name_fallback = basename try: zip = ZipFile(path) if archive_password is not None: zip.setpassword(archive_password.encode()) zip.extractall(tempdir) os.remove(path) for name in zip.namelist(): namepath = os.path.join(tempdir, name) # ignore hidden files, e.g. OSX archive weirdness and such if name.startswith(".") or os.path.split(name)[0] == "__MACOSX": shutil.rmtree(namepath) continue paths.append(namepath.encode('utf8')) except Exception as error: messages.append(('danger', "<strong>%s</strong>: %s" % (os.path.basename(path), error))) elif ext == "rar": # look at corresponding ext == zip comment... artist_name_fallback = basename try: rar = RarFile(path) if archive_password is None and rar.needs_password(): messages.append(('danger', "<strong>%s</strong>: Needs password but none provided." % os.path.basename(path))) else: if archive_password is not None: rar.setpassword(archive_password) rar.extractall(tempdir) os.remove(path) for name in rar.namelist(): namepath = os.path.join(tempdir, name) if name.startswith("."): shutil.rmtree(namepath) continue paths.append(namepath.encode('utf8')) except Exception as error: messages.append(('danger', "<strong>%s</strong>: %s" % (os.path.basename(path), error))) # this is a plain audio file else: paths.append(path.encode('utf8')) for path in paths: # update modified time to now, we don't want the time from the zip # archive or whatever os.utime(path, None) if len(paths) > 0: tracks, add_files_messages = library_dao.add_files(paths, move=True, remove_dirs=False, artist_name_fallback=artist_name_fallback, user=cherrypy.request.user) messages += add_files_messages else: tracks = [] shutil.rmtree(tempdir) for track in tracks: all_tracks.append(track.id) if track.album is not None: remotes.update_album(track.album) if track.artist is not None: remotes.update_artist(track.artist) remotes.update_track(track) hierarchy = Library._produce_track_hierarchy(library_dao.get_tracks_by_ids(all_tracks)) return {'hierarchy': hierarchy, 'messages': messages}
file = open(filename, 'r') #open file for reading passwords = [line.rstrip('\n') for line in file ] # read line by line and strip newline characters starttime = time.time() for i in passwords: #zip_archive = ZipFile('archive1.zip') #try: #zip_archive.extractall(pwd=i) #except: #pass zip_archive = Zipfile('archive2.7z') try: zip_archive.extractall(pwd=i) except: pass rar_archive = RarFile("archive3.rar") try: rar_archive.extractall(pwd=i) except: pass print(time.time() - starttime) file.close()