本文整理汇总了Python中zipfile.ZipFile.getnames方法的典型用法代码示例。如果您正苦于以下问题:Python ZipFile.getnames方法的具体用法?Python ZipFile.getnames怎么用?Python ZipFile.getnames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipfile.ZipFile
的用法示例。
在下文中一共展示了ZipFile.getnames方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _unpack
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import getnames [as 别名]
def _unpack(archive_file):
"""Unpacks and extracts files from an archive
This function will unpack and extra files from the file archive_file. It
will return the directory to which the files were unpacked.
An AttributeError is raised when the archive is not supported (when the
name does not end with '.zip' or '.tar.gz')
Returns string.
"""
logger.info("Unpacking archive '{0}'".format(archive_file))
if archive_file.endswith('.zip'):
archive = ZipFile(archive_file)
rootdir = archive.namelist()[0]
elif archive_file.endswith('.tar.gz'):
archive = tarfile.open(archive_file)
rootdir = archive.getnames()[0]
else:
raise AttributeError("Unsupported archive. Can't unpack.")
logger.info("Archive root folder is '{0}'".format(rootdir))
try:
shutil.rmtree(rootdir)
except OSError:
pass
logger.info("Extracting to '{0}'".format(rootdir))
archive.extractall()
archive.close()
return rootdir
示例2: unpack_archive
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import getnames [as 别名]
def unpack_archive(path):
tmp_dir = mkdtemp()
file_extension = get_extention(path)
if file_extension in ['.zip', '.cbz']:
archive = ZipFile(path, 'r')
namelist = archive.namelist()
elif file_extension in [".tar", ".cbt"]:
archive = tarfile.TarFile(path, 'r')
namelist = archive.getnames()
for member in namelist:
filename = os.path.basename(member)
# if directory break the loop
if not filename:
continue
# extract each file
source = archive.open(member)
target = open(os.path.join(path, filename), "wb")
with source, target:
copyfileobj(source, target)
return tmp_dir
示例3: compute_fields
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import getnames [as 别名]
def compute_fields(self):
"""Other keyword args get passed in as a matter of course, like BBOX, time, and elevation, but this basic driver
ignores them"""
super(SpatialiteDriver, self).compute_fields()
if not hasattr(self, "src_ext") and self.resource.resource_file :
self.src_ext = self.resource.resource_file.split('.')[-1]
# convert any other kind of file to spatialite. this way the sqlite driver can be used with any OGR compatible
# file
if self.src_ext.endswith('zip'):
archive = ZipFile(self.cached_basename + self.src_ext)
projection_found = False
for name in archive.namelist():
xtn = name.split('.')[-1].lower()
if xtn in {'shp', 'shx', 'dbf', 'prj'} and "__MACOSX" not in name:
projection_found = projection_found or xtn == 'prj'
with open(self.cached_basename + '.' + xtn, 'wb') as fout:
with archive.open(name) as fin:
chunk = fin.read(65536)
while chunk:
fout.write(chunk)
chunk = fin.read(65536)
if not projection_found:
with open(self.cached_basename + '.prj', 'w') as f:
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
f.write(srs.ExportToWkt())
in_filename = self.get_filename('shp')
out_filename = self.get_filename('sqlite')
if os.path.exists(out_filename):
os.unlink(out_filename)
sh.ogr2ogr(
'-skipfailures',
'-t_srs', 'epsg:3857',
'-f', 'SQLite',
'-dsco', 'SPATIALITE=YES',
out_filename, in_filename
)
elif self.src_ext.endswith('gz'):
archive = TarFile(self.cached_basename + self.src_ext)
projection_found = False
for name in archive.getnames():
xtn = name.split('.')[-1].lower()
if xtn in {'shp', 'shx', 'dbf', 'prj'} and "__MACOSX" not in name:
projection_found = projection_found or xtn == 'prj'
with open(self.cached_basename + '.' + xtn, 'wb') as fout:
with archive.open(name) as fin:
chunk = fin.read(65536)
while chunk:
fout.write(chunk)
chunk = fin.read(65536)
if not projection_found:
with open(self.cached_basename + '.prj', 'w') as f:
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
f.write(srs.ExportToWkt())
in_filename = self.get_filename('shp')
out_filename = self.get_filename('sqlite')
if os.path.exists(out_filename):
os.unlink(out_filename)
sh.ogr2ogr(
'-skipfailures',
'-t_srs', 'epsg:3857',
'-f', 'SQLite',
'-dsco', 'SPATIALITE=YES',
out_filename, in_filename
)
elif not self.src_ext.endswith('sqlite'):
in_filename = self.get_filename(self.src_ext)
out_filename = self.get_filename('sqlite')
if os.path.exists(out_filename):
os.unlink(out_filename)
sh.ogr2ogr(
'-skipfailures',
'-t_srs', 'epsg:3857',
'-f', 'SQLite',
'-dsco', 'SPATIALITE=YES',
out_filename, in_filename
)
connection = self._connection()
table, geometry_field, _, _, srid, _ = connection.execute("select * from geometry_columns").fetchone() # grab the first layer with a geometry
self._srid = srid
dataframe = self.get_filename('dfx')
if os.path.exists(dataframe):
os.unlink(dataframe)
c = connection.cursor()
c.execute("select AsText(Extent(w.{geom_field})) from {table} as w".format(
#.........这里部分代码省略.........
示例4: test_zip_content
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import getnames [as 别名]
#.........这里部分代码省略.........
archive_base_name = archive_name
archive_base_name = archive_base_name.replace(".zip", "")
archive_base_name = archive_base_name.replace(".tar.gz", "")
expected_file_list = ['README.md',
'license.txt',
'S130_license_agreement.pdf',
'driver/examples/Makefile.common',
'driver/examples/stdbool.h',
'driver/examples/advertising/main.c',
'driver/examples/advertising/gcc/Makefile',
'driver/examples/heart_rate_collector/main.c',
'driver/examples/heart_rate_collector/gcc/Makefile',
'driver/examples/heart_rate_monitor/main.c',
'driver/examples/heart_rate_monitor/gcc/Makefile',
'driver/examples/heart_rate_relay/main.c',
'driver/examples/heart_rate_relay/gcc/Makefile',
'driver/examples/multi_link/main.c',
'driver/examples/multi_link/gcc/Makefile',
'driver/include/ble.h',
'driver/include/ble_err.h',
'driver/include/ble_gap.h',
'driver/include/ble_gatt.h',
'driver/include/ble_gattc.h',
'driver/include/ble_gatts.h',
'driver/include/ble_hci.h',
'driver/include/ble_l2cap.h',
'driver/include/ble_ranges.h',
'driver/include/ble_types.h',
'driver/include/nrf_error.h',
'driver/include/nrf_svc.h',
'driver/include/sd_rpc.h',
'driver/lib/{0}s130_nrf51_ble_driver{1}'.format(dynamic_library_prefix, dynamic_library_suffix),
'firmware/connectivity_115k2_with_s130_1.0.0.hex',
'python/ble_driver_util.py',
'python/s130_nrf51_ble_driver.py',
'python/_s130_nrf51_ble_driver{0}'.format(python_bindings_suffix),
'python/examples/advertising/main.py',
'python/examples/heart_rate_monitor/main.py',
'python/examples/heart_rate_collector/main.py',
'python/examples/heart_rate_relay/main.py',
'python/examples/multi_link/main.py']
if platform.system() != 'Darwin':
expected_file_list.extend(['firmware/connectivity_1m_with_s130_1.0.0.hex'])
if platform.system() == 'Windows':
expected_file_list.extend(['driver/examples/advertising/msvc/advertising.vcxproj',
'driver/examples/heart_rate_collector/msvc/heart_rate_collector.vcxproj',
'driver/examples/heart_rate_monitor/msvc/heart_rate_monitor.vcxproj',
'driver/examples/heart_rate_relay/msvc/heart_rate_relay.vcxproj',
'driver/examples/multi_link/msvc/multi_link.vcxproj',
'driver/lib/s130_nrf51_ble_driver.lib'])
else:
expected_file_list.extend(['driver',
'driver/examples',
'driver/examples/advertising',
'driver/examples/advertising/gcc',
'driver/examples/heart_rate_collector',
'driver/examples/heart_rate_collector/gcc',
'driver/examples/heart_rate_monitor',
'driver/examples/heart_rate_monitor/gcc',
'driver/examples/heart_rate_relay',
'driver/examples/heart_rate_relay/gcc',
'driver/examples/multi_link',
'driver/examples/multi_link/gcc',
'driver/include',
'driver/lib',
'firmware',
'python',
'python/examples',
'python/examples/advertising',
'python/examples/heart_rate_collector',
'python/examples/heart_rate_monitor',
'python/examples/heart_rate_relay',
'python/examples/multi_link'])
expected_file_list = map(lambda x: archive_base_name + '/' + x, expected_file_list)
if not platform.system() == 'Windows':
expected_file_list.append(archive_base_name)
actual_file_list = []
if platform.system() == 'Windows':
archive = ZipFile(archive_path)
actual_file_list.extend(archive.namelist())
else:
archive = tarfile.open(archive_path)
actual_file_list.extend(archive.getnames())
expected_file_list = sorted(expected_file_list)
actual_file_list = sorted(actual_file_list)
self.assertEqual(actual_file_list, expected_file_list)
示例5: compute_fields
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import getnames [as 别名]
def compute_fields(self):
"""Other keyword args get passed in as a matter of course, like BBOX, time, and elevation, but this basic driver
ignores them"""
super(SpatialiteDriver, self).compute_fields()
if not hasattr(self, "src_ext") and self.resource.resource_file:
self.src_ext = self.resource.resource_file.name.split(".")[-1]
elif not hasattr(self, "src_ext"):
self.src_ext = "sqlite"
# convert any other kind of file to spatialite. this way the sqlite driver can be used with any OGR compatible
# file
if self.src_ext.endswith("zip"):
archive = ZipFile(self.cached_basename + self.src_ext)
projection_found = False
for name in archive.namelist():
xtn = name.split(".")[-1].lower()
if xtn in {"shp", "shx", "dbf", "prj"} and "__MACOSX" not in name:
projection_found = projection_found or xtn == "prj"
with open(self.cached_basename + "." + xtn, "wb") as fout:
with archive.open(name) as fin:
chunk = fin.read(1024768)
while chunk:
fout.write(chunk)
chunk = fin.read(1024768)
if not projection_found:
with open(self.cached_basename + ".prj", "w") as f:
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
f.write(srs.ExportToWkt())
in_filename = self.get_filename("shp")
out_filename = self.get_filename("sqlite")
if os.path.exists(out_filename):
os.unlink(out_filename)
# ogr2ogr -skipfailures -overwrite -f SQLite -dsco USE_SPATIALITE=YES -lco OVERWRITE=YES -dsco OGR_SQLITE_SYNCHRONOUS=OFF -gt 131072 -t_srs epsg:3857
sh.ogr2ogr(
"-explodecollections",
"-skipfailures",
"-overwrite",
"-gt",
"131072",
"-t_srs",
"epsg:3857",
"-f",
"SQLite",
"-dsco",
"SPATIALITE=YES",
out_filename,
in_filename,
)
self.resource.resource_file = File(open(out_filename), name=self.resource.slug.split("/")[-1] + ".sqlite")
elif self.src_ext.endswith("gz"):
archive = TarFile(self.cached_basename + self.src_ext)
projection_found = False
for name in archive.getnames():
xtn = name.split(".")[-1].lower()
if xtn in {"shp", "shx", "dbf", "prj"} and "__MACOSX" not in name:
projection_found = projection_found or xtn == "prj"
with open(self.cached_basename + "." + xtn, "wb") as fout:
with archive.open(name) as fin:
chunk = fin.read(65536)
while chunk:
fout.write(chunk)
chunk = fin.read(65536)
if not projection_found:
with open(self.cached_basename + ".prj", "w") as f:
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
f.write(srs.ExportToWkt())
in_filename = self.get_filename("shp")
out_filename = self.get_filename("sqlite")
if os.path.exists(out_filename):
os.unlink(out_filename)
sh.ogr2ogr(
"-explodecollections",
"-skipfailures",
"-overwrite",
"-gt",
"131072",
"-t_srs",
"epsg:3857",
"-f",
"SQLite",
"-dsco",
"SPATIALITE=YES",
out_filename,
in_filename,
)
self.resource.resource_file = File(open(out_filename), name=self.resource.slug.split("/")[-1] + ".sqlite")
elif not self.src_ext.endswith("sqlite"):
#.........这里部分代码省略.........