Ejemplo n.º 1
0
    def retrieve_archive(self, package, path=None, command=None):
        archive = package.archive
        url = package.url
        self.logger.info("-Downloading {}...".format(url))
        try:
            r = requests.get(url, allow_redirects=False, stream=True)
            if r.status_code == 302:
                newurl = altlink(r.url)
                # newurl = r.headers['Location']
                self.logger.info('Redirected to new URL: {}'.format(newurl))
                r = requests.get(newurl, stream=True)
        except requests.exceptions.ConnectionError as e:
            self.logger.warning("Caught download error: %s" % e.args)
            return False
        else:
            with open(archive, 'wb') as fd:
                for chunk in r.iter_content(chunk_size=8196):
                    fd.write(chunk)
            self.logger.info("-Extracting {}...".format(archive))

            if sys.version_info > (3, 5):
                if not py7zr.is_7zfile(archive):
                    raise BadPackageFile
            if command is None:
                py7zr.SevenZipFile(archive).extractall(path=path)
            else:
                if path is not None:
                    run([command, 'x', '-aoa', '-bd', '-y', '-o{}'.format(path), archive])
                else:
                    run([command, 'x', '-aoa', '-bd', '-y', archive])
            os.unlink(archive)
        return True
Ejemplo n.º 2
0
def test_py7zr_is_not_7zfile():
    tmpdir = tempfile.mkdtemp()
    target = os.path.join(tmpdir, 'test_not.7z')
    with open(target, 'wb') as f:
        f.write(b'12345dahodjg98adfjfak;')
    assert not py7zr.is_7zfile(target)
    shutil.rmtree(tmpdir)
Ejemplo n.º 3
0
Archivo: cli.py Proyecto: secsilm/py7zr
 def run_test(self, args):
     target = args.arcfile
     if not py7zr.is_7zfile(target):
         print('not a 7z file')
         return 1
     with open(target, 'rb') as f:
         try:
             a = py7zr.SevenZipFile(f)
             file = sys.stdout
             file.write("Testing archive: {}\n".format(a.filename))
             self.print_archiveinfo(archive=a, file=file)
             file.write('\n')
             if a.testzip() is None:
                 file.write('Everything is Ok\n')
                 return 0
             else:
                 file.write('Bad 7zip file\n')
                 return 1
         except py7zr.exceptions.Bad7zFile:
             print('Header is corrupted. Cannot read as 7z file.')
             return 1
         except py7zr.exceptions.PasswordRequired:
             print(
                 'The archive is encrypted but password is not given. FAILED.'
             )
             return 1
Ejemplo n.º 4
0
    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 []
Ejemplo n.º 5
0
Archivo: cli.py Proyecto: secsilm/py7zr
    def run_extract(self, args: argparse.Namespace) -> int:
        target = args.arcfile
        verbose = args.verbose
        if not py7zr.is_7zfile(target):
            print('not a 7z file')
            return 1
        if not args.password:
            password = None  # type: Optional[str]
        else:
            try:
                password = getpass.getpass()
            except getpass.GetPassWarning:
                sys.stderr.write('Warning: your password may be shown.\n')
                return 1
        try:
            a = py7zr.SevenZipFile(target, 'r', password=password)
        except py7zr.exceptions.Bad7zFile:
            print('Header is corrupted. Cannot read as 7z file.')
            return 1
        except py7zr.exceptions.PasswordRequired:
            print(
                'The archive is encrypted, but password is not given. ABORT.')
            return 1
        except lzma.LZMAError or _lzma.LZMAError:
            if password is None:
                print('The archive is corrupted. ABORT.')
            else:
                print('The archive is corrupted, or password is wrong. ABORT.')
            return 1

        cb = None  # Optional[ExtractCallback]
        if verbose:
            archive_info = a.archiveinfo()
            cb = CliExtractCallback(total_bytes=archive_info.uncompressed,
                                    ofd=sys.stderr)
        try:
            if args.odir:
                a.extractall(path=args.odir, callback=cb)
            else:
                a.extractall(callback=cb)
        except py7zr.exceptions.UnsupportedCompressionMethodError:
            print("Unsupported compression method is used in archive. ABORT.")
            return 1
        except py7zr.exceptions.DecompressionError:
            print("Error has been occurred during decompression. ABORT.")
            return 1
        except py7zr.exceptions.PasswordRequired:
            print(
                'The archive is encrypted, but password is not given. ABORT.')
            return 1
        except lzma.LZMAError or _lzma.LZMAError:
            if password is None:
                print('The archive is corrupted. ABORT.')
            else:
                print('The archive is corrupted, or password is wrong. ABORT.')
            return 1
        else:
            return 0
