当前位置: 首页>>代码示例>>Python>>正文


Python ZipFile.extract方法代码示例

本文整理汇总了Python中zipfile.ZipFile.extract方法的典型用法代码示例。如果您正苦于以下问题:Python ZipFile.extract方法的具体用法?Python ZipFile.extract怎么用?Python ZipFile.extract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zipfile.ZipFile的用法示例。


在下文中一共展示了ZipFile.extract方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: install

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
    def install(self, sid, data, fname):
        try:
            z = ZipFile(io.BytesIO(data))
        except zipfile.BadZipfile:
            showWarning(_("The download was corrupt. Please try again."))
            return

        name = os.path.splitext(fname)[0]

        # previously installed?
        meta = self.addonMeta(sid)
        base = self.addonsFolder(sid)
        if os.path.exists(base):
            self.backupUserFiles(sid)
            self.deleteAddon(sid)

        os.mkdir(base)
        self.restoreUserFiles(sid)

        # extract
        for n in z.namelist():
            if n.endswith("/"):
                # folder; ignore
                continue

            path = os.path.join(base, n)
            # skip existing user files
            if os.path.exists(path) and n.startswith("user_files/"):
                continue
            z.extract(n, base)

        # update metadata
        meta['name'] = name
        meta['mod'] = intTime()
        self.writeAddonMeta(sid, meta)
开发者ID:bbugyi200,项目名称:anki,代码行数:37,代码来源:addons.py

示例2: import_view

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def import_view(request):
    """
    Gets the existing declared parsers for the current project.
    This view handles only the file based import parsers.
    """
    choices = []
    choices_url = []
    render_dict = {}

    choices, choices_url, classes = discover_available_parsers()

    form = ImportDatasetFormWithFile(choices, prefix="with-file")
    form_without_file = ImportDatasetForm(
        choices_url, prefix="without-file")

    if request.method == 'POST':
        if 'upload-file' in request.POST:
            form = ImportDatasetFormWithFile(
                choices, request.POST, request.FILES, prefix="with-file")

            if form.is_valid():
                print(request.FILES)
                uploaded = request.FILES['with-file-zipfile']

                destination_dir, destination_file = create_tmp_destination(
                    uploaded.name)

                with open(destination_file, 'w+') as f:
                    f.write(uploaded.file.read())
                    zfile = ZipFile(f)
                    for name in zfile.namelist():
                        try:
                            zfile.extract(
                                name, os.path.dirname(os.path.realpath(f.name)))
                            if name.endswith('shp'):
                                parser = classes[int(form['parser'].value())]
                                import_datas.delay(
                                    '/'.join((destination_dir, name)),
                                    parser.__name__, parser.__module__
                                )
                                continue
                        except Exception:
                            raise

        if 'import-web' in request.POST:
            form_without_file = ImportDatasetForm(
                choices_url, request.POST, prefix="without-file")

            if form_without_file.is_valid():
                parser = classes[int(form_without_file['parser'].value())]
                import_datas_from_web.delay(
                    parser.__name__, parser.__module__
                )

    # Hide second form if parser has no web based imports.
    render_dict['form'] = form
    if choices_url:
        render_dict['form_without_file'] = form_without_file

    return render(request, 'common/import_dataset.html', render_dict)
开发者ID:brianpessers,项目名称:Geotrek,代码行数:62,代码来源:views.py

示例3: extract_subfiles

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def extract_subfiles(source_path, dest_path, verbose=False):
    if os.path.isdir(source_path):
        for dirpath, dirnames, filenames in os.walk(source_path):
            relpath = os.path.relpath(dirpath, source_path)
            new_dir_path = os.path.join(dest_path, relpath)
            if not os.path.isdir(new_dir_path):
                os.mkdir(new_dir_path)
            for filename in filenames:
                try:
                    source_file_path = os.path.join(dirpath, filename)
                    relpath = os.path.relpath(source_file_path, source_path)
                    dest_file_path = os.path.join(dest_path, relpath)
                    print dest_file_path
                    if dest_file_path.endswith('.cod'):
                        zip = ZipFile(source_file_path)
                        for info in zip.infolist():
                            if verbose:
                                print '    %s (extracted)' % info.filename
                            dest_unzip_path = os.path.split(dest_file_path)[0] 
                            if not os.path.realpath(os.path.join(dest_unzip_path, info.filename)).startswith(os.path.realpath(dest_unzip_path)):
                                raise(Exception('Security exception: zip file %s attempted to extract to a non-local location' % info.filename))
                            zip.extract(info, path = dest_unzip_path)
                    else:
                        shutil.copyfile(source_file_path, dest_file_path)
                except Exception, e:
                    if str(e) == 'File is not a zip file':
                        # this is a cod file or some other file
                        shutil.copyfile(source_file_path, dest_file_path)
                    else:
                        if verbose:
                            print >>sys.stderr, 'Error:',
                            print >>sys.stderr, str(e)
                        raise(e)
