Beispiel #1
0
    def from_archive(cls, archive, encoding=ENCODING):
        '''Instantiates an Entry class and sets all the properties from an archive header.'''
        e = _libarchive.archive_entry_new()
        try:
            call_and_check(_libarchive.archive_read_next_header2, archive._a,
                           archive._a, e)
            mode = _libarchive.archive_entry_filetype(e)
            mode |= _libarchive.archive_entry_perm(e)

            if PY3:
                pathname = _libarchive.archive_entry_pathname(e)
            else:
                pathname = _libarchive.archive_entry_pathname(e).decode(
                    encoding)

            entry = cls(
                pathname=pathname,
                size=_libarchive.archive_entry_size(e),
                mtime=_libarchive.archive_entry_mtime(e),
                mode=mode,
                hpos=archive.header_position,
            )
        finally:
            _libarchive.archive_entry_free(e)
        return entry
 def from_archive(cls, archive, encoding=ENCODING):
     '''Instantiates an Entry class and sets all the properties from an archive header.'''
     e = _libarchive.archive_entry_new()
     try:
         call_and_check(_libarchive.archive_read_next_header2, archive._a, archive._a, e)
         mode = _libarchive.archive_entry_filetype(e)
         mode |= _libarchive.archive_entry_perm(e)
         entry = cls(
             pathname=_libarchive.archive_entry_pathname(e),
             size=_libarchive.archive_entry_size(e),
             mtime=_libarchive.archive_entry_mtime(e),
             mode=mode,
             hpos=archive.header_position,
         )
     finally:
         _libarchive.archive_entry_free(e)
     return entry
Beispiel #3
0
    def extract(container, archive, target_path, checked=None):
        target_path = os.path.abspath(target_path).encode('utf8')
        archive_path = s2u(archive.filename)

        # reopen archive file
        archive.denit()
        archive.f.seek(0)
        archive.init()

        a = archive._a
        try:
            if checked:
                logging.info(u"START extract selective '{0}'".format(
                    archive_path
                ))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break

                        pathname = _libarchive.archive_entry_pathname(e) \
                            .decode('utf8', 'replace')
                        if pathname[-1] == '/':
                            pathname = pathname[:-1]

                        path = s2u(os.path.join(
                            target_path, pathname
                        ))

                        logging.info(u"from '{0}' to '{1}'".format(
                            pathname, path
                        ))

                        if LibArchive.verify(archive_path, pathname, checked):
                            ftype = _libarchive.archive_entry_filetype(e)
                            if stat.S_ISDIR(ftype):
                                makepath(path)
                            else:
                                makepath(os.path.dirname(path))
                                with open(path, 'wb') as f:
                                    _libarchive.archive_read_data_into_fd(
                                        a, f.fileno()
                                    )
                    finally:
                        _libarchive.archive_entry_free(e)

                logging.info(u"END extract selective '{0}'".format(
                    archive_path
                ))

            else:
                logging.info(u"START extract all '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break
                        pathname = _libarchive.archive_entry_pathname(e) \
                            .decode('utf8', 'replace')
                        path = s2u(os.path.join(target_path, pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            pathname, path
                        ))

                        if stat.S_ISDIR(_libarchive.archive_entry_filetype(e)):
                            makepath(path)
                        else:
                            makepath(os.path.dirname(path))
                            with open(path, 'wb') as f:
                                _libarchive.archive_read_data_into_fd(
                                    a, f.fileno()
                                )
                    finally:
                        _libarchive.archive_entry_free(e)
                logging.info(u"END extract all '{0}'".format(archive_path))
        finally:
            archive.close()
Beispiel #4
0
    def extract(container, archive, target_path, checked=None):
        target_path = os.path.abspath(target_path)
        archive_path = s2u(archive.filename)

        # reopen archive file
        archive.denit()
        archive.f.seek(0)
        archive.init()

        a = archive._a
        try:
            if checked:
                logging.info(
                    u"START extract selective '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break

                        pathname = _libarchive.archive_entry_pathname(e)
                        if pathname[-1] == '/':
                            pathname = pathname[:-1]

                        path = os.path.join(target_path, s2u(pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            s2u(pathname), s2u(path)))

                        if LibArchive.verify(archive_path, pathname, checked):
                            ftype = _libarchive.archive_entry_filetype(e)
                            if stat.S_ISDIR(ftype):
                                makepath(path)
                            else:
                                makepath(os.path.dirname(path))
                                with open(path, 'wb') as f:
                                    _libarchive.archive_read_data_into_fd(
                                        a, f.fileno())
                    finally:
                        _libarchive.archive_entry_free(e)

                logging.info(
                    u"END extract selective '{0}'".format(archive_path))

            else:
                logging.info(u"START extract all '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break
                        pathname = _libarchive.archive_entry_pathname(e)
                        path = os.path.join(target_path, s2u(pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            s2u(pathname), s2u(path)))

                        if stat.S_ISDIR(_libarchive.archive_entry_filetype(e)):
                            makepath(path)
                        else:
                            makepath(os.path.dirname(path))
                            with open(u2s(path), 'wb') as f:
                                _libarchive.archive_read_data_into_fd(
                                    a, f.fileno())
                    finally:
                        _libarchive.archive_entry_free(e)
                logging.info(u"END extract all '{0}'".format(archive_path))
        finally:
            archive.close()