Ejemplo n.º 6
0
def un_7z(folderPath, filePath, _7z_password):
    try:
        if py7zr.is_7zfile(filePath):
            with py7zr.SevenZipFile(filePath, password=_7z_password,
                                    mode='r') as sevenZ_f:
                sevenZ_f.extractall(folderPath)
        return 0
    except:
        return 1
Ejemplo n.º 7
0
 def run_list(self, args):
     target = args.arcfile
     if not py7zr.is_7zfile(target):
         print('not a 7z file')
         return (1)
     with open(target, 'rb') as f:
         a = py7zr.SevenZipFile(f)
         a.list()
     return (0)
Ejemplo n.º 8
0
    def run_extract(self, args: argparse.Namespace) -> int:
        target = args.arcfile
        if not py7zr.is_7zfile(target):
            print('not a 7z file')
            return (1)

        a = py7zr.SevenZipFile(target, 'r')
        if args.odir:
            a.extractall(path=args.odir)
        else:
            a.extractall()
        return (0)
Ejemplo n.º 9
0
 def run_test(self, args):
     target = args.arcfile
     if not py7zr.is_7zfile(target):
         print('not a 7z file')
         return (1)
     with open(target, 'rb') as f:
         a = py7zr.SevenZipFile(f)
         res = a.test()
     if res:
         return (0)
     else:
         return (1)
 def un_py7zr(filename, extract_dir):
     list = []
     try:
         is7z = py7zr.is_7zfile(filename)
         if is7z:
             ret = py7zr.unpack_7zarchive(filename, extract_dir)
             arc = py7zr.SevenZipFile(filename)
             list = arc.getnames()
             # print(list)
         else:
             print('un_py7zr: unknow file type')
     except Exception as e:
         print(e)
     return list
Ejemplo n.º 11
0
 def install(self):
     downloadUrl: str = None
     for tag in get(self.app.repoApiUrl).json():
         if tag['name'] == self.app.version:
             for asset in tag['assets']:
                 if '.exe' in asset['name']:
                     downloadUrl = asset['browser_download_url']
                     break
             break
     if not downloadUrl:
         wx.GenericMessageDialog(
             parent=self.app.root,
             message="Can't find the version specified",
             caption='Error',
             style=None,
         ).ShowModal()
         wx.CallAfter(self.app.root.Destroy)
         return
     request = get(downloadUrl, stream=True)  # download BEE
     # working variables
     zipdata = io.BytesIO()
     dl = 0
     total_length = int(request.headers.get('content-length'))
     total_length_mb: int(total_length / 1024 / 1024)
     wx.CallAfter(self.app.root.progBar.SetRange, total_length)
     # download!
     for data in request.iter_content(chunk_size=1024):
         dl += len(data)
         zipdata.write(data)
         done = int(100 * dl / total_length)
         print(f'total: {total_length}, dl: {dl}, done: {done}')
         wx.CallAfter(self.app.root.progBar.SetValue, done)
         wx.CallAfter(self.app.root.megaText.SetLabel,
                      f'Done: {done / 1024 / 1024 }/{total_length_mb}MB')
         wx.CallAfter(self.app.root.speedText.SetLabel,
                      f'Speed: {len(data)}mbs')
     wx.CallAfter(self.app.root.progBar.Pulse)
     # read the data as bytes and then create the zipfile object from it
     if py7zr.is_7zfile(zipdata):
         wx.GenericMessageDialog(
             parent=self.app.root,
             message="The downloaded file wasn't a 7z file.",
             caption='Error',
             style=None,
         ).ShowModal()
         wx.CallAfter(self.app.root.Destroy)
         return
     tempdir = tempfile.mkdtemp(prefix='bridge-inst')
     py7zr.unpack_7zarchive(zipdata, tempdir)
     shutil.move(tempdir, self.app.installPath)
Ejemplo n.º 12
0
    def un_py7zr(filename):
        extract_dir = os.getcwd() + "\\firmware"
        if os.path.isdir(extract_dir):
            pass
        else:
            os.mkdir(extract_dir)

        is7z = py7zr.is_7zfile(filename)
        py7zr.SevenZipFile
        if is7z:
            ret = py7zr.unpack_7zarchive(filename, extract_dir)
            print(ret)
        else:
            print('unknow file type')
        return extract_dir
