Example #1
0
def unzipSomeFolder():
    folders = ('缩略图','预览图')
    folder = '缩略图'
    for k in os.listdir(path):
        if os.path.isdir(path+k):
            if os.path.exists(path+k+'/'+folders[0]) or os.path.exists(path+k+'/'+folders[1]):
                continue
            for j in os.listdir(path+k):
                file = path+k+'/'+j
                if file.endswith('.rar'):
                    rf = rarfile.RarFile(file)
                    # print(k,re.search(r'(.*\.rar)',k))
                    # for f in rf.infolist():
                    #     print(f.filename , k+'/'+folder)
                    #     if f.filename == k+'/'+folder:
                    #         print('ok')
                    try:
                        rf.extract(k+'/'+folder, path=path+k, pwd='yshqxxpt')
                        shutil.move(path+k+'/'+k+'/'+folder, path+k)
                        shutil.rmtree(path+k+'/'+k)
                    except Exception as e:
                        print(e)
                    print(k)
Example #2
0
	def __init__(self, filepath):
		self.type = 0
		try:
			if filepath.endswith(ARCHIVE_FILES):
				if filepath.endswith(ARCHIVE_FILES[:2]):
					self.archive = zipfile.ZipFile(os.path.normcase(filepath))
					b_f = self.archive.testzip()
					self.type = self.zip
				elif filepath.endswith(ARCHIVE_FILES[2:]):
					self.archive = rarfile.RarFile(os.path.normcase(filepath))
					b_f = self.archive.testrar()
					self.type = self.rar

				# test for corruption
				if b_f:
					log_w('Bad file found in archive {}'.format(filepath.encode(errors='ignore')))
					raise app_constants.CreateArchiveFail
			else:
				log_e('Archive: Unsupported file format')
				raise app_constants.CreateArchiveFail
		except:
			log.exception('Create archive: FAIL')
			raise app_constants.CreateArchiveFail
Example #3
0
def test_symlink_win(tmp_path):
    fn = "test/files/rar5-symlink-win.rar"
    with rarfile.RarFile(fn) as rf:
        assert get_props(rf, "content/dir1") == "-D-"
        assert get_props(rf, "content/dir2") == "-D-"
        assert get_props(rf, "content/file.txt") == "F--"
        assert get_props(rf, "links/bad_link") == "--L"
        assert get_props(rf, "links/dir_junction") == "--L"
        assert get_props(rf, "links/dir_link") == "--L"
        assert get_props(rf, "links/file_link") == "--L"

        with pytest.warns(rarfile.UnsupportedWarning):
            rf.extractall(tmp_path)

        assert sorted(os.listdir(tmp_path)) == ["content", "links"]
        assert sorted(os.listdir(tmp_path /
                                 "content")) == ["dir1", "dir2", "file.txt"]
        assert sorted(os.listdir(
            tmp_path / "links")) == ["bad_link", "dir_link", "file_link"]

        assert os.path.islink(tmp_path / "links/bad_link")
        assert os.path.islink(tmp_path / "links/dir_link")
        assert os.path.islink(tmp_path / "links/file_link")
Example #4
0
    def _get_compressed_fobjs(self, filename, year):
        with open(filename, mode="rb") as fobj:
            first_bytes = fobj.read(10)
        if first_bytes.startswith(b"PK\x03\x04"):  # Zip archive
            zfile = ZipFile(str(filename))
            filelist = [fn.filename for fn in zfile.filelist]
            opener = zfile
        elif first_bytes.startswith(b"Rar!"):
            rarobj = rarfile.RarFile(str(filename))
            filelist = rarobj.namelist()
            opener = rarobj
        else:
            raise RuntimeError(f"Could not extract archive '{filename}'")

        valid_names = []
        fobjs = []
        for internal_filename in filelist:
            if not self.valid_filename(internal_filename, year=year):
                continue
            fobjs.append(opener.open(internal_filename))
            valid_names.append(internal_filename)

        return fobjs, valid_names
    def _assert_subtitle_data(self, subtitleData, fileName):
        extension = os.path.splitext(fileName)[1]

        temp = tempfile.NamedTemporaryFile()
        temp.write(subtitleData)
        temp.flush()

        if (extension == ".zip"):
            rf = zipfile.ZipFile(temp.name)
        elif (extension == ".rar"):
            rf = rarfile.RarFile(temp.name)
        else:
            fail("unknown extension found %s", extension)

        for f in rf.infolist():
            data = rf.read(f.filename)

            temp = tempfile.NamedTemporaryFile()
            temp.write(data)
            temp.flush()

            self.assertIsNotNone(temp.read())
            break
