Extract each archive from the list of filenames.
    Normal files stay untouched.
    Add all files to the Recent Files.
    :type file_names: list of str
    :type temp_dir: str
    :rtype: list of str
7673
def uncompress_archives(file_names, temp_dir):
    
    result = []
    for filename in file_names:
        if filename.endswith(".tar") or filename.endswith(".tar.gz") or filename.endswith(".tar.bz2"):
            obj = tarfile.open(filename, "r")
            extracted_file_names = []
            for j, member in enumerate(obj.getmembers()):
                obj.extract(member, temp_dir)
                extracted_filename = os.path.join(temp_dir, obj.getnames()[j])
                extracted_file_names.append(extracted_filename)
                archives[extracted_filename] = filename
            result.extend(extracted_file_names[:])
        elif filename.endswith(".zip"):
            obj = zipfile.ZipFile(filename)
            extracted_file_names = []
            for j, info in enumerate(obj.infolist()):
                obj.extract(info, path=temp_dir)
                extracted_filename = os.path.join(temp_dir, obj.namelist()[j])
                extracted_file_names.append(extracted_filename)
                archives[extracted_filename] = filename
            result.extend(extracted_file_names[:])
        else:
            result.append(filename)

    return result
1327592
def zip_many_files(list_of_abspath, dst):
    
    base_dir = os.getcwd()

    with ZipFile(dst, "w") as f:
        for abspath in list_of_abspath:
            dirname, basename = os.path.split(abspath)
            os.chdir(dirname)
            f.write(basename)

    os.chdir(base_dir)
910012
def for_each_file(base_dir, func):
    

    for dir_path, _, file_names in os.walk(base_dir):
        for filename in file_names:
            func(os.path.join(dir_path, filename))
1290188
def extract_archive(archive, verbosity=0, outdir=None, program=None, interactive=True):
    """"""
    util.check_existing_filename(archive)
    if verbosity >= 0:
        util.log_info("Extracting %s ..." % archive)
    return _extract_archive(archive, verbosity=verbosity, interactive=interactive, outdir=outdir, program=program)
1018794
def _temporary_filenames(total):
    """"""
    temp_files = [_get_temporary_filename('optimage-') for i in range(total)]
    yield temp_files
    for temp_file in temp_files:
        try:
            os.remove(temp_file)
        except OSError:
            # Continue in case we could not remove the file. One reason is that
            # the fail was never created.
            pass
121142
def zipfiles(fnames, archive, mode='w', log=lambda msg: None, cleanup=False):
    
    prefix = len(os.path.commonprefix([os.path.dirname(f) for f in fnames]))
    with zipfile.ZipFile(
            archive, mode, zipfile.ZIP_DEFLATED, allowZip64=True) as z:
        for f in fnames:
            log('Archiving %s' % f)
            z.write(f, f[prefix:])
            if cleanup:  # remove the zipped file
                os.remove(f)
    log('Generated %s' % archive)
    return archive
745886
def _extract_to_tempdir(archive_filename):
    """"""
    if not os.path.exists(archive_filename):
        raise Exception("Archive '%s' does not exist" % (archive_filename))

    tempdir = tempfile.mkdtemp(prefix="metaextract_")
    current_cwd = os.getcwd()
    try:
        if tarfile.is_tarfile(archive_filename):
            with tarfile.open(archive_filename) as f:
                f.extractall(tempdir)
        elif zipfile.is_zipfile(archive_filename):
            with zipfile.ZipFile(archive_filename) as f:
                f.extractall(tempdir)
        else:
            raise Exception("Can not extract '%s'. "
                            "Not a tar or zip file" % archive_filename)
        os.chdir(tempdir)
        yield tempdir
    finally:
        os.chdir(current_cwd)
        shutil.rmtree(tempdir)
974776
def extract_files(files):
    """"""
    expanded_files = []
    legal_extensions = [".md", ".txt", ".rtf", ".html", ".tex", ".markdown"]

    for f in files:
        # If it's a directory, recursively walk through it and find the files.
        if os.path.isdir(f):
            for dir_, _, filenames in os.walk(f):
                for filename in filenames:
                    fn, file_extension = os.path.splitext(filename)
                    if file_extension in legal_extensions:
                        joined_file = os.path.join(dir_, filename)
                        expanded_files.append(joined_file)

        # Otherwise add the file directly.
        else:
            expanded_files.append(f)

    return expanded_files
985270
def _tempDirectories(self):
        
        def _dirs(path, levels):
            if levels > 0:
                for subPath in os.listdir(path):
                    for i in _dirs(os.path.join(path, subPath), levels-1):
                        yield i
            else:
                yield path
        for tempDir in _dirs(self.tempFilesDir, self.levels):
            yield tempDir
334051
def archive_filenames(self):
        """"""
        try:
            return _bfd.archive_list_filenames(self._ptr)
        except TypeError, err:
            raise BfdException(err)
780251
def extractall(self, directory, auto_create_dir=False, patool_path=None):
        '''
        
        '''
        log.debug('extracting %s into %s (backend=%s)', self.filename, directory, self.backend)
        is_zipfile = zipfile.is_zipfile(self.filename)
        directory = _fullpath(directory)
        if not os.path.exists(self.filename):
            raise ValueError(
                'archive file does not exist:' + str(self.filename))
        if not os.path.exists(directory):
            if auto_create_dir:
                os.makedirs(directory)
            else:
                raise ValueError('directory does not exist:' + str(directory))

        if self.backend == 'auto':
            if is_zipfile:
                self.extractall_zipfile(directory)
            else:
                self.extractall_patool(directory, patool_path)

        if self.backend == 'zipfile':
            if not is_zipfile:
                raise ValueError('file is not zip file:' + str(self.filename))
            self.extractall_zipfile(directory)

        if self.backend == 'patool':
            self.extractall_patool(directory, patool_path)
164400
def list_archive(archive, verbosity=1, program=None, interactive=True):
    """"""
    # Set default verbosity to 1 since the listing output should be visible.
    util.check_existing_filename(archive)
    if verbosity >= 0:
        util.log_info("Listing %s ..." % archive)
    return _handle_archive(archive, 'list', verbosity=verbosity,
      interactive=interactive, program=program)
379657
def extract(path, to_path='', ext='', **kwargs):
    
    Archive(path, ext=ext).extract(to_path, **kwargs)
613204
def _all_filenames(self):
        """"""
        try:
            return [os.path.join(self.cache_dir, filename) for filename in
                    os.listdir(self.cache_dir)]
        except (FileNotFoundError, OSError):
            return []
761045
def archive_if_exists(filename):
    
    if os.path.exists(filename):
        current_time = datetime.datetime.now()
        dt_format = '%Y-%m-%dT%H:%M:%S%z'
        timestamp = current_time.strftime(dt_format)
        dst = filename + '_' + timestamp
        shutil.move(filename, dst)
256246
def list_files(dir_path, recursive=True):
    

    for root, dirs, files in os.walk(dir_path):
        file_list = [os.path.join(root, f) for f in files]
        if recursive:
            for dir in dirs:
                dir = os.path.join(root, dir)
                file_list.extend(list_files(dir, recursive=True))
        return file_list
419876
def dir_list(load):
    '''
    
    '''
    ret = set()
    files = file_list(load)
    for f in files:
        dirname = f
        while dirname:
            dirname = os.path.dirname(dirname)
            if dirname:
                ret.add(dirname)
    return list(ret)