Ejemplo n.º 13
0
 def run_test(self, args):
     target = args.arcfile
     if not py7zr.is_7zfile(target):
         print('not a 7z file')
         return (1)
     with open(target, 'rb') as f:
         a = py7zr.SevenZipFile(f)
         file = sys.stdout
         file.write("Testing archive: {}\n".format(a.filename))
         self.print_archiveinfo(archive=a, file=file)
         file.write('\n')
         if a.test():
             file.write('Everything is Ok\n')
             return (0)
         else:
             file.write('Bad 7zip file\n')
             return (1)
Ejemplo n.º 14
0
def async_unzip(file_path, zip_out=TEMP_DOWNLOAD_DIRECTORY):
    if not exists(zip_out):
        makedirs(zip_out)
    output_path = join(zip_out, basename(splitext(file_path)[0]))
    if is_zipfile(file_path):
        zip_type = ZipFile
    elif is_rarfile(file_path):
        zip_type = RarFile
    elif is_tarfile(file_path):
        zip_type = TarFile
    elif is_7zfile(file_path):
        zip_type = SevenZipFile
    else:
        raise TypeError("Unsupported archive.")
    with zip_type(file_path, "r") as zip_obj:
        zip_obj.extractall(output_path)

    return output_path
Ejemplo n.º 15
0
def extract_file(file_path, directory) -> bool:
    """
    Extract the contents of a file to a directory.
    Uses 7za or libarchive depending on what's available
    WARNING:  This can and will silently overwrite files in the target directory.
    """
    requirements = requirement_status()
    if requirements["py7zr"]:
        import py7zr
        if py7zr.is_7zfile(file_path):
            return extract_via_py7zr(file_path, directory)
    if requirements["libarchive"]:
        return extract_via_libarchive(file_path, directory)
    if requirements["7-Zip"]:
        return extract_via_7za(file_path, directory)
    update_logger.warning(
        "No usable file extractors found.  Try installing 7-Zip.")
    return False
Ejemplo n.º 16
0
 def run_extract(self, args: argparse.Namespace) -> int:
     target = args.arcfile
     if not py7zr.is_7zfile(target):
         print('not a 7z file')
         return (1)
     if not args.password:
         password = None  # type: Optional[str]
     else:
         try:
             password = getpass.getpass()
         except getpass.GetPassWarning:
             sys.stderr.write('Warning: your password may be shown.\n')
             return (1)
     a = py7zr.SevenZipFile(target, 'r', password=password)
     if args.odir:
         a.extractall(path=args.odir)
     else:
         a.extractall()
     return (0)
Ejemplo n.º 17
0
    def unzip_a_file(filename: str, extract_dir=None, file_password=None):
        """Extract files in a .7z or .zip archive"""

        import zipfile
        import py7zr
        import os

        if extract_dir is None:
            extract_dir = os.getcwd()

        # when file is .zip file
        if zipfile.is_zipfile(filename):
            with open(filename, mode='rb') as f:
                zipfile.ZipFile(f).extractall(path=extract_dir)

        # when file is .7z file
        elif py7zr.is_7zfile(filename):
            py7zr.SevenZipFile(
                filename, password=file_password).extractall(path=extract_dir)

        print('Operation Complete.')