Example #6
0
    def get_file_list(self, file_handler):
        """ 传入一个压缩文件控制对象,读取对应压缩文件内文件列表。
            返回 {one_sub: file_handler} """

        sub_lists_dict = dict()
        for one_file in file_handler.namelist():

            if one_file[-1] == '/':
                continue
            if os.path.splitext(one_file)[-1] in self.sub_format_list:
                sub_lists_dict[one_file] = file_handler
                continue

            if os.path.splitext(one_file)[-1] in self.support_file_list:
                sub_buff = BytesIO(file_handler.read(one_file))
                datatype = os.path.splitext(one_file)[-1]
                if datatype == '.zip':
                    sub_file_handler = zipfile.ZipFile(sub_buff, mode='r')
                elif datatype == '.rar':
                    sub_file_handler = rarfile.RarFile(sub_buff, mode='r')
                sub_lists_dict.update(self.get_file_list(sub_file_handler))

        return sub_lists_dict
Example #7
0
def extractr_archive(path_to_file):
    extracted = list()
    ext = re.findall(ZIP, path_to_file)
    if ext.__len__() > 0:
        zip = zipfile.ZipFile(path_to_file, 'r')
        for f in zip.filelist:
            zip.extract(f.filename, TMP_EXTRACT)
            path_extract = TMP_EXTRACT + '/' + f.filename
            extracted.append(str(path_extract))
        zip.close()
    ext = re.findall(RAR, path_to_file)
    if ext.__len__() > 0:
        try:
            rar = rarfile.RarFile(path_to_file)
            for f in rar.namelist():
                rar.extract(f, TMP_EXTRACT)
                path_extract = TMP_EXTRACT + '/' + f
                extracted.append(str(path_extract))
            rar.close()
        except rarfile.BadRarFile:
            LOG.error("Bad RarFile... path: %s" % path_to_file)

    ext = re.findall(SEVEN_Z, path_to_file)
    if ext.__len__() > 0:
        file = open(path_to_file, 'rb')
        seven_zip = py7zlib.Archive7z(file)
        for name in seven_zip.getnames():
            path_extract = os.path.join(TMP_EXTRACT, name)
            outdir = os.path.dirname(path_extract)
            if not os.path.exists(outdir):
                os.makedirs(outdir)
            outfile = open(path_extract, 'wb')
            outfile.write(seven_zip.getmember(name).read())
            outfile.close()
            extracted.append(str(path_extract))
        file.close()
    return extracted
Example #8
0
 def _list_rar(name, cached):
     '''
     List the contents of a rar archive.
     '''
     dirs = []
     files = []
     if HAS_RARFILE:
         with rarfile.RarFile(cached) as rf:
             for member in rf.infolist():
                 path = member.filename.replace('\\', '/')
                 if member.isdir():
                     dirs.append(path + '/')
                 else:
                     files.append(path)
     else:
         if not salt.utils.path.which('rar'):
             raise CommandExecutionError(
                 'rar command not available, is it installed?')
         output = __salt__['cmd.run'](['rar', 'lt', name],
                                      python_shell=False,
                                      ignore_retcode=False)
         matches = re.findall(r'Name:\s*([^\n]+)\s*Type:\s*([^\n]+)',
                              output)
         for path, type_ in matches:
             if type_ == 'Directory':
                 dirs.append(path + '/')
             else:
                 files.append(path)
         if not dirs and not files:
             raise CommandExecutionError(
                 'Failed to list {0}, is it a rar file? If so, the '
                 'installed version of rar may be too old to list data in '
                 'a parsable format. Installing the rarfile Python module '
                 'may be an easier workaround if newer rar is not readily '
                 'available.'.format(name),
                 info={'error': output})
     return dirs, files, []