开发者ID:bkerler,项目名称:cod2jar,代码行数:35,代码来源:cod_extract.py

示例4: _loadShpList

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
	def _loadShpList(self):
		"""
		Loads (extracts) a batch of shapefiles from an archive. Only those that match
		the given pattern are extracted.
		"""
		self.shpList = None
		shpFiles = list()
		tmpFiles = list()
		shpmatch = re.compile('\.shp$')
		searchRe = re.compile(self.pattern)
		# Iterate through the list of archives.
		for i in range(self.zipIndex, len(self.zipList)):
			f = self.zipList[i]
			try:
				# Open the zip file and read the entries.
				z = ZipFile(f)
				entries = z.namelist()
				# Iterate through the entries. If an entry matches the search string, extract
				# it to the tmp folder.
				for e in entries:
					if searchRe.search(e):
						z.extract(e, self.tmpDir)
						# Add to the list of extracted files.
						tmpFiles.append(e)
						# If the entry is a shapefile, add it to the shp list.
						if shpmatch.search(e):
							shpFiles.append(e)
			except Exception, e:
				print "Failed to open {0}: {1}.".format(f, e.__str__())
			finally:
开发者ID:rskelly,项目名称:canvec,代码行数:32,代码来源:canvec.py

示例5: unzip

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def unzip(filename):
    z = ZipFile(filename)
    names = z.namelist()
    for path in names:
        if path.startswith('__MACOSX/'):
            continue

        base, name = os.path.split(path)

        if name.startswith('._') and\
            '%s/' % name.replace('._', '', 1) in names:
            continue

        double = os.path.join('__MACOSX', base, '._' + name)
        if double in names:
            print '=> %s.bin' % path

            info = z.getinfo(path)

            bin = MacBinary(name)
            bin.data = z.open(path, 'r').read()
            bin.res = z.open(double, 'r').read()

            modified = datetime.datetime(*info.date_time)
            bin.modified = time.mktime(modified.timetuple())
            bin.created = time.time()

            if not os.path.exists(base):
                os.makedirs(base)

            with open('%s.bin' % path.rstrip('\r'), 'wb') as f:
                f.write(bin.encode())
        else:
            print '-> %s' % path
            z.extract(path)
开发者ID:adamcowan,项目名称:meta,代码行数:37,代码来源:macbinary.py

示例6: download

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def download(url, dirname, filename):
    if int(__addon__.getSetting("sub_language1")) == 0:
        return {}

    cache_path = os.path.join(CACHE_DIR, "temp.zip")
    try:
        urlretrieve(url, cache_path)
        z = ZipFile(cache_path)
    except:
        return None

    path = None
    for f in z.namelist():
        ext = os.path.splitext(f)[1]
        if not ext in _subtitles_formats:
            continue
        path = os.path.join(dirname, filename + ext)
        z.extract(f, CACHE_DIR)
        if os.path.isfile(path):
            os.unlink(path)
        os.rename(os.path.join(CACHE_DIR, f), path)
        break

    z.close()
    os.unlink(cache_path)
    return path
开发者ID:revho6,项目名称:KODI-Popcorn-Time,代码行数:28,代码来源:subtitle_yify.py

示例7: install

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
    def install(self, sid, data, fname):
        try:
            z = ZipFile(io.BytesIO(data))
        except zipfile.BadZipfile:
            showWarning(_("The download was corrupt. Please try again."))
            return

        name = os.path.splitext(fname)[0]

        # previously installed?
        meta = self.addonMeta(sid)
        base = self.addonsFolder(sid)
        if os.path.exists(base):
            self.deleteAddon(sid)

        # extract
        os.mkdir(base)
        for n in z.namelist():
            if n.endswith("/"):
                # folder; ignore
                continue
            # write
            z.extract(n, base)

        # update metadata
        meta['name'] = name
        meta['mod'] = intTime()
        self.writeAddonMeta(sid, meta)
