Python rarfile.RarFile() Examples
The following are 30
code examples of rarfile.RarFile().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rarfile
, or try the search function
.
Example #1
Source File: data.py From UnFlow with MIT License | 7 votes |
def _download_and_extract(self, url, extract_to, ext='zip'): def _progress(count, block_size, total_size): if total_size > 0: print('\r>> Downloading %s %.1f%%' % (url, float(count * block_size) / float(total_size) * 100.0), end=' ') else: print('\r>> Downloading %s' % (url), end=' ') sys.stdout.flush() urlretrieve = FancyURLopener().retrieve local_zip_path = os.path.join(self.data_dir, 'tmp.' + ext) urlretrieve(url, local_zip_path, _progress) sys.stdout.write("\n>> Finished downloading. Unzipping...\n") if ext == 'zip': with zipfile.ZipFile(local_zip_path, "r") as zip_ref: zip_ref.extractall(extract_to) else: with rarfile.RarFile(local_zip_path, "r") as zip_ref: zip_ref.extractall(extract_to) sys.stdout.write(">> Finished unzipping.\n") os.remove(local_zip_path) self.clear_statistics()
Example #2
Source File: comicscan.py From gazee with GNU General Public License v3.0 | 7 votes |
def build_unpack_comic(self, comic_path): logging.info("%s unpack requested" % comic_path) for root, dirs, files in os.walk(os.path.join(gazee.TEMP_DIR, "build"), topdown=False): for f in files: os.chmod(os.path.join(root, f), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 os.remove(os.path.join(root, f)) for root, dirs, files in os.walk(os.path.join(gazee.TEMP_DIR, "build"), topdown=False): for d in dirs: os.chmod(os.path.join(root, d), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 os.rmdir(os.path.join(root, d)) if comic_path.endswith(".cbr"): opened_rar = rarfile.RarFile(comic_path) opened_rar.extractall(os.path.join(gazee.TEMP_DIR, "build")) elif comic_path.endswith(".cbz"): opened_zip = zipfile.ZipFile(comic_path) opened_zip.extractall(os.path.join(gazee.TEMP_DIR, "build")) return
Example #3
Source File: rarfile.py From arissploit with GNU General Public License v3.0 | 6 votes |
def read(self, fname, psw = None): """Return uncompressed data for archive entry. For longer files using :meth:`RarFile.open` may be better idea. Parameters: fname filename or RarInfo instance psw password to use for extracting. """ f = self.open(fname, 'r', psw) try: return f.read() finally: f.close()
Example #4
Source File: nekur.py From bazarr with GNU General Public License v3.0 | 6 votes |
def download_subtitle(self, subtitle): if isinstance(subtitle, NekurSubtitle): # download the subtitle r = self.session.get(subtitle.download_link, timeout=10) r.raise_for_status() # open the archive archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): archive = RarFile(archive_stream) elif is_zipfile(archive_stream): archive = ZipFile(archive_stream) else: subtitle.content = r.content if subtitle.is_valid(): return subtitle.content = None raise ProviderError('Unidentified archive type') subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
Example #5
Source File: supersubtitles.py From bazarr with GNU General Public License v3.0 | 6 votes |
def download_subtitle(self, subtitle): # download as a zip logger.info('Downloading subtitle %r', subtitle.subtitle_id) r = self.session.get(subtitle.page_link, timeout=10) r.raise_for_status() if ".rar" in subtitle.page_link: logger.debug('Archive identified as rar') archive_stream = io.BytesIO(r.content) archive = RarFile(archive_stream) subtitle.content = self.get_subtitle_from_archive(subtitle, archive) elif ".zip" in subtitle.page_link: logger.debug('Archive identified as zip') archive_stream = io.BytesIO(r.content) archive = ZipFile(archive_stream) subtitle.content = self.get_subtitle_from_archive(subtitle, archive) else: subtitle.content = fix_line_ending(r.content)
Example #6
Source File: subtitriid.py From bazarr with GNU General Public License v3.0 | 6 votes |
def download_subtitle(self, subtitle): if isinstance(subtitle, SubtitriIdSubtitle): # download the subtitle r = self.session.get(subtitle.download_link, timeout=10) r.raise_for_status() # open the archive archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): archive = RarFile(archive_stream) elif is_zipfile(archive_stream): archive = ZipFile(archive_stream) else: subtitle.content = r.content if subtitle.is_valid(): return subtitle.content = None raise ProviderError('Unidentified archive type') subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
Example #7
Source File: comicscan.py From gazee with GNU General Public License v3.0 | 6 votes |
def user_unpack_comic(self, comic_path, user): logging.info("%s unpack requested" % comic_path) for root, dirs, files in os.walk(os.path.join(gazee.TEMP_DIR, user), topdown=False): for f in files: os.chmod(os.path.join(root, f), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 os.remove(os.path.join(root, f)) for root, dirs, files in os.walk(os.path.join(gazee.TEMP_DIR, user), topdown=False): for d in dirs: os.chmod(os.path.join(root, d), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 os.rmdir(os.path.join(root, d)) if comic_path.endswith(".cbr"): opened_rar = rarfile.RarFile(comic_path) opened_rar.extractall(os.path.join(gazee.TEMP_DIR, user)) elif comic_path.endswith(".cbz"): opened_zip = zipfile.ZipFile(comic_path) opened_zip.extractall(os.path.join(gazee.TEMP_DIR, user)) return # This method will return a list of .jpg files in their numberical order to be fed into the reading view.
Example #8
Source File: pickup.py From cobra with MIT License | 6 votes |
def __decompress_rar(self): """extract a rar file.""" try: rar_file = rarfile.RarFile(self.filepath) # check if there is a filename directory self.__check_filename_dir() os.mkdir(os.path.join(self.package_path, self.dir_name)) rar_file.extractall(os.path.join(self.package_path, self.dir_name)) rar_file.close() except (BadRarFile, NotRarFile): logger.error('File is not a rar file or is bad rar file') exit() return True
Example #9
Source File: data.py From DF-Net with MIT License | 6 votes |
def _download_and_extract(self, url, extract_to, ext='zip'): def _progress(count, block_size, total_size): if total_size > 0: print('\r>> Downloading %s %.1f%%' % (url, float(count * block_size) / float(total_size) * 100.0), end=' ') else: print('\r>> Downloading %s' % (url), end=' ') sys.stdout.flush() urlretrieve = FancyURLopener().retrieve local_zip_path = os.path.join(self.data_dir, 'tmp.' + ext) urlretrieve(url, local_zip_path, _progress) sys.stdout.write("\n>> Finished downloading. Unzipping...\n") if ext == 'zip': with zipfile.ZipFile(local_zip_path, "r") as zip_ref: zip_ref.extractall(extract_to) else: with rarfile.RarFile(local_zip_path, "r") as zip_ref: zip_ref.extractall(extract_to) sys.stdout.write(">> Finished unzipping.\n") os.remove(local_zip_path) self.clear_statistics()
Example #10
Source File: rar.py From ingestors with MIT License | 6 votes |
def unpack(self, file_path, temp_dir): # FIXME: need to figure out how to unpack multi-part files. try: with rarfile.RarFile(file_path) as rf: names = rf.namelist() encoding = self.detect_list_encoding(names) log.debug('Detected filename encoding: %s', encoding) for name in names: try: fh = rf.open(name) self.extract_member(temp_dir, name, fh, encoding=encoding) except Exception as ex: # TODO: should this be a fatal error? log.debug("Failed to unpack [%r]: %s", name, ex) except rarfile.NeedFirstVolume: raise ProcessingException('Cannot load splitted RAR files') except rarfile.Error as err: raise ProcessingException('Invalid RAR file: %s' % err)
Example #11
Source File: hosszupuska.py From bazarr with GNU General Public License v3.0 | 6 votes |
def download_subtitle(self, subtitle): r = self.session.get(subtitle.page_link, timeout=10) r.raise_for_status() # open the archive archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): logger.debug('Archive identified as rar') archive = RarFile(archive_stream) elif is_zipfile(archive_stream): logger.debug('Archive identified as zip') archive = ZipFile(archive_stream) else: raise ProviderError('Unidentified archive type') subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
Example #12
Source File: download.py From deep500 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def unrar(local_file): try: import rarfile except (ImportError, ModuleNotFoundError) as ex: raise ImportError('Cannot use unrar without rarfile: %s' % str(ex)) path = os.path.dirname(os.path.abspath(local_file)) print('\nunzipping in path: {}'.format(path)) rar = rarfile.RarFile(local_file) dir = os.path.join(path, sorted(rar.namelist())[0]) namelist = set([f.rstrip('/') for f in glob.glob("{}/**".format(dir), recursive=True)]) rar_namelist = set([os.path.join(path, f) for f in rar.namelist()]) if rar_namelist == namelist: dirs = set([f.rstrip('/') for f in glob.glob("{}/**/".format(dir), recursive=True)]) files = list(namelist - dirs) else: files = [] for filename in rar.namelist(): if not is_file_in_dir(path, filename): rar.extract(filename, path) files.append(path + '/' + filename) print('done!') return files
Example #13
Source File: legendastv.py From bazarr with GNU General Public License v3.0 | 6 votes |
def download_archive(self, archive): """Download an archive's :attr:`~LegendasTVArchive.content`. :param archive: the archive to download :attr:`~LegendasTVArchive.content` of. :type archive: :class:`LegendasTVArchive` """ logger.info('Downloading archive %s', archive.id) r = self.session.get(self.server_url + 'downloadarquivo/{}'.format(archive.id)) raise_for_status(r) # open the archive archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): logger.debug('Identified rar archive') archive.content = RarFile(archive_stream) elif is_zipfile(archive_stream): logger.debug('Identified zip archive') archive.content = ZipFile(archive_stream) else: raise ValueError('Not a valid archive')
Example #14
Source File: betaseries.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_archive(content): # open the archive archive_stream = io.BytesIO(content) archive = None if rarfile.is_rarfile(archive_stream): logger.debug('Identified rar archive') archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): logger.debug('Identified zip archive') archive = zipfile.ZipFile(archive_stream) return archive
Example #15
Source File: rarfile.py From bazarr with GNU General Public License v3.0 | 5 votes |
def open(self, fname, mode='r', psw=None): """Returns file-like object (:class:`RarExtFile`) from where the data can be read. The object implements :class:`io.RawIOBase` interface, so it can be further wrapped with :class:`io.BufferedReader` and :class:`io.TextIOWrapper`. On older Python where io module is not available, it implements only .read(), .seek(), .tell() and .close() methods. The object is seekable, although the seeking is fast only on uncompressed files, on compressed files the seeking is implemented by reading ahead and/or restarting the decompression. Parameters: fname file name or RarInfo instance. mode must be 'r' psw password to use for extracting. """ if mode != 'r': raise NotImplementedError("RarFile.open() supports only mode=r") # entry lookup inf = self.getinfo(fname) if inf.isdir(): raise TypeError("Directory does not have any data: " + inf.filename) # check password if inf.needs_password(): psw = psw or self._password if psw is None: raise PasswordRequired("File %s requires password" % inf.filename) else: psw = None return self._file_parser.open(inf, psw)
Example #16
Source File: legendastv.py From bazarr with GNU General Public License v3.0 | 5 votes |
def __init__(self, id, name, pack, featured, link, downloads=0, rating=0, timestamp=None): #: Identifier self.id = id #: Name self.name = name #: Pack self.pack = pack #: Featured self.featured = featured #: Link self.link = link #: Download count self.downloads = downloads #: Rating (0-10) self.rating = rating #: Timestamp self.timestamp = timestamp #: Compressed content as :class:`rarfile.RarFile` or :class:`zipfile.ZipFile` self.content = None
Example #17
Source File: rarfile.py From bazarr with GNU General Public License v3.0 | 5 votes |
def read(self, fname, psw=None): """Return uncompressed data for archive entry. For longer files using :meth:`RarFile.open` may be better idea. Parameters: fname filename or RarInfo instance psw password to use for extracting. """ with self.open(fname, 'r', psw) as f: return f.read()
Example #18
Source File: compressed_file.py From subfinder with MIT License | 5 votes |
def __init__(self, file): self.file = file self._file = None _, ext = os.path.splitext(file) if ext == '.zip': import zipfile self._file = zipfile.ZipFile(self.file, 'r') elif ext == '.rar': import rarfile if sys.platform == 'win32': # if os system is windows,try to use built-in unrar rarfile.UNRAR_TOOL = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'unrar.exe') self._file = rarfile.RarFile(self.file, 'r') else: raise ValueError('CompressedFile doesnt support "{}"'.format(ext))
Example #19
Source File: subssabbz.py From bazarr with GNU General Public License v3.0 | 5 votes |
def download_archive_and_add_subtitle_files(self, link, language, video, fps, num_cds): logger.info('Downloading subtitle %r', link) request = self.session.get(link, headers={ 'Referer': 'http://subs.sab.bz/index.php?' }) request.raise_for_status() archive_stream = io.BytesIO(request.content) if is_rarfile(archive_stream): return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps, num_cds) elif is_zipfile(archive_stream): return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps, num_cds) else: logger.error('Ignore unsupported archive %r', request.headers) return []
Example #20
Source File: yavkanet.py From bazarr with GNU General Public License v3.0 | 5 votes |
def download_archive_and_add_subtitle_files(self, link, language, video, fps): logger.info('Downloading subtitle %r', link) request = self.session.get(link, headers={ 'Referer': 'http://yavka.net/subtitles.php' }) request.raise_for_status() archive_stream = io.BytesIO(request.content) if is_rarfile(archive_stream): return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps) elif is_zipfile(archive_stream): return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps) else: logger.error('Ignore unsupported archive %r', request.headers) return []
Example #21
Source File: subs4series.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_archive(content): # open the archive archive_stream = io.BytesIO(content) archive = None if rarfile.is_rarfile(archive_stream): logger.debug('Identified rar archive') archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): logger.debug('Identified zip archive') archive = zipfile.ZipFile(archive_stream) return archive
Example #22
Source File: greeksubtitles.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_archive(content): # open the archive archive_stream = io.BytesIO(content) archive = None if rarfile.is_rarfile(archive_stream): logger.debug('Identified rar archive') archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): logger.debug('Identified zip archive') archive = zipfile.ZipFile(archive_stream) return archive
Example #23
Source File: titlovi.py From bazarr with GNU General Public License v3.0 | 5 votes |
def download_subtitle(self, subtitle): r = self.session.get(subtitle.download_link, timeout=10) r.raise_for_status() # open the archive archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): logger.debug('Archive identified as rar') archive = RarFile(archive_stream) elif is_zipfile(archive_stream): logger.debug('Archive identified as zip') archive = ZipFile(archive_stream) else: subtitle.content = r.content if subtitle.is_valid(): return subtitle.content = None raise ProviderError('Unidentified archive type') subs_in_archive = archive.namelist() # if Serbian lat and cyr versions are packed together, try to find right version if len(subs_in_archive) > 1 and (subtitle.language == 'sr' or subtitle.language == 'sr-Cyrl'): self.get_subtitle_from_bundled_archive(subtitle, subs_in_archive, archive) else: # use default method for everything else subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
Example #24
Source File: subdivx.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_archive(self, content): # open the archive archive_stream = io.BytesIO(content) if rarfile.is_rarfile(archive_stream): logger.debug('Identified rar archive') archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): logger.debug('Identified zip archive') archive = zipfile.ZipFile(archive_stream) else: raise APIThrottled('Unsupported compressed format') return archive
Example #25
Source File: subsunacs.py From bazarr with GNU General Public License v3.0 | 5 votes |
def download_archive_and_add_subtitle_files(self, link, language, video, fps, num_cds): logger.info('Downloading subtitle %r', link) request = self.session.get(link, headers={ 'Referer': 'https://subsunacs.net/search.php' }) request.raise_for_status() archive_stream = io.BytesIO(request.content) if is_rarfile(archive_stream): return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps, num_cds) elif is_zipfile(archive_stream): return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps, num_cds) else: logger.error('Ignore unsupported archive %r', request.headers) return []
Example #26
Source File: legendasdivx.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_archive(self, content): # open the archive archive_stream = io.BytesIO(content) if rarfile.is_rarfile(archive_stream): logger.debug('Legendasdivx.pt :: Identified rar archive') archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): logger.debug('Legendasdivx.pt :: Identified zip archive') archive = zipfile.ZipFile(archive_stream) else: logger.error('Legendasdivx.pt :: Unsupported compressed format') return None return archive
Example #27
Source File: rar_cracker.py From arissploit with GNU General Public License v3.0 | 5 votes |
def run(self): if variables["file"][0] == "": printError("No rar file specified!") return ModuleError("No rar file specified!") file = variables["file"][0] if os.path.exists(file): if os.path.isdir(file): printError("Error: "+file+": is a directory!") return ModuleError("Error: "+file+": is a directory!") else: rf = rarfile.RarFile(file) else: printError("Input file: "+file+": does not exist!") return ModuleError("Input file: "+file+": does not exist!") for word in self.words: if self.pwdh.pwd != None: return elif self.pwdh.error != None: return elif self.pwdh.kill == True: return try: word = word.decode("utf-8").replace("\n", "") if word[0] == "#": continue #animline("trying password: "+word) rf.extractall(path=variables["exto"][0], pwd=word) self.pwdh.pwd = word return except rarfile.RarCRCError: pass except rarfile.RarUserBreak: self.pwdh.kill = True except rarfile.RarSignalExit: pass
Example #28
Source File: EXTRACT_RAR.py From fsf with Apache License 2.0 | 5 votes |
def get_rar_info(tmpfile, PARENT_BIN): file_num = 0 password_required = False rf = rarfile.RarFile(tmpfile) if rf.needs_password(): password_required = True for r in rf.infolist(): CHILD_BIN = OrderedDict([('Filename', r.filename), ('Last Modified', datetime(*r.date_time).strftime("%Y-%m-%d %H:%M:%S")), ('Comment', r.comment), ('CRC', hex(r.CRC)), ('Compressed Size', '%s bytes' % r.compress_size), ('Uncompressed Size', '%s bytes' % r.file_size), ('Compress Type', get_compression_method(r.compress_type)), ('Create System', get_system_mapping(r.host_os)), ('Password Required', password_required)]) if not password_required and r.file_size != 0: CHILD_BIN['Buffer'] = rf.read(r) PARENT_BIN['Object_%s' % file_num] = CHILD_BIN file_num += 1 rf.close() return PARENT_BIN
Example #29
Source File: prepare_data.py From athena with Apache License 2.0 | 5 votes |
def download_and_extract(directory, url): """Download and extract the given dataset. Args: directory: the directory where to extract the tarball. url: the url to download the data file. """ _, rar_filepath = tempfile.mkstemp(suffix=".rar") # get rar_path try: logging.info("Downloading %s to %s" % (url, rar_filepath)) def _progress(count, block_size, total_size): sys.stdout.write( "\r>> Downloading {} {:.1f}%".format( rar_filepath, 100.0 * count * block_size / total_size ) ) sys.stdout.flush() urllib.request.urlretrieve(url, rar_filepath, _progress) # show the progress of download statinfo = os.stat(rar_filepath) # run a stat logging.info( "Successfully downloaded %s, size(bytes): %d" % (url, statinfo.st_size) # size -->bytes ) rf = rarfile.RarFile(rar_filepath) # need to install unrar!!!! rf.extractall(directory) finally: GFILE.Remove(rar_filepath)
Example #30
Source File: rarfile.py From Lector with GNU General Public License v3.0 | 5 votes |
def __init__(self, rarfile, mode="r", charset=None, info_callback=None, crc_check=True, errors="stop"): """Open and parse a RAR archive. Parameters: rarfile archive file name mode only 'r' is supported. charset fallback charset to use, if filenames are not already Unicode-enabled. info_callback debug callback, gets to see all archive entries. crc_check set to False to disable CRC checks errors Either "stop" to quietly stop parsing on errors, or "strict" to raise errors. Default is "stop". """ self._rarfile = rarfile self._charset = charset or DEFAULT_CHARSET self._info_callback = info_callback self._crc_check = crc_check self._password = None self._file_parser = None if errors == "stop": self._strict = False elif errors == "strict": self._strict = True else: raise ValueError("Invalid value for 'errors' parameter.") if mode != "r": raise NotImplementedError("RarFile supports only mode=r") self._parse()