Example #9
0
    def __init__(self, filename):
        """Opens archive."""
        self.file = os.path.realpath(filename)
        if not os.path.isfile(self.file) or not os.access(self.file, os.R_OK):
            raise IOError("Cannot open %s. No such file." % self.file)

        self.filetype = self.get_filetype()

        if self.filetype == ZIP:
            self.fd = zipfile.ZipFile(self.file, "r")
        elif self.filetype == GZIP:
            self.fd = gzip.GzipFile(self.file, "rb")
        elif self.filetype == BZIP:
            self.fd = bz2.BZ2File(self.file, "r")
        elif self.filetype == ROM:
            self.fd = open(self.file, "rb")
        elif self.filetype == RAR:
            if HAS_RAR:
                rarfile.USE_EXTRACT_HACK = 0
                self.fd = rarfile.RarFile(self.file)
            elif RAR_CMD:
                self.fd = RarCmd(self.file)
            else:
                raise IOError("rarfile module or rar/unrar is needed for %s." %
                              self.file)
        elif self.filetype == LZMA:
            if HAS_7Z:
                self.fd = Archive7z(open(self.file, "rb"))
            elif LZMA_CMD:
                self.fd = LzmaCmd(self.file)
            else:
                raise IOError("lzma module or 7z is needed for %s." %
                              self.file)
        else:
            raise IOError("File %s is not a N64 ROM file." % self.file)

        self.namelist = self.get_namelist()
Example #10
0
 def add_series(self, archive_path, series=None, series_id=None):
     """Add a series via zip file."""
     try:
         if archive_path.endswith('zip'):
             compressed = zipfile.ZipFile(archive_path)
         elif archive_path.endswith('rar'):
             compressed = rarfile.RarFile(archive_path)
         else:
             LOGGER.error("[%s] not a valid archive",
                          os.path.basename(archive_path))
             return
     except zipfile.BadZipFile:
         LOGGER.error("[%s] bad zip file", os.path.basename(archive_path))
         return
     with compressed as series_zip:
         LOGGER.info("[%s] opened archive", os.path.basename(archive_path))
         for zip_member in series_zip.infolist():
             series_zip.extract(zip_member, path=self.temp_dir.name)
             date_time = time.mktime(zip_member.date_time + (0, 0, -1))
             os.utime(os.path.join(self.temp_dir.name, zip_member.filename),
                      (date_time, date_time))
         for filename in sorted(series_zip.namelist()):
             if filename.endswith('/'):
                 continue
             LOGGER.info("[%s] processing member %s",
                         os.path.basename(archive_path), filename)
             played, _ = parse_filename(os.path.basename(filename))
             if not played:
                 played = datetime.fromtimestamp(
                     os.path.getmtime(
                         os.path.join(self.temp_dir.name, filename)))
             self.add_file(os.path.join(self.temp_dir.name, filename),
                           os.path.basename(archive_path),
                           series,
                           series_id,
                           played=played)
         LOGGER.info("[%s] finished", os.path.basename(archive_path))