开发者ID:jdennis,项目名称:anki,代码行数:30,代码来源:addons.py

示例8: base_scrape_alexa

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def base_scrape_alexa(url, browser_args, out_name, add_http):
	b = Browser(name, **browser_args)
	r = b.go(url)

	zip_file = StringIO(r.content)
	z = ZipFile(zip_file, 'r')
	csv_file = 'top-1m.csv'
	z.extract(csv_file, out_dir)

	file_path = path.join(out_dir, csv_file)
	lines = strip_open(file_path)

	out_path = path.join(out_dir, out_name)
	if add_http:
		os.remove(file_path)
		with open(out_path, 'w+') as f:
			for line in lines:
				print >> f, 'http://' + line.split(',')[1]
	else:
		try:
			os.rename(file_path, out_path)
		except Exception as e:
			pass

	return len(lines)
开发者ID:gzzo,项目名称:arachne,代码行数:27,代码来源:scrape_alexa.py

示例9: extract_from_zip

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def extract_from_zip(filename):
    zp = ZipFile(filename, 'r')
    namelist = zp.namelist()
    desired_files = [x for x in namelist if 'Award' in x or 'Summary' in x]
    for file in desired_files:
        if not os.path.exists(file):
            zp.extract(file)
开发者ID:tweyter,项目名称:ScheduleStats,代码行数:9,代码来源:convert_to_text.py

示例10: download_flights

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def download_flights(start_date, end_date):
    pkl_path = 'data/processed/flights/flights{0}.{1}-{2}.{3}.pkl'.format(start_date.year, start_date.month,
                                                                          end_date.year,
                                                                          end_date.month)
    if Path(pkl_path).exists():
        return pd.read_pickle(pkl_path)
    files, failed = download_range(r'http://tsdata.bts.gov/PREZIP/On_Time_On_Time_Performance_%Y_%-m.zip',
                                   'data/raw/flights', start_date, end_date)
    dest_path = Path('data/unpacked/flights')
    dest_path.mkdir(parents=True, exist_ok=True)

    frames = []

    for zip_file in files:
        zf = ZipFile(str(zip_file))
        csv_name = zip_file.with_suffix('.csv').name
        csv_file = dest_path / csv_name
        if not csv_file.exists():
            print("Extracting", csv_name)
            zf.extract(csv_name, str(dest_path))
            print("...done")
        else:
            print(csv_name, "already extracted")
        df = flightdata.read_csv(str(csv_file))
        frames.append(df)

    all_flights = merge_frames(frames, ignore_index=True)
    all_flights.to_pickle(pkl_path)
    return all_flights
开发者ID:nasi-famnit,项目名称:HOur-flight,代码行数:31,代码来源:downloader.py

示例11: addCBZFiles

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
    def addCBZFiles(self, filenames):
        directories = []
        tempDir = tempfile.gettempdir()
        filenames.sort()

        filenamesListed = []
        for i in xrange(0, self.listWidgetFiles.count()):
            filenamesListed.append(self.listWidgetFiles.item(i).text())

        for filename in filenames:
            folderName = os.path.splitext(basename(str(filename)))[0]
            path = tempDir + "/" + folderName + "/"
            cbzFile = ZipFile(str(filename))
            for f in cbzFile.namelist():
                if f.endswith('/'):
                    try:
                        os.makedirs(path + f)
                    except:
                        pass  # the dir exists so we are going to extract the images only.
                else:
                    cbzFile.extract(f, path)
            if os.path.isdir(unicode(path)):  # Add the directories
                directories.append(path)
        
        self.addImageDirs(directories)  # Add the files
开发者ID:clizana,项目名称:mangle,代码行数:27,代码来源:book.py

示例12: dump_files

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def dump_files(zipfile: ZipFile):
    """Dump packed files to a location.
    """
    dump_folder = CONF['packfile_dump', '']
    if not dump_folder:
        return

    dump_folder = os.path.abspath(dump_folder)

    # Delete files in the folder, but don't delete the folder itself.
    try:
        files = os.listdir(dump_folder)
    except FileNotFoundError:
        return

    for name in files:
        name = os.path.join(dump_folder, name)
        if os.path.isdir(name):
            try:
                shutil.rmtree(name)
            except OSError:
                # It's possible to fail here, if the window is open elsewhere.
                # If so, just skip removal and fill the folder.
                pass
        else:
            os.remove(name)

    for zipinfo in zipfile.infolist():
        zipfile.extract(zipinfo, dump_folder)
