Download and unzip an archive and extract the underlying fname
772584
def urlextract(url, fname):
    
    with urlopen(url) as f:
        data = io.BytesIO(f.read())
    with zipfile.ZipFile(data) as z:
        try:
            return z.open(fname)
        except KeyError:
            # for instance the ShakeMap ci3031111 has inside a file
            # data/verified_atlas2.0/reviewed/19920628115739/output/
            # uncertainty.xml
            # instead of just uncertainty.xml
            zinfo = z.filelist[0]
            if zinfo.filename.endswith(fname):
                return z.open(zinfo)
            else:
                raise
1001641
def extract_cab (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    cmdlist = [cmd, '-d', outdir]
    if verbosity > 0:
        cmdlist.append('-v')
    cmdlist.append(archive)
    return cmdlist
750319
def download_archive(self, archive):
        
        logger.info('Downloading archive %s', archive.id)
        r = self.session.get(self.server_url + 'downloadarquivo/{}'.format(archive.id))
        r.raise_for_status()

        # open the archive
        archive_stream = io.BytesIO(r.content)
        if is_rarfile(archive_stream):
            logger.debug('Identified rar archive')
            archive.content = RarFile(archive_stream)
        elif is_zipfile(archive_stream):
            logger.debug('Identified zip archive')
            archive.content = ZipFile(archive_stream)
        else:
            raise ValueError('Not a valid archive')
120131
def unzip(filepath, output_path):
    
    filename = os.path.split(filepath)[1]
    (name, extension) = os.path.splitext(filename)
    extension = extension[1:].lower()
    extension2 = os.path.splitext(name)[1][1:].lower()

    if extension not in ZIP_EXTENSIONS:
        raise Exception("Impossible to extract archive file %s" % filepath)

    extract_command = "unzip"
    output_args = "-d"
    if extension == 'bz2' and extension2 == 'tar':
        extract_command = "tar -xjf"
        output_args = "-C"
    elif extension == 'gz' and extension2 == 'tar':
        extract_command = "tar -xzf"
        output_args = "-C"
    elif extension == 'xz' and extension2 == 'tar':
        extract_command = "tar -xJf"
        output_args = "-C"
    elif extension == 'bz2':
        extract_command = "bunzip2 -dc "
        output_args = ">"
        output_path = os.path.join(output_path, name)
    elif extension == 'rar':
        extract_command = "unrar x"
        output_args = ""
    elif extension == 'gz':
        extract_command = "gunzip"
        output_args = ""
    elif extension == 'tar':
        extract_command = "tar -xf"
        output_args = "-C"
    elif extension == 'tbz2':
        extract_command = "tar -xjf"
        output_args = "-C"
    elif extension == 'tgz':
        extract_command = "tar -xzf"
        output_args = "-C"
    elif extension == 'zip':
        extract_command = "unzip"
        output_args = "-d"
    elif extension == 'Z':
        extract_command = "uncompress"
        output_args = ""
    elif extension == '7z':
        extract_command = "7z x"
        output_args = ""
    elif extension == 'xz':
        extract_command = "unxz"
        output_args = ""
    elif extension == 'ace':
        extract_command = "unace"
        output_args = ""
    elif extension == 'iso':
        extract_command = "7z x"
        output_args = ""
    elif extension == 'arj':
        extract_command = "7z x"
        output_args = ""

    command = """%(extract_command)s "%(filepath)s" %(output_args)s "%(output_folder)s" """
    params = {
        'extract_command': extract_command,
        'filepath': filepath,
        'output_folder': output_path,
        'output_args': output_args,
    }
    result = os.system(command % params)
    return result
1031834
def _extract_archive(archive, verbosity=0, interactive=True, outdir=None,
                     program=None, format=None, compression=None):
    
    if format is None:
        format, compression = get_archive_format(archive)
    check_archive_format(format, compression)
    program = find_archive_program(format, 'extract', program=program)
    check_program_compression(archive, 'extract', program, compression)
    get_archive_cmdlist = get_archive_cmdlist_func(program, 'extract', format)
    if outdir is None:
        outdir = util.tmpdir(dir=".")
        do_cleanup_outdir = True
    else:
        do_cleanup_outdir = False
    try:
        cmdlist = get_archive_cmdlist(archive, compression, program, verbosity, interactive, outdir)
        if cmdlist:
            # an empty command list means the get_archive_cmdlist() function
            # already handled the command (eg. when it's a builtin Python
            # function)
            run_archive_cmdlist(cmdlist, verbosity=verbosity)
        if do_cleanup_outdir:
            target, msg = cleanup_outdir(outdir, archive)
        else:
            target, msg = outdir, "`%s'" % outdir
        if verbosity >= 0:
            util.log_info("... %s extracted to %s." % (archive, msg))
        return target
    finally:
        # try to remove an empty temporary output directory
        if do_cleanup_outdir:
            try:
                os.rmdir(outdir)
            except OSError:
                pass
706993
def extract_cpio (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    cmdlist = [util.shell_quote(cmd), '--extract', '--make-directories',
        '--preserve-modification-time']
    if sys.platform.startswith('linux') and not cmd.endswith('bsdcpio'):
        cmdlist.extend(['--no-absolute-filenames',
        '--force-local', '--nonmatching', r'"*\.\.*"'])
    if verbosity > 1:
        cmdlist.append('-v')
    cmdlist.extend(['<', util.shell_quote(os.path.abspath(archive))])
    return (cmdlist, {'cwd': outdir, 'shell': True})
881274
def extract_zip(self, suffix, path='.'):
        
        zip_fd, zip_fn = tempfile.mkstemp(prefix='fuzzfetch-', suffix='.zip')
        os.close(zip_fd)
        try:
            _download_url(self.artifact_url(suffix), zip_fn)
            LOG.info('.. extracting')
            with zipfile.ZipFile(zip_fn) as zip_fp:
                for info in zip_fp.infolist():
                    _extract_file(zip_fp, info, path)
        finally:
            os.unlink(zip_fn)
1260217
def untar_data(url:str, fname:PathOrStr=None, dest:PathOrStr=None, data=True, force_download=False) -> Path:
    ""
    dest = url2path(url, data) if dest is None else Path(dest)/url2name(url)
    fname = Path(ifnone(fname, _url2tgz(url, data)))
    if force_download or (fname.exists() and url in _checks and _check_file(fname) != _checks[url]):
        print(f"A new version of the {'dataset' if data else 'model'} is available.")
        if fname.exists(): os.remove(fname)
        if dest.exists(): shutil.rmtree(dest)
    if not dest.exists():
        fname = download_data(url, fname=fname, data=data)
        if url in _checks:
            assert _check_file(fname) == _checks[url], f"Downloaded file {fname} does not match checksum expected! Remove that file from {Config().data_archive_path()} and try your code again."
        tarfile.open(fname, 'r:gz').extractall(dest.parent)
    return dest
1039760
def _extract_local_archive(working_dir, cleanup_functions, env_name, local_archive):
    
    with zipfile.ZipFile(local_archive) as z:
        z.extractall(working_dir)
        archive_filenames = z.namelist()

    root_elements = {m.split(posixpath.sep, 1)[0] for m in archive_filenames}
    abs_archive_filenames = [os.path.abspath(os.path.join(working_dir, f)) for f in root_elements]

    def cleanup():
        for fn in abs_archive_filenames:
            if os.path.isdir(fn):
                shutil.rmtree(fn)
            else:
                os.unlink(fn)

    cleanup_functions.append(cleanup)
    env_dir = os.path.join(working_dir, env_name)
    # Because of a python deficiency (Issue15795), the execute bits aren't
    # preserved when the zip file is unzipped. Need to add them back here.
    _fix_permissions(env_dir)
    return env_dir
1333797
def extract_ace (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    cmdlist = [cmd, 'x']
    if not outdir.endswith('/'):
        outdir += '/'
    cmdlist.extend([archive, outdir])
    return cmdlist
1144356
def fetch_archive(savedir, url, fname, force=False):
    """"""
    dest = pjoin(savedir, fname)
    if os.path.exists(dest) and not force:
        info("already have %s" % fname)
        return dest
    info("fetching %s into %s" % (url, savedir))
    if not os.path.exists(savedir):
        os.makedirs(savedir)
    req = urlopen(url)
    with open(dest, 'wb') as f:
        f.write(req.read())
    return dest
1277002
def extract_flac (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    outfile = util.get_single_outfile(outdir, archive, extension=".wav")
    cmdlist = [cmd, '--decode', archive, '--output-name', outfile]
    return cmdlist
944154
def load_data(fname, **kwargs):
    """"""
    fname = get_file(fname)
    if fname.suffix == ".lzma":
        return load_tar_lzma_data(fname)
    elif fname.suffix == ".zip":
        return load_zip_data(fname, **kwargs)
435940
def download(ctx, archive_name, filepath, version):
    '''
    
    '''

    _generate_api(ctx)
    var = ctx.obj.api.get_archive(archive_name)

    if version is None:
        version = var.get_default_version()

    var.download(filepath, version=version)

    archstr = var.archive_name +\
        '' if (not var.versioned) else ' v{}'.format(version)

    click.echo('downloaded{} to {}'.format(archstr, filepath))
596617
def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
    

    if not zipfile.is_zipfile(filename):
        raise UnrecognizedFormat("%s is not a zip file" % (filename,))

    with zipfile.ZipFile(filename) as z:
        for info in z.infolist():
            name = info.filename

            # don't extract absolute paths or ones with .. in them
            if name.startswith('/') or '..' in name.split('/'):
                continue

            target = os.path.join(extract_dir, *name.split('/'))
            target = progress_filter(name, target)
            if not target:
                continue
            if name.endswith('/'):
                # directory
                ensure_directory(target)
            else:
                # file
                ensure_directory(target)
                data = z.read(info.filename)
                with open(target, 'wb') as f:
                    f.write(data)
            unix_attributes = info.external_attr >> 16
            if unix_attributes:
                os.chmod(target, unix_attributes)
687041
def extract_gzip (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    targetname = util.get_single_outfile(outdir, archive)
    try:
        with gzip.GzipFile(archive) as gzipfile:
            with open(targetname, 'wb') as targetfile:
                data = gzipfile.read(READ_SIZE_BYTES)
                while data:
                    targetfile.write(data)
                    data = gzipfile.read(READ_SIZE_BYTES)
    except Exception as err:
        msg = "error extracting %s to %s: %s" % (archive, targetname, err)
        raise util.PatoolError(msg)
    return None
172562
def unzip_file(filename):
    """'"""

    if filename.endswith('bz2'):
        bz2file = bz2.BZ2File(filename)
        fdn, tmpfilepath = tempfile.mkstemp()
        with closing(os.fdopen(fdn, 'wb')) as ofpt:
            try:
                ofpt.write(bz2file.read())
            except IOError:
                import traceback
                traceback.print_exc()
                LOGGER.info("Failed to read bzipped file %s", str(filename))
                os.remove(tmpfilepath)
                return None

        return tmpfilepath

    return None