Example #11
0
    def scan(self, file_object, options):
        file_limit = options.get("limit", 1000)

        self.metadata["total"] = {"files": 0, "extracted": 0}

        with io.BytesIO(file_object.data) as rar_object:
            with rarfile.RarFile(rar_object) as rf:
                rf_info_list = rf.infolist()
                self.metadata["total"]["files"] = len(rf_info_list)
                for rf_object in rf_info_list:
                    if not rf_object.isdir():
                        if self.metadata["total"]["extracted"] >= file_limit:
                            break

                        child_file = rf.read(rf_object)
                        child_info = rf.getinfo(rf_object)
                        if not child_info.needs_password():
                            rar_metadata = {
                                "scanRarHostOs":
                                HOST_OS_MAPPING[child_info.host_os]
                            }
                            child_filename = f"{self.scanner_name}::{child_info.filename}"
                            child_fo = objects.StrelkaFile(
                                data=child_file,
                                filename=child_filename,
                                depth=file_object.depth + 1,
                                parent_uid=file_object.uid,
                                root_uid=file_object.root_uid,
                                parent_hash=file_object.hash,
                                root_hash=file_object.root_hash,
                                source=self.scanner_name,
                                external_metadata=rar_metadata)
                            self.children.append(child_fo)
                            self.metadata["total"]["extracted"] += 1
                        else:
                            file_object.flags.append(
                                f"{self.scanner_name}::password_protected")
Example #12
0
def main():
    ''' Main function '''
    parser = argparse.ArgumentParser()
    parser.add_argument('num', type=int, default=None)
    args = parser.parse_args()
    num = int(args.num)

    dirpath = '../../9111/2017_'
    xpath= './tmp'+str(num)+'.txt'

    answers_list = []
    i = 0
    j = 0
    print(time_str())

    filepath = dirpath + str(num) + '.rar'
    with rarfile.RarFile(filepath) as opened_rar:
        for f in opened_rar.infolist():
            if(f.filename.count('/') == 2):
                text = opened_rar.read(f, 'r')
                out = codecs.open(xpath, "wb")
                out.write(text)
                out.close()
                text = codecs.open(xpath, "r", "utf-8").read()
                q = make_question_card(text)
                q.date = re.search( r'(\d{4}_\d+_\d+)', f.filename).group(1)
                q.questionID = re.search( r'(q\d+)', f.filename).group(1)
                answers_list.append(q.to_dict())
                i = i + 1
                if(i == 2000):
                    print(f.filename, j, time_str())
                    with open('data_'+str(num)+'_'+str(j), 'w') as outfile:
                        json.dump(answers_list, outfile)
                    i = 0
                    j = j + 1
                    answers_list = []
Example #13
0
def rar_info(fn):
    hsh = hashlib.md5(fn).hexdigest()
    file = open("output\\rar.txt", "a")
    file.write("******************************************************\n")
    file.write("Filename: " + fn + "\n")
    file.write("Hash: " + hsh + "\n")
    file.write("------------------------------------------------------\n")
    # Extract files inside the RAR archive
    rf = rarfile.RarFile(fn)
    i = 1
    for f in rf.infolist():
        file.write("Archive File" + str(i) + ": \"" + f.filename +
                   "\" File Size : " + str(f.file_size) + "\n")
        i = i + 1
        try:
            # Extract the file contents inside RAR
            file.write("\n" + "-----------File Contents------------------" +
                       "\n")
            file.write(str(rf.read(f)) + "\n")
        except:
            print("Unable to read" + f.filename + "\n")
        file.write("----------------------------------------\n")
    file.write("------------------------------------------------------\n")
    file.close()
Example #14
0
def main():
    parser = optparse.OptionParser("usage%prog --fr <rarfile> -c <charset> -n <size>")
    parser.add_option('--fr', dest='rname', type='string', help='specify rar file')
    parser.add_option('--fz', dest='zname', type='string', help='specify zip file')
    parser.add_option('-c', dest='charset', type='string', help='specify charset')
    parser.add_option('-n', dest='size', type='string', help='size of password')
    (options, args) = parser.parse_args()
    if (options.rname == None and options.zname == None) or (options.charset == None) or (options.size == None):
        print parser.usage
        exit(0)
    else:
        if options.rname:
            arname = options.rname
            arFile = rarfile.RarFile(arname)
        else:
            arname = options.zname
            arFile = zipfile.ZipFile(arname)
        charset = options.charset
        size = options.size

    size = int(size)
    for attempt in bruteforce(charset, size):
    # match it against your password, or whatever
        extractFile(arFile,attempt)