开发者ID:BenVlodgi,项目名称:BEE2.4,代码行数:31,代码来源:vrad.py

示例13: unzip

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def unzip(filename, match_dir=False, destdir=None):
    """
    Extract all files from a zip archive
    filename: The path to the zip file
    match_dir: If True all files in the zip must be contained in a subdirectory
      named after the archive file with extension removed
    destdir: Extract the zip into this directory, default current directory

    return: If match_dir is True then returns the subdirectory (including
      destdir), otherwise returns destdir or '.'
    """
    if not destdir:
        destdir = '.'

    z = ZipFile(filename)
    unzipped = '.'

    if match_dir:
        if not filename.endswith('.zip'):
            raise FileException('Expected .zip file extension', filename)
        unzipped = os.path.basename(filename)[:-4]
        check_extracted_paths(z.namelist(), unzipped)
    else:
        check_extracted_paths(z.namelist())

    # File permissions, see
    # http://stackoverflow.com/a/6297838
    # http://stackoverflow.com/a/3015466
    for info in z.infolist():
        log.debug('Extracting %s to %s', info.filename, destdir)
        z.extract(info, destdir)
        os.chmod(os.path.join(destdir, info.filename),
                 info.external_attr >> 16 & 4095)

    return os.path.join(destdir, unzipped)
开发者ID:snoopycrimecop,项目名称:omego,代码行数:37,代码来源:fileutils.py

示例14: census2000

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
        def census2000(summary_file_num, filenum):
            base_url = 'http://www2.census.gov/census_2000/datasets/'
            path = os.path.join( 
                'Summary_File_%s' % summary_file_num, state_name.replace(' ', '_'), 
                '%s000%02d_uf%s.zip' % (state_abbr, filenum, summary_file_num)
            )
            reader = remote_fallback_reader(
                '../data/census_2000/datasets',
                base_url,
                path
            )
            
            z = ZipFile(reader)
            n = z.namelist()
            z.extract(n[0], '/tmp/')
            
            filename = '/tmp/%s000%02d.uf%s' % (state_abbr, filenum, summary_file_num)

            # count the number of columns, to determine how many columns to fill
            firstline = open(filename).readline()
            cols = ['fileid', 'stusab', 'chariter', 'cifsn', 'logrecno']
            cols.extend(map(
                lambda c: 'col%s' % str(c+1),
                range(firstline.count(',')+1-5) # subtract 5 to account for the 5 geo header columns
            ))
            cursor.copy_from(open(filename), 'census_row', sep=',',
                columns=cols)
            os.unlink(filename)
开发者ID:peterlandry,项目名称:django-census,代码行数:30,代码来源:load_census.py

示例15: download_custom_reports

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extract [as 别名]
def download_custom_reports(domain, report_id, report_name):
    """Downloads custom report data csv zip export from commcare.
    The extracted csv is placed in data/DOMAIN/latest/Pregnancy Visit.csv.
    :domain - commcare domain
    :report_id - custom report id for report
    """
    if not report_id:
        raise Exception(u"Please ensure the custom report id"
                        u" is configured in CUSTOM_REPORTS settings.")
    url_path = "/a/%(domain)s/reports/export/custom/%(id)s/download/"\
        "?format=csv" % {'domain': domain, 'id': report_id}
    f = download_from_commcare(url_path)
    try:
        z = ZipFile(f, 'r')
    except BadZipfile:
        print f.read()
    else:
        zdst = os.path.join('data', domain, 'latest')
        dst = os.path.join(zdst, report_name)
        if z.NameToInfo.keys():
            filename = z.NameToInfo.keys()[0]
            src = os.path.join(zdst, filename)
            z.extract(z.NameToInfo[filename], zdst)
            if os.path.exists(src):
                os.rename(src, dst)
                print u"Successfully downloaded %s" % report_name
开发者ID:SEL-Columbia,项目名称:mvpqa,代码行数:28,代码来源:pull_data.py


注:本文中的zipfile.ZipFile.extract方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。