Ejemplo n.º 1
0
 def readpath(self, f):
     '''Write current archive entry contents to file. f can be a file-like object or
     a path.'''
     if isinstance(f, basestring):
         basedir = os.path.basename(f)
         if not os.path.exists(basedir):
             os.makedirs(basedir)
         f = file(f, 'w')
     return _libarchive.archive_read_data_into_fd(self._a, f.fileno())
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()