Example #15
0
    def __init__(self, _protocol, path: str, mode='r'):
        """
        通用压缩协议类初始化

        General compression protocol class initialization

        :param _protocol: 压缩协议包 | General compression protocol packages
        :param path: 压缩包路径 | compression package path
        :param mode: 读写模式 | 'r' or 'w', which 'r' means read and 'w' means write
        """
        self._protocol = _protocol
        self.path = path
        if mode not in ('r', 'w'):
            raise ValueError("Requires mode 'r', 'w'")
        if mode == 'r':
            checkIsProtocolFile(_protocol, path)
            if _protocol == zipfile:
                self.src = zipfile.ZipFile(path, 'r')
            elif _protocol == rarfile:
                self.src = rarfile.RarFile(path)
            elif _protocol == py7zr:
                self.src = py7zr.SevenZipFile(path, 'r')
            else:
                self.src = _protocol.open(path, 'r')
            self.mode = True
        elif mode == 'w':
            if _protocol == tarfile:
                self.src = _protocol.open(path, 'x:gz')
            elif _protocol == zipfile:
                self.src = _protocol.ZipFile(path, 'w')
            elif _protocol == rarfile:
                raise NotImplementedError(
                    'qs not support to create rar file because `RarFile`')
            elif _protocol == py7zr:
                self.src = _protocol.SevenZipFile(path, 'w')
            self.mode = False
Example #16
0
 def check_rar(self, dict_path):
     logging.info(f"[RAR] Cracking rar with dict {dict_path}")
     dname = self.fromDictionary(dict_path)
     try:
         with rarfile.RarFile(self.file_path) as zr:
             try:
                 if not zr.needs_password():
                     zr.close()
                     self.clear_file(self.file_path)
                     return 'EMPTY_PASSWORD'
             except Exception as e:
                 logging.error(f"[RAR] Not an empty password:  {e}")
                 pass
             for line in dname:
                 found = self.extractFile(zr, line)
                 if found == True:
                     self.clear_file(self.file_path)
                     return line
         logging.info(f"[RAR] Cracking rar with with dict {dict_path} not succeded")
         self.clear_file()
         return None
     except FileNotFoundError:
         self.clear_file()
         pass
Example #17
0
def Decompress(Source):
    print("=================== Start =========================")
    print(time.strftime("Start Time :%Y-%m-%d %X", time.localtime()))
    UnzipPath = "".join(Source.split('.')[:-1])
    if 'zip' in Source:
        import zipfile
        ZFile = zipfile.ZipFile(Source)
        if os.path.exists(UnzipPath):
            print(UnzipPath + " is exists and won't cover!")
        else:
            os.mkdir(UnzipPath)
            for names in ZFile.namelist():
                ZFile.extract(names, UnzipPath)
            print(Source + " has been decompressed to " + UnzipPath +
                  " successfullly!")
            ZFile.close()
    elif 'rar' in Source:
        Output = os.popen('pip list')
        if 'rarfile' not in Output.read():
            os.system('pip install rarfile')
            os.system('pip install unrar')
        import rarfile
        rar = rarfile.RarFile(Source)
        if os.path.exists(UnzipPath):
            print(UnzipPath + " is exists and won't cover!")
        else:
            os.mkdir(UnzipPath)
            rar.extractall(UnzipPath)
            print(Source + " has been decompressed to " + UnzipPath +
                  " successfullly!")
            rar.close()
    else:
        print("Cannot decompress " + Source + " as its not the correct format")
    print(time.strftime("End Time :%Y-%m-%d %X", time.localtime()))
    print("=================== Finish ========================")
    CountineOrExit()