Ejemplo n.º 18
0
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)
    output_path = TEMP_DOWNLOAD_DIRECTORY + basename(splitext(input_str)[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
        elif is_tarfile(input_str):
            zip_type = TarFile
        elif is_7zfile(input_str):
            zip_type = SevenZipFile
        else:
            return await event.edit(
                "`Unsupported file types!`\n`ZIP, TAR, 7z, and RAR only`"
            )
        try:
            with zip_type(input_str, "r") as zip_obj:
                zip_obj.extractall(output_path)
        except BadRarFile:
            return await event.edit("**Error:** `Corrupted RAR File`")
        except BadZipFile:
            return await event.edit("**Error:** `Corrupted ZIP File`")
        except Bad7zFile:
            return await event.edit("**Error:** `Corrupted 7z File`")
        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}` into `{output_path}` in `{end_time}` seconds."
        )
    else:
        await event.edit("`404: Not Found`")
Ejemplo n.º 19
0
    def retrieve_archive(package, path=None, command=None):
        archive = package.archive
        url = package.url
        print("-Downloading {}...".format(url))
        try:
            r = requests.get(url, stream=True, allow_redirects=False)
            if r.status_code == 302:
                # tsinghua.edu.cn is problematic and it prohibit service to specific geo location.
                # we will use another redirected location for that.
                newurl = r.headers['Location']
                mml = Metalink(url)
                newurl = mml.altlink(blacklist=blacklist)
                print('Redirected to new URL: {}'.format(newurl))
                r = requests.get(newurl, stream=True, allow_redirects=True)
        except requests.exceptions.ConnectionError as e:
            print("Caught download error: %s" % e.args)
            return False
        else:
            with open(archive, 'wb') as fd:
                for chunk in r.iter_content(chunk_size=8196):
                    fd.write(chunk)
            print("-Extracting {}...".format(archive))

            if sys.version_info > (3, 5):
                if not py7zr.is_7zfile(archive):
                    raise BadPackageFile
            if command is None:
                py7zr.SevenZipFile(archive).extractall(path=path)
            else:
                if path is not None:
                    run([
                        command, 'x', '-aoa', '-bd', '-y', '-o{}'.format(path),
                        archive
                    ])
                else:
                    run([command, 'x', '-aoa', '-bd', '-y', archive])
            os.unlink(archive)
        return True
Ejemplo n.º 20
0
def checkIsProtocolFile(protocol, path):
    """
    验证压缩包文件

    Verify compressed package file
    :param protocol: 压缩协议 | compress protocol
    :param path: 文件路径 | File Path
    :return:
    """
    if os.path.exists(path):
        if protocol == tarfile and not tarfile.is_tarfile(path):
            raise TypeError(f"{path} " + ('Not recognized by tar protocol' if
                                          user_lang != 'zh' else '无法被tar协议识别'))
        elif protocol == zipfile and not zipfile.is_zipfile(path):
            raise TypeError(f"{path} " + ('Not recognized by zip protocol' if
                                          user_lang != 'zh' else '无法被zip协议识别'))
        elif protocol == rarfile and not rarfile.is_rarfile(path):
            raise TypeError(f"{path} " + ('Not recognized by rar protocol' if
                                          user_lang != 'zh' else '无法被rar协议识别'))
        elif protocol == py7zr and not py7zr.is_7zfile(path):
            raise TypeError(f"{path} " + ('Not recognized by 7z protocol' if
                                          user_lang != 'zh' else '无法被7z协议识别'))
    else:
        raise FileNotFoundError
Ejemplo n.º 21
0
def test_py7zr_is_7zfile():
    assert py7zr.is_7zfile(os.path.join(testdata_path, "test_1.7z"))
Ejemplo n.º 22
0
Archivo: cli.py Proyecto: secsilm/py7zr
    def run_list(self, args):
        """Print a table of contents to file. """
        target = args.arcfile
        verbose = args.verbose
        if not py7zr.is_7zfile(target):
            print('not a 7z file')
            return 1
        with open(target, 'rb') as f:
            a = py7zr.SevenZipFile(f)
            file = sys.stdout
            archive_info = a.archiveinfo()
            archive_list = a.list()
            if verbose:
                file.write("Listing archive: {}\n".format(target))
                file.write("--\n")
                file.write("Path = {}\n".format(archive_info.filename))
                file.write("Type = 7z\n")
                fstat = os.stat(archive_info.filename)
                file.write("Phisical Size = {}\n".format(fstat.st_size))
                file.write("Headers Size = {}\n".format(
                    archive_info.header_size))
                file.write("Method = {}\n".format(archive_info.method_names))
                if archive_info.solid:
                    file.write("Solid = {}\n".format('+'))
                else:
                    file.write("Solid = {}\n".format('-'))
                file.write("Blocks = {}\n".format(archive_info.blocks))
                file.write('\n')
            file.write('total %d files and directories in %sarchive\n' %
                       (len(archive_list),
                        (archive_info.solid and 'solid ') or ''))
            file.write(
                '   Date      Time    Attr         Size   Compressed  Name\n')
            file.write(
                '------------------- ----- ------------ ------------  ------------------------\n'
            )
            for f in archive_list:
                if f.creationtime is not None:
                    lastwritedate = f.creationtime.astimezone(Local).strftime(
                        "%Y-%m-%d")
                    lastwritetime = f.creationtime.astimezone(Local).strftime(
                        "%H:%M:%S")
                else:
                    lastwritedate = '         '
                    lastwritetime = '         '
                if f.is_directory:
                    attrib = 'D...'
                else:
                    attrib = '....'
                if f.archivable:
                    attrib += 'A'
                else:
                    attrib += '.'
                if f.is_directory:
                    extra = '           0 '
                elif f.compressed is None:
                    extra = '             '
                else:
                    extra = '%12d ' % (f.compressed)
                file.write('%s %s %s %12d %s %s\n' %
                           (lastwritedate, lastwritetime, attrib,
                            f.uncompressed, extra, f.filename))
            file.write(
                '------------------- ----- ------------ ------------  ------------------------\n'
            )

        return 0
Ejemplo n.º 23
0
Archivo: cli.py Proyecto: miurahr/py7zr
    def _run_list(self, target, verbose):
        if not py7zr.is_7zfile(target):
            print("not a 7z file")
            return 1
        with py7zr.SevenZipFile(target, "r") as a:
            file = sys.stdout
            archive_info = a.archiveinfo()
            archive_list = a.list()
            if verbose:
                if isinstance(target, io.IOBase):
                    file.write("Listing archive: {}\n".format(target.name))
                else:
                    file.write("Listing archive: {}\n".format(str(target)))
                file.write("--\n")
                file.write("Path = {}\n".format(archive_info.filename))
                file.write("Type = 7z\n")
                fstat = archive_info.stat
                file.write("Phisical Size = {}\n".format(fstat.st_size))
                file.write("Headers Size = {}\n".format(
                    archive_info.header_size))
                file.write("Method = {}\n".format(", ".join(
                    archive_info.method_names)))
                if archive_info.solid:
                    file.write("Solid = {}\n".format("+"))
                else:
                    file.write("Solid = {}\n".format("-"))
                file.write("Blocks = {}\n".format(archive_info.blocks))
                file.write("\n")
            file.write("total %d files and directories in %sarchive\n" %
                       (len(archive_list),
                        (archive_info.solid and "solid ") or ""))
            file.write(
                "   Date      Time    Attr         Size   Compressed  Name\n")
            file.write(
                "------------------- ----- ------------ ------------  ------------------------\n"
            )
            for f in archive_list:
                if f.creationtime is not None:
                    lastwritedate = f.creationtime.astimezone(Local).strftime(
                        "%Y-%m-%d")
                    lastwritetime = f.creationtime.astimezone(Local).strftime(
                        "%H:%M:%S")
                else:
                    lastwritedate = "         "
                    lastwritetime = "         "
                if f.is_directory:
                    attrib = "D..."
                else:
                    attrib = "...."
                if f.archivable:
                    attrib += "A"
                else:
                    attrib += "."
                if f.is_directory:
                    extra = "           0 "
                elif f.compressed is None:
                    extra = "             "
                else:
                    extra = "%12d " % (f.compressed)
                file.write("%s %s %s %12d %s %s\n" % (
                    lastwritedate,
                    lastwritetime,
                    attrib,
                    f.uncompressed,
                    extra,
                    f.filename,
                ))
            file.write(
                "------------------- ----- ------------ ------------  ------------------------\n"
            )

        return 0
Ejemplo n.º 24
0
def test_py7zr_is_not_7zfile(tmp_path):
    target = tmp_path.joinpath("test_not.7z")
    with target.open("wb") as f:
        f.write(b"12345dahodjg98adfjfak;")
    with target.open("rb") as f:
        assert not py7zr.is_7zfile(f)
Ejemplo n.º 25
0
def test_py7zr_is_7zfile_path():
    assert py7zr.is_7zfile(pathlib.Path(testdata_path).joinpath("test_1.7z"))
Ejemplo n.º 26
0
def test_py7zr_is_7zfile_fileish():
    assert py7zr.is_7zfile(open(os.path.join(testdata_path, "test_1.7z"),
                                "rb"))
Ejemplo n.º 27
0
def test_py7zr_is_7zfile_fileish():
    assert py7zr.is_7zfile(open(os.path.join(testdata_path, 'test_1.7z'),
                                'rb'))
Ejemplo n.º 28
0
 def checkFileCorrupted(self,path7Z):
     return is_7zfile(path7Z)
Ejemplo n.º 29
0
def test_py7zr_is_not_7zfile(tmp_path):
    target = tmp_path.joinpath('test_not.7z')
    with target.open('wb') as f:
        f.write(b'12345dahodjg98adfjfak;')
    with target.open('rb') as f:
        assert not py7zr.is_7zfile(f)