def test_detection(): assert rarfile.is_rarfile("test/files/ctime4.rar.exp") is False assert rarfile.is_rarfile("test/files/ctime4.rar") is True assert rarfile.is_rarfile("test/files/rar5-crc.rar") is True assert rarfile.is_rarfile(Path("test/files/rar5-crc.rar")) is True assert rarfile.is_rarfile("test/files/_missing_.rar") is False
def extractCbrImage(self): print rarfile.is_rarfile(self.filePath) rar = rarfile.RarFile(self.filePath) print rar.namelist() infoList = rar.infolist() for info in infoList: print info # print rar.printdir() if not os.path.exists("/tmp/1"): os.mkdir("/tmp/1") try: rar.extractall("/tmp/1") except: traceback.print_exc()
async def unzip_file(event): if event.fwd_from: return if not exists(TEMP_DOWNLOAD_DIRECTORY): os.makedirs(TEMP_DOWNLOAD_DIRECTORY) input_str = event.pattern_match.group(1) file_name = basename(input_str) output_path = TEMP_DOWNLOAD_DIRECTORY + re.split("(.zip|.rar)", file_name)[0] if exists(input_str): start_time = datetime.now() await event.edit("`Unzipping...`") if is_zipfile(input_str): zip_type = ZipFile elif is_rarfile(input_str): zip_type = RarFile else: return await event.edit( "`Unsupported file types!`\n`ZIP and RAR only`") try: with zip_type(input_str, "r") as zip_obj: zip_obj.extractall(output_path) except BaseException: return await event.edit(f"**Error:** `Corrupted Archive`") end_time = (datetime.now() - start_time).seconds await event.edit( f"Unzipped `{input_str}` into `{output_path}` in `{end_time}` seconds." ) else: await event.edit("`404: Not Found`")
def extract_all(*args): if len(args) > 0: dest = input('destination: ') for arg in args: if os.path.isdir(arg): print("'" + arg + "' is a directory") elif not os.path.exists(arg): print("'" + arg + "' not found") elif zipfile.is_zipfile(arg): zipf = zipfile.ZipFile(arg) if len(dest) == 0: zipf.extractall() else: zipf.extractall(dest) print("contents extracted from '" + arg + "'") elif tarfile.is_tarfile(arg): tar = tarfile.open(arg) if len(dest) == 0: tar.extractall() else: tar.extractall(dest) print("contents extracted from '" + arg + "'") elif rarfile.is_rarfile(arg): if len(dest) == 0: subprocess.check_output(['unar', arg]) else: subprocess.check_output(['unar', '-o', args[-1], arg]) print("contents extracted from '" + arg + "'") else: print("unable to extract '" + arg + "'") else: raise TypeError
def download_subtitle(self, subtitle): res = self.session.get(subtitle.download_link, headers={'Referer': subtitle.page_link}, timeout=self.timeout) try: res.raise_for_status() except: raise HTTPError( f"An error occured during the download request to {subtitle.download_link}" ) archive_stream = io.BytesIO(res.content) archive = None if rarfile.is_rarfile(archive_stream): logger.debug("Titulky.com: Identified rar archive") archive = rarfile.RarFile(archive_stream) subtitle_content = self.get_subtitle_from_archive( subtitle, archive) elif zipfile.is_zipfile(archive_stream): logger.debug("Titulky.com: Identified zip archive") archive = zipfile.ZipFile(archive_stream) subtitle_content = self.get_subtitle_from_archive( subtitle, archive) else: subtitle_content = fix_line_ending(res.content) if not subtitle_content: logger.debug( "Titulky.com: No subtitle content found. The downloading limit has been most likely exceeded." ) raise DownloadLimitExceeded( "Subtitles download limit has been exceeded") subtitle.content = subtitle_content
def save_file(url, filename): filename = "out/" + filename # Check if we already have it if os.path.isfile(filename + ".zip") or os.path.isfile(filename + ".rar") or os.path.isfile(filename + ".zip"): print "We already have " + filename + ", skipping" return print("Downloading... " + filename) try: f = open(filename, 'wb') f.write(urllib2.urlopen(url).read()) f.close() if zipfile.is_zipfile(filename): print(filename + " is a zip file") shutil.move(filename, filename + ".zip") elif rarfile.is_rarfile(filename): print(filename + " is a rar file") shutil.move(filename, filename + ".rar") else: print(filename + " is an nds file") shutil.move(filename, filename + ".nds") except urllib2.URLError: print "Failed to download: " + filename
def extract(f): if zipfile.is_zipfile(f): fz=zipfile.ZipFile(f,'r') for file in fz.namelist(): ''' img_type=imghdr.what(file) if img_type == 'jpeg': ''' fz.extract(file,uploadDir) fz.close() return True if tarfile.is_tarfile(f): fz=tarfile.open(f) for file in fz.getnames(): ''' img_type = imghdr.what(file) if img_type == 'jpeg': ''' fz.extract(file,uploadDir) fz.close() return True if rarfile.is_rarfile(f): fz=rarfile.RarFile(f) for file in fz.namelist(): ''' img_type = imghdr.what(file) if img_type == 'jpeg': ''' fz.extract(file,uploadDir) fz.close() return True return False
async def unzip_file(event): if event.fwd_from: return if not exists(TEMP_DOWNLOAD_DIRECTORY): os.makedirs(TEMP_DOWNLOAD_DIRECTORY) input_str = event.pattern_match.group(1) file_name = basename(input_str) output_path = TEMP_DOWNLOAD_DIRECTORY + re.split("(.zip|.rar)", file_name)[0] if exists(input_str): start_time = datetime.now() await event.edit("`Mengekstrak file...`") if is_zipfile(input_str): zip_type = ZipFile elif is_rarfile(input_str): zip_type = RarFile else: return await event.edit( "`Jenis file tidak didukung!`\n`Hanya file ZIP dan RAR`") try: with zip_type(input_str, "r") as zip_obj: zip_obj.extractall(output_path) except BadRarFile: return await event.edit("**Kesalahan :** `File RAR rusak.`") except BadZipFile: return await event.edit("**Kesalahan :** `File ZIP rusak.`") except BaseException as err: return await event.edit(f"**Kesalahan :** `{err}`") end_time = (datetime.now() - start_time).seconds await event.edit( f"`Mengekstrak` **{input_str}** `menjadi` **{output_path}** `dalam` **{end_time}** `detik.`" ) else: await event.edit("**404 :** tidak ditemukan.")
def give_me_paths(path): raw_d_dirs = set() for root, dirs, files in os.walk(path): print(root) for file in files: if file.endswith(".html"): raw_d_dirs.add(root + "\\" + file) elif zip.is_zipfile(root + "\\" + file): try: with zip.ZipFile(root + "\\" + file, "r") as arch: arch.extractall(root) for f in arch.filelist: if f.filename.endswith(".html"): raw_d_dirs.add(root + "\\" + f.filename.replace("/", "\\")) except zip.BadZipfile: print("Bad zip ->", root + "\\" + file) except: print("Unknown error", root + "\\" + file) elif rar.is_rarfile(root + "\\" + file): try: with rar.RarFile(root + "\\" + file, "r") as arch: arch.extractall(root) for f in arch.infolist(): if (f.filename.endswith(".html")): raw_d_dirs.add(root + "\\" + f.filename.replace("/", "\\")) except rar.BadRarFile: print("Bad rar ->", root + "\\" + file) except: print("Unknown Error", root + "\\" + file) return raw_d_dirs
def _unpack(file_path: str, file_names: List[str], final_file_path: str, counter, output, is_canceled, is_finished) -> None: if is_zipfile(file_path): u_type = ZipFile elif is_rarfile(file_path): u_type = RarFile else: u_type = tar_open with u_type(file_path, 'r') as p_f: for file_name in file_names: if is_canceled.value: if not output.value: output.value = "`process canceled!`" if not is_finished.value: is_finished.value = True raise Exception(output.value) try: p_f.extract(file_name, final_file_path) except FileExistsError: pass except Exception as z_e: output.value = str(z_e) is_finished.value = True raise z_e else: counter.value += 1
def is_archive(filename): """ test if file is a valid archive (zip, tar or rar) """ return tarfile.is_tarfile(filename) or \ zipfile.is_zipfile(filename) or \ (ARCHIVE_RAR_AVAILABLE and rarfile.is_rarfile(filename))
def download_archive_and_add_subtitle_files(self, link, language, video, fps, num_cds): logger.info('Downloading subtitle %r', link) cache_key = sha1(link.encode("utf-8")).digest() request = region.get(cache_key) if request is NO_VALUE: request = self.session.get( link, headers={'Referer': 'https://subsunacs.net/search.php'}) request.raise_for_status() region.set(cache_key, request) else: logger.info('Cache file: %s', codecs.encode(cache_key, 'hex_codec').decode('utf-8')) try: 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) elif archive_stream.seek(0) == 0 and is_7zfile(archive_stream): return self.process_archive_subtitle_files( SevenZipFile(archive_stream), language, video, link, fps, num_cds) except: pass logger.error('Ignore unsupported archive %r', request.headers) region.delete(cache_key) return []
def zip_and_rar(file): full_path = "/root/Desktop/Csa-Checkpoint/Careful_steps/archives/unzipme." + str( file) if zipfile.is_zipfile(full_path): f.write(zipfile.ZipFile(full_path).comment + "\n") elif rarfile.is_rarfile(full_path): f.write(rarfile.RarFile(full_path).comment + "\n")
def get_image_format(filename, arguments): """gets the image format""" image = None bad_image = 1 image_format = NONE_FORMAT sequenced = False try: image = Image.open(filename) bad_image = image.verify() image_format = image.format sequenced = is_image_sequenced(image) except (OSError, IOError): pass if sequenced: image_format = SEQUENCED_TEMPLATE % image_format elif image is None or bad_image or image_format == NONE_FORMAT: image_format = ERROR_FORMAT filename_ext = os.path.splitext(filename)[-1].lower() if filename_ext in COMIC_EXTS: if zipfile.is_zipfile(filename): image_format = CBZ_FORMAT elif rarfile.is_rarfile(filename): image_format = CBR_FORMAT if (arguments.verbose > 1) and image_format == ERROR_FORMAT and (not arguments.list_only): print(filename, "doesn't look like an image or comic archive.") return image_format
def download_subtitle(self, subtitle): r = self.session.get(subtitle.download_link, headers={'Referer': self.api_url}, 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') if subtitle.is_episode: subtitle.content = self._get_subtitle_from_archive( subtitle, archive) else: subtitle.content = self.get_subtitle_from_archive( subtitle, archive)
def _unpack(self, file_names: List[str]) -> None: if is_zipfile(self._file_path): u_type = ZipFile elif is_rarfile(self._file_path): u_type = RarFile else: u_type = tar_open with u_type(self._file_path) as p_f: for file_name in file_names: if self._is_canceled: if not self._output: self._output = "`process canceled!`" if not self._is_finished: self._is_finished = True break try: p_f.extract(file_name, self._final_file_path) except FileExistsError: pass except Exception as z_e: self._output = str(z_e) self._is_finished = True break else: self._current += 1
async def unzip_file(event): if event.fwd_from: return if not exists(TEMP_DOWNLOAD_DIRECTORY): os.makedirs(TEMP_DOWNLOAD_DIRECTORY) input_str = event.pattern_match.group(1) file_name = basename(input_str) output_path = TEMP_DOWNLOAD_DIRECTORY + re.split("(.zip|.rar)", file_name)[0] if exists(input_str): start_time = datetime.now() await event.edit("`Unzipping...`") if is_zipfile(input_str): zip_type = ZipFile elif is_rarfile(input_str): zip_type = RarFile else: return await event.edit( "`Jenis file tidak didukung!`\n`Hanya Bisa ZIP dan RAR`" ) try: with zip_type(input_str, "r") as zip_obj: zip_obj.extractall(output_path) except BadRarFile: return await event.edit("**Error:** `File RAR Rusak`") except BadZipFile: return await event.edit("**Error:** `File ZIP Rusak`") except BaseException as err: return await event.edit(f"**Error:** `{err}`") end_time = (datetime.now() - start_time).seconds await event.edit( f"Unzipped `{input_str}` ke `{output_path}` dalam `{end_time}` detik." ) else: await event.edit("`404: Not Found`")
def is_rar(path, file): try: if rarfile.is_rarfile(os.path.join(path, file)): return True except OSError, e: logging.error("Error in is_rar for '%s': %s" % (file, e)) raise OSError
def main(): target_dir = sys.argv[1] newsdomains = set([]) with open(sys.argv[2], 'r') as newssites: for line in newssites: newsdomains.add(line.strip()) if len(sys.argv) < 3: print 'Usage: ' + sys.argv[0] + ' + <traversal dir> + <news domains text file>' sys.exit(1) for item in os.listdir(target_dir): item_path = os.path.join(target_dir, item) if item.startswith('web-') and rarfile.is_rarfile(item_path): rf = rarfile.RarFile(item_path) try: tmp_dir = tempfile.mkdtemp() rf.extractall(path=tmp_dir) filename = rf.namelist()[0] flatsnap = process_file(os.path.join(tmp_dir, filename), filename, newsdomains) archive_name = os.path.splitext(filename)[0] + '_news.gz' with gzip.open(archive_name, 'wb') as archive: for item in flatsnap: archive.write('%s\n' % item) finally: try: shutil.rmtree(tmp_dir) except OSError, e: if e.errno != 2: # code 2 - no such file or directory raise
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)
def findVideo(self): fullpath = self.config['file']['path'] + self.config['file']['name'] files = False if os.path.isdir(fullpath): os.chdir(fullpath) files = {"path": os.getcwd(), "files": []} archive = False for file in os.listdir(files['path']): if zipfile.is_zipfile(file): print "Unzip..." zip = zipfile.ZipFile(file) zip.extractall() archive = True elif rarfile.is_rarfile(file): try: print "Unrar..." rar = rarfile.RarFile(file) rar.extractall() archive = True except rarfile.NeedFirstVolume: pass self.config['file']['name'] = file if self.isVideo(): files['files'].append(file) if archive: allFiles = os.listdir(files['path']) for file in allFiles: self.config['file']['name'] = file if not file in files['files'] and self.isVideo(): files['files'].append(file) return files
def testPath(path): print("testing path: %s" % path) #is it file or folder ? if os.path.isfile(path): print("manga: path is a file") desc = getFileDescription(path) print("file type: %s" % desc) mime = getFilePathMime(path) print("file mime: %s" % mime) mimeSplit = mime.split('/') m1 = mimeSplit[0] m2 = mimeSplit[1] if m1 == 'image': print("image file selected,\ncontaining folder could be loaded as a manga") (folderPath, tail) = os.path.split(path) return "folder", folderPath, desc, mime elif mime == 'application/zip' or mime == 'application/x-zip' or zipfile.is_zipfile(path): print("file is probably a zip file") return "zip", path, desc, mime elif mime == 'application/rar' or mime == 'application/x-rar' or rarfile.is_rarfile(path): print("file is probably a rar file") return 'rar', path, desc, mime else: print("the path: %s is an unsupported file") print("it has this mime: %s" % mime) print("and this description: %s" % desc) return False elif os.path.isdir(path): print("manga: path is a directory") return "folder", path, None, "a folder" else: print("manga: loading failed, path is neither file nor directory") return False
def main(): parser = optparse.OptionParser("Usage: pyUnCompress.py " + "-f <zipfile | rarfile> -d <dictionary>") parser.add_option('-f', dest='zname', type='string', help='specify zip | rar file') parser.add_option('-d', dest='dname', type='string', help='specify dictionary file') (options, args) = parser.parse_args() if (options.zname == None) | (options.dname == None): print parser.usage exit(0) else: zname = options.zname dname = options.dname flag = 0 # filetype -- rar = 1 | zip = 2 if (rarfile.is_rarfile(zname)): compressFile = rarfile.RarFile(zname) elif (zipfile.is_zipfile(zname)): compressFile = zipfile.ZipFile(zname) else: print 'Unrecognized file' exit(0) passFile = open(dname) for line in passFile.readlines(): if (ALIVE == False): exit(0) password = line.strip('\n') t = Thread(target=extractFile, args=(compressFile, password)) t.start()
def download_archive_and_add_subtitle_files(self, link, language, video, fps, subs_id): logger.info('Downloading subtitle %r', link) cache_key = sha1(link.encode("utf-8")).digest() request = region.get(cache_key) if request is NO_VALUE: time.sleep(1) request = self.retry(self.session.post(link, data={ 'id': subs_id, 'lng': language.basename.upper() }, headers={ 'referer': link }, allow_redirects=False)) if not request: return [] request.raise_for_status() region.set(cache_key, request) else: logger.info('Cache file: %s', codecs.encode(cache_key, 'hex_codec').decode('utf-8')) try: archive_stream = io.BytesIO(request.content) if is_rarfile(archive_stream): return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps, subs_id) elif is_zipfile(archive_stream): return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps, subs_id) except: pass logger.error('Ignore unsupported archive %r', request.headers) region.delete(cache_key) return []
def get_archive_handler(archive_uri): if is_zipfile(archive_uri): return ZipFile elif RAR and is_rarfile(archive_uri): return RarFile else: raise TypeError
def Unzip(self, to_path=None, delete_original=False): for filepath in self: was_extracted = False new_path = to_path if to_path else self._get_base_path(filepath) if self._is_aggragate_name(new_path): new_path = self._name_to_path(filepath, new_path) if tarfile.is_tarfile(filepath): with tarfile.open(filepath) as tar_file: tar_file.extractall(new_path) was_extracted = True elif zipfile.is_zipfile(filepath): with zipfile.ZipFile(filepath, 'r') as zip_file: zip_file.extractall(new_path) was_extracted = True elif rarfile.is_rarfile(filepath): with rarfile.open(filepath) as rar_file: rar_file.extractall(new_path) was_extracted = True if was_extracted == True: if delete_original: self._delete(filepath) else: log.warning(f"Could not detect archive type of '{filepath}'")
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)
def _uncompress(self, content, function, *args, **kwargs): bc = io.BytesIO(content) cf = RarFile(bc) if is_rarfile(bc) else ( ZipFile(bc) if is_zipfile(bc) else None) return function(cf, *args, **kwargs) if cf else None
def unzip_file(zip_src, dst_dir): ''' zip_src: zip file path dst_dir: extract to this floder etc: zip_src = 'somefloder/hello.zip' dst_dir = './extract_zip' after call unzip_function, will extract hello.zip to './extract_zip/hello/' ''' unrar_file_path = r'C:\Program Files\WinRAR\Unrar' filename, ext = splitext(zip_src) if ext not in file_extensions: return None index = filename.rfind('\\') if (index != -1): sub_floder = filename[index + 1:] else: sub_floder = filename dst_dir = '\\'.join((dst_dir, sub_floder)) if zipfile.is_zipfile(zip_src): with zipfile.ZipFile(zip_src, "r") as fz: fz.extractall(dst_dir) elif rarfile.is_rarfile(zip_src): rarfile.UNRAR_TOOL = unrar_file_path with rarfile.RarFile(zip_src, 'r') as fz: fz.extractall(dst_dir)
def openFile(self): ''' Open a file stream to either a rar/cbr or zip/cbz file ''' inFile, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open file', QtCore.QDir.currentPath()) self.currentPage = 0 if zipfile.is_zipfile(inFile) == True: #Check if its a zip file (.zip, .cbz) self.z = zipfile.ZipFile(inFile, "r") elif rarfile.is_rarfile(inFile) == True: #Check if its a rar file (.rar, .cbr) self.z = rarfile.RarFile(inFile) else: msgBox = QtGui.QMessageBox() msgBox.setText("This is not a valid CBZ or CBR file!") msgBox.setStandardButtons(QtGui.QMessageBox.Ok) msgBox.setDefaultButton(QtGui.QMessageBox.Ok) ret = msgBox.exec_() #if statement is probably unecessary if ret == QtGui.QMessageBox.Ok: self.openFile() self.showImage(self.currentPage) #Make the label clickable to go forward pages Utility.clickable(self.lbl).connect(self.changePage) self.scaleFactor = 1.0 self.scaleImage(self.scaleFactor) self.updateActions()
def download_subtitle(self, subtitle): logger.info('Downloading archive %s', subtitle) r = self.session.get(subtitle.subtitle_url, headers={'Referer': MAIN_SUBDIVX_URL+subtitle.subtitle_id}, timeout=10, verify=True) r.raise_for_status() # open the archive content = None archive_stream = io.BytesIO(r.content) if is_rarfile(archive_stream): logger.debug('Identified rar archive') content = RarFile(archive_stream) # logger.info('RarFile archive %r', content) elif is_zipfile(archive_stream): logger.debug('Identified zip archive') content = ZipFile(archive_stream) else: raise ValueError('Not a valid archive') # TODO content_list = content.namelist() # NON_LATINO_REFERENCES_IN_FILENAME = ['Espa§a'.decode('utf-8'),'espa§a'.decode('utf-8')] NON_LATINO_REFERENCES_IN_FILENAME = ['Espa§a', 'espa§a'] # logger.info('archive content_list %r', content_list) if len(content_list) == 1: sub = fix_line_ending(content.read(content_list[0])) else: for name in content_list: # logger.debug('name archive') logger.debug('name archive %s', name) # discard thae FORZADOS file if name.endswith('FORZADO.srt'): logger.debug('name.endswith(FORZADO.srt): %s', name) continue # discard hidden files if os.path.split(name)[-1].startswith('.'): logger.debug( 'os.path.split(name)[-1].startswith(.): %s', name) continue # LatinoamÇrica Espa§a # discard non-subtitle files if not name.lower().endswith(MY_SUBTITLE_EXTENSIONS): logger.debug( 'not name.lower().endswith(SUBTITLE_EXTENSIONS): %s', name) continue # discard Espa§a subtitle files if any(word in name for word in NON_LATINO_REFERENCES_IN_FILENAME): logger.debug('discard España subtitle files') continue else: logger.debug('sub selected: %s', name) sub = fix_line_ending(content.read(name)) # logger.info('sub %r', sub) subtitle.content = sub
def __init__(self, path): Container.__init__(self, path) self.rf = None if rarfile.is_rarfile(path): try: self.rf = rarfile.RarFile(path, 'r') except Exception, e: "error, loading rar file failed: %s" % e
def __init__(self, path): Container.__init__(self, path) self.rf = None if rarfile.is_rarfile(path): try: self.rf = rarfile.RarFile(path,'r') except Exception, e: "error, loading rar file failed: %s" % e
def __init__(self, origFileName): self.origFileName = origFileName if zipfile.is_zipfile(origFileName): self.compressor = 'zip' elif rarfile.is_rarfile(origFileName): self.compressor = 'rar' else: self.compressor = None
def extract(archive_file, path=".", delete_on_success=False, enable_rar=False): """ Automatically detect archive type and extract all files to specified path. .. code:: python import os os.listdir(".") # ['test_structure.zip'] reusables.extract("test_structure.zip") os.listdir(".") # [ 'test_structure', 'test_structure.zip'] :param archive_file: path to file to extract :param path: location to extract to :param delete_on_success: Will delete the original archive if set to True :param enable_rar: include the rarfile import and extract :return: path to extracted files """ if not os.path.exists(archive_file) or not os.path.getsize(archive_file): logger.error("File {0} unextractable".format(archive_file)) raise OSError("File does not exist or has zero size") arch = None if zipfile.is_zipfile(archive_file): logger.debug("File {0} detected as a zip file".format(archive_file)) arch = zipfile.ZipFile(archive_file) elif tarfile.is_tarfile(archive_file): logger.debug("File {0} detected as a tar file".format(archive_file)) arch = tarfile.open(archive_file) elif enable_rar: import rarfile if rarfile.is_rarfile(archive_file): logger.debug("File {0} detected as " "a rar file".format(archive_file)) arch = rarfile.RarFile(archive_file) if not arch: raise TypeError("File is not a known archive") logger.debug("Extracting files to {0}".format(path)) try: arch.extractall(path=path) finally: arch.close() if delete_on_success: logger.debug("Archive {0} will now be deleted".format(archive_file)) os.unlink(archive_file) return os.path.abspath(path)
def getrarlist(rarname): rarfile.NEED_COMMENTS = 0 filelist = [] if not rarfile.is_rarfile(rarname): return filelist rararc = rarfile.RarFile(rarname) for rarentry in rararc.infolist(): filelist.append(rarentry.filename) return filelist
def can_handle(filename): try: import rarfile return zipfile.is_zipfile(filename) or tarfile.is_tarfile( filename) or rarfile.is_rarfile(filename) except ImportError: warnings.warn( "Can't test for rarfiles since rarfile is not installed!") return zipfile.is_zipfile(filename) or tarfile.is_tarfile(filename)
def get_comic_format(path: Path) -> Optional[str]: """Return the comic format if it is a comic archive.""" image_format = None filename_ext = path.suffix.lower() if filename_ext == _CBZ_EXT and zipfile.is_zipfile(path): image_format = _CBZ_FORMAT if filename_ext == _CBR_EXT and rarfile.is_rarfile(path): image_format = _CBR_FORMAT return image_format
def extractFirstPageCbrImage(self): print rarfile.is_rarfile(self.filePath) rar = rarfile.RarFile(self.filePath) nameList = rar.namelist() infoList = rar.infolist() nameList.sort() firstPage = None for name in nameList: firstPage = name break # print rar.printdir() if not os.path.exists("/tmp/1"): os.mkdir("/tmp/1") try: rar.extractall("/tmp/1") except: traceback.print_exc() return firstPage
def unrar(file, dest): print('unrar %s in %s' % (file, dest)) if rarfile.is_rarfile(file): rf = rarfile.RarFile(file, 'r') rf.extractall(dest) chmod(dest, 0o644) return 0 else: print('failed', file=sys.stderr) return 1
def get_archive(self, content): archive_stream = io.BytesIO(content) if rarfile.is_rarfile(archive_stream): archive = rarfile.RarFile(archive_stream) elif zipfile.is_zipfile(archive_stream): archive = zipfile.ZipFile(archive_stream) else: raise ParseResponseException('Unsupported compressed format') return archive
def get_comic_format(filename): """Return the comic format if it is a comic archive.""" image_format = None filename_ext = os.path.splitext(filename)[-1].lower() if filename_ext in _COMIC_EXTS: if zipfile.is_zipfile(filename): image_format = _CBZ_FORMAT elif rarfile.is_rarfile(filename): image_format = _CBR_FORMAT return image_format
def is_rarfile(filename): """Verify if file is rar file Args: filename: name of file Returns: True if file is a rar file otherwise, False """ return rarfile.is_rarfile(filename)
def __init__(self, origFileName): self.origFileName = origFileName if zipfile.is_zipfile(origFileName): self.compressor = 'zip' elif rarfile.is_rarfile(origFileName): self.compressor = 'rar' elif origFileName.endswith('.7z') or origFileName.endswith('.cb7'): self.compressor = '7z' else: self.compressor = None
def test_real(fn, psw): """Actual archive processing. """ xprint("Archive: %s", fn) cb = None if cf_verbose > 1: cb = show_item rfarg = fn if cf_test_memory: rfarg = io.BytesIO(open(fn, 'rb').read()) # check if rar if not rf.is_rarfile(rfarg): xprint(" --- %s is not a RAR file ---", fn) return # open r = rf.RarFile(rfarg, charset=cf_charset, info_callback=cb) # set password if r.needs_password(): if psw: r.setpassword(psw) else: xprint(" --- %s requires password ---", fn) return # show comment if cf_show_comment and r.comment: for ln in r.comment.split('\n'): xprint(" %s", ln) elif cf_verbose > 0 and r.comment: cm = repr(r.comment) if cm[0] == 'u': cm = cm[1:] xprint(" comment=%s", cm) # process for n in r.namelist(): inf = r.getinfo(n) if inf.isdir(): continue if cf_verbose == 1: show_item(inf) if cf_test_read: test_read(r, inf) if cf_extract: r.extractall() for inf in r.infolist(): r.extract(inf) if cf_test_unrar: r.testrar()
def unrar(rarFileName, path, filename=''): if (rarfile.is_rarfile(rarFileName)): rarFile = rarfile.RarFile(rarFileName) for f in rarFile.infolist(): if not filename: filename = f.filename targetFile = open(path + filename, 'wb') targetFile.write(rarFile.read(f)) targetFile.close() else: raise ValueError("File %s is not RAR file" % rarFileName)
def __init__(self): self.destdir = '' self.tmpdir = '/tmp/cdbs' self.cdbsdirs = ['IMAGE', 'SMALL_N', 'THMB_N', 'SMALL_R', 'THMB_R', 'NAME'] self.cbdsname = '' self.totalpags = 0 for comic in argv[1:]: for d in self.cdbsdirs: if not path.exists(self.tmpdir+'/'+d): makedirs(self.tmpdir+'/'+d) self.destdir = path.dirname(comic) if path.isdir(comic): self.cbdsname = path.basename(comic) print 'Convirtiendo:', self.cbdsname self.fromDir(comic) elif path.isfile(comic): tempdir = self.tmpdir+'/temp' if not path.exists(tempdir): print 'Creando directoriotemporal' makedirs(tempdir) self.cbdsname = path.splitext(path.split(comic)[1])[0] print 'Convirtiendo:', self.cbdsname try: zip = zipfile.ZipFile(comic) zip.extractall(tempdir) self.fromDir(tempdir) except zipfile.BadZipfile: if rarfile.is_rarfile(comic): rar = rarfile.RarFile(comic) if rar.needs_password(): try: passwd = raw_input('Contraseña del RAR: ') rar.setpassword(passwd) rar.extractall(tempdir) self.fromDir(self.tmpdir) except rarfile.RarCRCError: print 'Contraseña del rar invalida o archivo dañado' else: rar.extractall(tempdir) self.fromDir(self.tmpdir) elif comic.endswith('.pdf'): pipe = 'pdfimages -j %s %s/' % (comic, tempdir) cmd = Popen(pipe.split()) cmd.wait() self.fromDir(self.tmpdir) else: print 'Archivo no valido o dañado'
def extract_archive(self, movie, video): dest_dir = os.path.join(old_movie.directory, movie) os.chdir(new_movie.directory) for item in os.listdir(): if item.startswith(movie + "." + video): os.chdir(item) for archive in os.listdir(): if "part1" in archive and rarfile.is_rarfile(archive): rf = rarfile.RarFile(archive) rf.extractall(path=dest_dir, pwd="Ares275") os.rename(dest_dir +"/"+ rf.infolist()[0].filename, os.path.join(dest_dir, video) + ".mkv")
def _uncompress(self, subtitle_id, timestamp, function, *args, **kwargs): content = self.download_content(subtitle_id, timestamp) tmp = io.BytesIO(content) try: rarfile.PATH_SEP = '/' rarfile.NEED_COMMENTS = 0 cf = rarfile.RarFile(io.BytesIO(content)) if rarfile.is_rarfile(tmp) else (ZipFile(tmp.name) if is_zipfile(tmp.name) else None) return function(cf, *args, **kwargs) if cf else None finally: tmp.close()
def recursive_unrar(): unrared=[] flag = True while flag == True: flag = False indir=os.listdir('./') for files in indir: if rarfile.is_rarfile(files) and files not in unrared: flag = True unrared.append(files) extract=rarfile.RarFile(files) extract.extractall()
def extract_archive(movie, video): dest_dir = movie_dir +os.sep+ movie +os.sep os.chdir(download_dir) for item in os.listdir(): if item.startswith(movie + "." + video): os.chdir(item) for archive in os.listdir(): if "part1" in archive and rarfile.is_rarfile(archive): rf = rarfile.RarFile(archive) rf.extractall(path=dest_dir, pwd=password) os.rename(dest_dir + rf.infolist()[0].filename, dest_dir + video + ".mkv") return
def check_file(self): errorlist = [] if all (key in self.config for key in ("file","filesize","file-md5")): tmpfile = tempfile.NamedTemporaryFile(delete = False) md5sum = hashlib.md5() bytes = 0 with urllib.request.urlopen(self.config["file"]) as f: for chunk in iter(lambda: f.read(4096), b""): bytes += len(chunk) tmpfile.write(chunk) md5sum.update(chunk) if bytes > 100 * 1024 * 1024: errorlist += ["File provided is too large. (100MB limit)"] break tmpfile.close() # If everything downloaded fine if not errorlist: filelist = [] if not md5sum.hexdigest() == self.config["file-md5"]: errorlist += ["Hash provided by `file-md5` does not match file checksum."] if not bytes == self.config["filesize"]: errorlist += ["Value for `filesize` is incorrect."] # TODO: check for 7z format too, the 3ds app already supports it if zipfile.is_zipfile(tmpfile.name): with zipfile.ZipFile(tmpfile.name) as archive: filelist = archive.namelist() elif tarfile.is_tarfile(tmpfile.name): with tarfile.open(tmpfile.name) as archive: filelist = archive.getnames() elif rarfile.is_rarfile(tmpfile.name): with rarfile.RarFile(tmpfile.name) as archive: filelist = [x.replace('\\','/') for x in archive.namelist()] else: errorlist += ["`file` is not a recognized archive. Supported types: *.rar *.zip *.tar.(bz2|gz|xz}"] # Check if files are in permitted locations for filename in filelist: base = os.path.dirname(filename) # Zipfile returns directories (e.g. 3ds/) for some stupid reason if not filename == '3ds/' and not base.startswith("3ds/"+self.dir): errorlist += ["Archive file `{}` is not in a permitted location.".format(filename)] # Check for expected 3dsx file filename = '3ds/' + self.dir + '/' + self.dir + '.3dsx' if not filename in filelist: errorlist += ["Expected file not found: `{}`".format(filename)] os.remove(tmpfile.name) return errorlist
def getrarlist(rarname, filelist, mediatypes): rarfile.NEED_COMMENTS = 0 if not rarfile.is_rarfile(rarname): return False rararc = rarfile.RarFile(rarname) for rarentry in rararc.infolist(): fn = rarentry.filename if fn[-3:] in mediatypes: filelist.append(fn) if len(filelist) == 0: return False return True
def __init__(self, paths): self.to_process = [] # list[Subtitles] for p in paths: if p.startswith("http://") or p.startswith("https://"): file_size_, buffer = process_http(p) file_size = int(file_size_) md5hash = self.calculatemd5(buffer[0]) opensub_hash = Napisy24.buffer_hash(file_size, buffer) movie_name = p.rsplit("/", 1)[1] movie_subtitles_name = os.path.splitext(movie_name)[0] + ".txt" self.to_process.append(Subtitles(movie_name, movie_subtitles_name, md5hash, opensub_hash)) get_subtitles(self.to_process) self.to_process = [] break for f in glob.iglob(p): absolute_f = os.path.realpath(os.path.join(os.curdir, f)) # print absolute_f if os.path.isfile(absolute_f): if rarfile.is_rarfile(absolute_f): # rar file self.to_process.extend(self.processRarFile(absolute_f)) elif zipfile.is_zipfile(absolute_f): # zip file raise NotImplemented("Zip file handle is not implemented yet.") else: # raw file self.to_process.extend(self.processRawFile(absolute_f)) elif os.path.isdir(absolute_f): # directory for rootdir, dirname, filename in os.walk(absolute_f): rawfiles = filterVideoFiles(filename) for i in rawfiles: print rootdir + os.sep + i self.to_process.extend(self.processRawFile(rootdir + os.sep + i)) rarfiles = filterArchiveFiles(filename) for i in rarfiles: print rootdir + os.sep + i self.to_process.extend(self.processRarFile(rootdir + os.sep + i)) # get subtitles get_subtitles(self.to_process) self.to_process = []
def open_archive(self, entry): """ Returns the appropriate archive """ archive_path = entry['location'] archive = None if zipfile.is_zipfile(archive_path): try: archive = zipfile.ZipFile(file=archive_path) log.debug('Successfully opened ZIP: %s' % archive_path) except zipfile.BadZipfile: error_message = 'Bad ZIP file: %s' % archive_path log.error(error_message) entry.fail(error_message) return None except Exception as e: log.error('Failed to open ZIP: %s (%s)' (archive_path, e)) entry.fail(e) return None elif rarfile: if rarfile.is_rarfile(archive_path): try: archive = rarfile.RarFile(rarfile=archive_path) log.debug('Successfully opened RAR: %s' % archive_path) except rarfile.RarWarning as e: log.warn('Nonfatal error: %s (%s)' % (archive_path, e)) except rarfile.NeedFirstVolume: log.error('Not the first volume: %s' % archive_path) return None except rarfile.NotRarFile: log.error('Not a RAR file: %s' % archive_path) return None except Exception as e: log.error('Failed to open RAR: %s (%s)' (archive_path, e)) entry.fail(e) return None else: log.warn('Can''t extract RAR archives without the rarfile Python module.') if not archive: error_message = 'Couldn''t open file: %s' % archive_path log.error(error_message) entry.fail(error_message) return archive
def _uncompress(self, subtitle_id, timestamp, function, *args, **kwargs): content = self.download_content(subtitle_id, timestamp) # Download content might be a rar file (most common) or a zip. # Unfortunately, rarfile module only works with files (no in-memory streams) tmp = NamedTemporaryFile() try: tmp.write(content) tmp.flush() cf = RarFile(tmp.name) if is_rarfile(tmp.name) else (ZipFile(tmp.name) if is_zipfile(tmp.name) else None) return function(cf, *args, **kwargs) if cf else None finally: tmp.close()
def ArchiveFile(filename): """ Pseudo class (hence the Case) to wrap both rar and zip handling, since they both share almost identical API usage: myarchive = ArchiveFile(filename) return: a RarFile or ZipFile instance (or None), depending on <filename> content """ if rarfile.is_rarfile(filename): return rarfile.RarFile(filename, mode='r') elif zipfile.is_zipfile(filename): return zipfile.ZipFile(filename, mode='r') else: return None