Example #18
0
    def comic_archive_uncompress(
            settings: Settings, path: Path,
            image_format: str) -> Tuple[Optional[Path], Optional[ReportStats]]:
        """
        Uncompress comic archives.

        Return the name of the working directory we uncompressed into.
        """
        if not settings.comics:
            report = f"Skipping archive file: {path}"
            return None, ReportStats(path, report=report)

        if settings.verbose:
            print(f"Extracting {path}...", end="")

        # create the tmpdir
        tmp_dir = Comic._get_archive_tmp_dir(path)
        if tmp_dir.exists():
            shutil.rmtree(tmp_dir)
        tmp_dir.mkdir()

        # extract archvie into the tmpdir
        if image_format == _CBZ_FORMAT:
            with zipfile.ZipFile(path, "r") as zfile:
                zfile.extractall(tmp_dir)
        elif image_format == _CBR_FORMAT:
            with rarfile.RarFile(path, "r") as rfile:
                rfile.extractall(tmp_dir)
        else:
            report = f"{path} {image_format} is not a good format"
            return None, ReportStats(path, report=report)

        if settings.verbose:
            print("done")

        return tmp_dir, None
Example #19
0
File: unrar.py Project: wkown/py3x
def get_pwd(file_path, output_path, pwd):
    '''
    判断密码是否正确
    :param file_path: 需要破解的文件路径,这里仅对单个文件进行破解
    :param output_path: 解压输出文件路径
    :param pwd: 传入的密码
    :return:
    '''
    # 传入被解压的文件路径,生成待解压文件对象
    file = rarfile.RarFile(file_path)
    # 输出解压后的文件路径
    out_put_file_path = '{}/{}'.format(output_path, file.namelist()[0])
    try:
        # 如果发现文件被解压处理,移除该文件
        file.extractall(path=output_path, pwd=pwd)
        os.remove(out_put_file_path)
        # 说明当前密码有效,并告知
        print('Find password is "{}"'.format(pwd))
        return True
    except Exception as e:
        # 密码不正确
        print('"{}" is nor correct password!'.format(pwd))
        # print(e)
        return False
Example #20
0
def test_extract_mem(tmp_path):
    ex1 = tmp_path / "extract11"
    ex2 = tmp_path / "extract22"
    ex3 = tmp_path / "extract33"
    os.makedirs(str(ex1))
    os.makedirs(str(ex2))
    os.makedirs(str(ex3))

    with open('test/files/seektest.rar', 'rb') as f:
        arc = f.read()
    rf = rarfile.RarFile(io.BytesIO(arc))

    rf.extractall(str(ex1))
    assert os.path.isfile(str(ex1 / 'stest1.txt')) is True
    assert os.path.isfile(str(ex1 / 'stest2.txt')) is True

    rf.extract('stest1.txt', str(ex2))
    assert os.path.isfile(str(ex2 / 'stest1.txt')) is True
    assert os.path.isfile(str(ex2 / 'stest2.txt')) is False

    inf = rf.getinfo('stest2.txt')
    rf.extract(inf, str(ex3))
    assert os.path.isfile(str(ex3 / 'stest1.txt')) is False
    assert os.path.isfile(str(ex3 / 'stest2.txt')) is True
Example #21
0
def Crack(Encrypted_file):
    bFound = False
    fp = rarfile.RarFile(Encrypted_file)

    #使用循环尝试
    start = 0
    stop = 9999
    for i in range(start, stop):
        pwd = str(i).zfill(4)

        try:
            fp.extractall(path="./aaa", pwd=pwd)

            print('\n暴破成功,密码:' + pwd)
            bFound = True
            fp.close()

        except Exception as e:
            print("\r已尝试",
                  '{:.0%}'.format((i - start) / (stop - start)),
                  end="")

        if bFound:
            break
Example #22
0
def extract_archive(path):
    archive_dir, archive_filename = os.path.split(path)
    extract_dir = os.path.join(
        archive_dir,
        '__extracted__',
        os.path.splitext(archive_filename)[0],
    )
    if not os.path.isdir(extract_dir):
        os.makedirs(extract_dir)

    rf = rarfile.RarFile(path)
    rf.extractall(extract_dir)

    extracted_files = []
    for f in rf.infolist():
        if f.isdir():
            continue

        native_filename = f.filename.replace('\\', '/')

        extracted_files.append((os.path.join(extract_dir,
                                             native_filename), f.file_size))

    return extracted_files
Example #23
0
    def readdir(self, path, offset):
        """ readdir - return directory listing
        """
        logger.info("readdir -- path: " + str(path) + "  offset: " + str(offset) )
        dirent = [ '.', '..' ]

        if isRarFilePath(path):
            logger.debug("readdir: on rar archive, using rarfile")
            rf = rarfile.RarFile('.' + path, 'r', None, None, False)
            for e in rf.namelist():
                dirent.append(str(e))
        else:
            logger.debug("readdir: normal dir, using os.listdir()")
            # TODO: why do we need to try this? weird way to handle it
            try:
                os.listdir('.' + path)
            except:
                return

            for e in os.listdir('.' + path):
                dirent.append(e)

        for e in dirent:
            yield fuse.Direntry(e)
Example #24
0
    def load_data(self, source):
        try:
            archive = rarfile.RarFile(source)
        except (rarfile.NotRarFile, rarfile.BadRarFile) as e:
            raise BadArchiveError('Archive: `{0}`, ver: `{1}` corrupted'
                                  ' or is not rar-archive'.format(source))

        if not archive.namelist():
            raise BadArchiveError('Archive: `{0}`, ver: `{1}` is empty'
                                  ''.format(source))

        first_name = archive.namelist()[0]
        if table_dbf_re.match(first_name) or table_dbt_re.match(first_name):
            pre_unpack.send(sender=self.__class__, archive=archive)

            path = LocalArchiveTableList.unpack(archive=archive,
                                                tempdir=self.tempdir)

            post_unpack.send(sender=self.__class__, archive=archive, dst=path)

            return DirectoryTableList.wrapper_class(source=path,
                                                    is_temporary=True)

        return self.wrapper_class(source=archive)
Example #25
0
 def get(cls,
         species='hsa',
         directed=True,
         sources=False,
         local_path=None,
         **kwargs):
     # ---
     import rarfile
     # ---
     species = 'human' if species == 'hsa' else 'mouse'
     what = species if not directed else 'RegulatoryDirections'
     local_path = DEFAULT_REG_NETWORK_LOCAL_PATH if local_path is None else local_path
     if directed:
         local_file = os.path.join(local_path, what + '.rar')
     else:
         local_file = os.path.join(local_path, species + '.zip')
     if not sources:
         return cls.get_data_from_form(species,
                                       local_path,
                                       skiprows=1,
                                       header=None)
     if not os.path.isfile(local_file):
         cls.download(what, local_path=local_path, **kwargs)
     if directed:
         filename = 'kegg.' + species + '.reg.direction'
         with rarfile.RarFile(local_file) as rf:
             with rf.open(filename) as fp:
                 return pd.read_table(fp,
                                      sep='\s+',
                                      skiprows=1,
                                      header=None)
     else:
         filename = species + '.source'
         with zipfile.ZipFile(local_file) as zf:
             with zf.open(filename) as fp:
                 return pd.read_table(fp, header=None, low_memory=False)
Example #26
0
def extract_rar(dirname, dir_rar, bearing):

    print("Extracting Bearing Data:", bearing)
    dir_bearing_rar = os.path.join(dirname, dir_rar, bearing + ".rar")
    dir_bearing_data = os.path.join(dirname, bearing)
    if not os.path.exists(dir_bearing_data):
        file_name = dir_bearing_rar
        Archive(file_name).extractall(dirname)
        extracted_files_qnt = len([
            name for name in os.listdir(dir_bearing_data)
            if os.path.isfile(os.path.join(dir_bearing_data, name))
        ])
    else:
        extracted_files_qnt = len([
            name for name in os.listdir(dir_bearing_data)
            if os.path.isfile(os.path.join(dir_bearing_data, name))
        ])
    rf = rarfile.RarFile(dir_bearing_rar)
    rar_files_qnt = len(rf.namelist())

    if rar_files_qnt != extracted_files_qnt + 1:
        shutil.rmtree(dir_bearing_data)
        print("Extracted Files Incorrect. Extracting Again.")
        extract_rar(dirname, dir_rar, bearing)
Example #27
0
File: rar.py Project: x0rzkov/aleph
    def unpack(self, file_path, entity, temp_dir):
        # FIXME: need to figure out how to unpack multi-part files.
        try:
            with rarfile.RarFile(file_path.as_posix()) 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 exc:
                        # TODO: should this be a fatal error?
                        log.warning("Failed to unpack [%s]: %s", name, exc)
        except rarfile.NeedFirstVolume as nfv:
            raise ProcessingException('Cannot load RAR partials') from nfv
        except rarfile.PasswordRequired as pr:
            raise ProcessingException(str(pr)) from pr
        except (rarfile.Error, TypeError) as err:
            raise ProcessingException('Invalid RAR file: %s' % err) from err
Example #28
0
 def userUnpackComic(self, comic_path, user):
     logging.basicConfig(level=logging.DEBUG,
                         filename=os.path.join(gazee.DATA_DIR, 'gazee.log'))
     logger = logging.getLogger(__name__)
     logger.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
Example #29
0
 def extractRar(self, directory_name):
     '''
     extracting rar file
     '''
     os.chdir(directory_name)
     #         directory_name = '/docs/new/library/8006'
     listOfFiles = [
         name for name in os.listdir(directory_name)
         if not os.path.isdir(os.path.join(directory_name, name))
     ]
     for fileName in listOfFiles:
         if fileName.endswith(".rar"):
             #                 print fileName
             directory_name
             rar = rarfile.RarFile(os.path.join(directory_name, fileName))
             #                 print rar.namelist()
             infoList = rar.infolist()
             nameList = rar.namelist()
             for name in nameList:
                 if not ((name.endswith('.html')) or
                         (name.endswith('.htm')) or
                         (name.endswith('.txt'))):
                     rar.extract(name, directory_name)
     pass
Example #30
0
def test_extract():
    os.makedirs('tmp/extract1')
    os.makedirs('tmp/extract2')
    os.makedirs('tmp/extract3')
    rf = rarfile.RarFile('test/files/seektest.rar')

    rf.extractall('tmp/extract1')
    assert_true(os.path.isfile('tmp/extract1/stest1.txt'))
    assert_true(os.path.isfile('tmp/extract1/stest2.txt'))

    rf.extract('stest1.txt', 'tmp/extract2')
    assert_true(os.path.isfile('tmp/extract2/stest1.txt'))
    assert_false(os.path.isfile('tmp/extract2/stest2.txt'))

    inf = rf.getinfo('stest2.txt')
    rf.extract(inf, 'tmp/extract3')
    assert_false(os.path.isfile('tmp/extract3/stest1.txt'))
    assert_true(os.path.isfile('tmp/extract3/stest2.txt'))

    rf.extractall('tmp/extract2', ['stest1.txt'])
    assert_true(os.path.isfile('tmp/extract2/stest1.txt'))

    rf.extractall('tmp/extract3', [rf.getinfo('stest2.txt')])
    assert_true(os.path.isfile('tmp/extract3/stest2.txt'))