本文整理汇总了Python中lzma.LZMAFile方法的典型用法代码示例。如果您正苦于以下问题:Python lzma.LZMAFile方法的具体用法?Python lzma.LZMAFile怎么用?Python lzma.LZMAFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lzma
的用法示例。
在下文中一共展示了lzma.LZMAFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if mode not in ("r", "w"):
raise ValueError("mode must be 'r' or 'w'")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
raise ReadError("not an lzma file")
t._extfileobj = False
return t
# All *open() methods are registered here.
示例2: _write_fileobject
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def _write_fileobject(filename, compress=("zlib", 3)):
"""Return the right compressor file object in write mode."""
compressmethod = compress[0]
compresslevel = compress[1]
if compressmethod == "gzip":
return _buffered_write_file(BinaryGzipFile(filename, 'wb',
compresslevel=compresslevel))
elif compressmethod == "bz2" and bz2 is not None:
return _buffered_write_file(bz2.BZ2File(filename, 'wb',
compresslevel=compresslevel))
elif lzma is not None and compressmethod == "xz":
return _buffered_write_file(lzma.LZMAFile(filename, 'wb',
check=lzma.CHECK_NONE,
preset=compresslevel))
elif lzma is not None and compressmethod == "lzma":
return _buffered_write_file(lzma.LZMAFile(filename, 'wb',
preset=compresslevel,
format=lzma.FORMAT_ALONE))
else:
return _buffered_write_file(BinaryZlibFile(filename, 'wb',
compresslevel=compresslevel))
###############################################################################
# Joblib zlib compression file object definition
示例3: load_logos
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def load_logos(filename):
'''
Load logos from a geologos archive from <filename>
<filename> can be either a local path or a remote URL.
'''
if filename.startswith('http'):
log.info('Downloading GeoLogos bundle: %s', filename)
filename, _ = urlretrieve(filename, tmp.path('geologos.tar.xz'))
log.info('Extracting GeoLogos bundle')
with contextlib.closing(lzma.LZMAFile(filename)) as xz:
with tarfile.open(fileobj=xz, encoding='utf8') as tar:
tar.extractall(tmp.root, members=tar.getmembers())
log.info('Moving to the final location and cleaning up')
if os.path.exists(logos.root):
shutil.rmtree(logos.root)
shutil.move(tmp.path('logos'), logos.root)
log.info('Done')
示例4: compressOpen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def compressOpen(fn, mode='rb', compress_type=None):
if not compress_type:
# we are readonly and we don't give a compress_type - then guess based on the file extension
compress_type = fn.split('.')[-1]
if compress_type not in _available_compression:
compress_type = 'gz'
if compress_type == 'xz':
fh = lzma.LZMAFile(fn, mode)
if mode == 'w':
fh = Duck(write=lambda s, write=fh.write: s != '' and write(s),
close=fh.close)
return fh
elif compress_type == 'bz2':
return bz2.BZ2File(fn, mode)
elif compress_type == 'gz':
return _gzipOpen(fn, mode)
else:
raise MDError, "Unknown compression type %s" % compress_type
示例5: data_file
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def data_file(self):
"""Return the uncompressed raw CPIO data of the RPM archive."""
if self._data_file is None:
fileobj = _SubFile(self._fileobj, self.data_offset)
if self.headers["archive_compression"] == b"xz":
if not getattr(sys.modules[__name__], "lzma", False):
raise NoLZMAModuleError("lzma module not present")
self._data_file = lzma.LZMAFile(fileobj)
elif self.headers["archive_compression"] == b"zstd":
if not getattr(sys.modules[__name__], "zstandard", False):
raise NoZSTANDARDModuleError("zstandard module not present")
if not (sys.version_info.major >= 3 and sys.version_info.minor >= 5):
raise NoBytesIOError("Need io.BytesIO (Python >= 3.5)")
with zstandard.ZstdDecompressor().stream_reader(fileobj) as zstd_data:
self._data_file = io.BytesIO(zstd_data.read())
else:
self._data_file = gzip.GzipFile(fileobj=fileobj)
return self._data_file
示例6: _write_fileobject
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def _write_fileobject(filename, compress=("zlib", 3)):
"""Return the right compressor file object in write mode."""
compressmethod = compress[0]
compresslevel = compress[1]
if compressmethod == "gzip":
return _buffered_write_file(BinaryGzipFile(filename, 'wb',
compresslevel=compresslevel))
elif compressmethod == "bz2":
return _buffered_write_file(bz2.BZ2File(filename, 'wb',
compresslevel=compresslevel))
elif lzma is not None and compressmethod == "xz":
return _buffered_write_file(lzma.LZMAFile(filename, 'wb',
check=lzma.CHECK_NONE,
preset=compresslevel))
elif lzma is not None and compressmethod == "lzma":
return _buffered_write_file(lzma.LZMAFile(filename, 'wb',
preset=compresslevel,
format=lzma.FORMAT_ALONE))
else:
return _buffered_write_file(BinaryZlibFile(filename, 'wb',
compresslevel=compresslevel))
###############################################################################
# Joblib zlib compression file object definition
示例7: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if mode not in ("r", "w", "x"):
raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
if mode == 'r':
raise ReadError("not an lzma file")
raise
except:
fileobj.close()
raise
t._extfileobj = False
return t
# All *open() methods are registered here.
示例8: open
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def open(self):
return lzma.LZMAFile(self.f)
示例9: test_csv_compress
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def test_csv_compress(bucket, compression):
path = f"s3://{bucket}/test_csv_compress_{compression}/"
wr.s3.delete_objects(path=path)
df = get_df_csv()
if compression == "gzip":
buffer = BytesIO()
with gzip.GzipFile(mode="w", fileobj=buffer) as zipped_file:
df.to_csv(TextIOWrapper(zipped_file, "utf8"), index=False, header=None)
s3_resource = boto3.resource("s3")
s3_object = s3_resource.Object(bucket, f"test_csv_compress_{compression}/test.csv.gz")
s3_object.put(Body=buffer.getvalue())
file_path = f"s3://{bucket}/test_csv_compress_{compression}/test.csv.gz"
elif compression == "bz2":
buffer = BytesIO()
with bz2.BZ2File(mode="w", filename=buffer) as zipped_file:
df.to_csv(TextIOWrapper(zipped_file, "utf8"), index=False, header=None)
s3_resource = boto3.resource("s3")
s3_object = s3_resource.Object(bucket, f"test_csv_compress_{compression}/test.csv.bz2")
s3_object.put(Body=buffer.getvalue())
file_path = f"s3://{bucket}/test_csv_compress_{compression}/test.csv.bz2"
elif compression == "xz":
buffer = BytesIO()
with lzma.LZMAFile(mode="w", filename=buffer) as zipped_file:
df.to_csv(TextIOWrapper(zipped_file, "utf8"), index=False, header=None)
s3_resource = boto3.resource("s3")
s3_object = s3_resource.Object(bucket, f"test_csv_compress_{compression}/test.csv.xz")
s3_object.put(Body=buffer.getvalue())
file_path = f"s3://{bucket}/test_csv_compress_{compression}/test.csv.xz"
else:
file_path = f"s3://{bucket}/test_csv_compress_{compression}/test.csv"
wr.s3.to_csv(df=df, path=file_path, index=False, header=None)
wr.s3.wait_objects_exist(paths=[file_path])
df2 = wr.s3.read_csv(path=[file_path], names=df.columns)
assert len(df2.index) == 3
assert len(df2.columns) == 10
dfs = wr.s3.read_csv(path=[file_path], names=df.columns, chunksize=1)
for df3 in dfs:
assert len(df3.columns) == 10
wr.s3.delete_objects(path=path)
示例10: requires_name_attribute
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def requires_name_attribute(self):
self.skipTest("LZMAFile have no name attribute")
示例11: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if mode not in ("r", "w"):
raise ValueError("mode must be 'r' or 'w'")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
if mode == 'r':
raise ReadError("not an lzma file")
raise
except:
fileobj.close()
raise
t._extfileobj = False
return t
# All *open() methods are registered here.
示例12: download_file
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def download_file(url):
fname = url.split('/')[-1]
print(f'Downloading & Extracting - {fname}')
base = '../DynamicAnalyzer/tools/onDevice/frida/'
dwd_file = fname.replace('.xz', '')
dwd_loc = f'{base}{dwd_file}'
with requests.get(url, stream=True) as r:
with lzma.LZMAFile(r.raw) as f:
with open(dwd_loc, 'wb') as flip:
shutil.copyfileobj(f, flip)
示例13: xzFile
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def xzFile(source, dest):
if not 'xz' in _available_compression:
raise MDError, "Cannot use xz for compression, library/module is not available"
s_fn = open(source, 'rb')
destination = lzma.LZMAFile(dest, 'w')
while True:
data = s_fn.read(1024000)
if not data: break
destination.write(data)
destination.close()
s_fn.close()
示例14: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, compresslevel=6):
"""
Open xz compressed cpio archive name for reading or writing.
Appending is not allowed.
"""
if len(mode) > 1 or mode not in "rw":
raise ValueError("mode must be 'r' or 'w'.")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
if fileobj is not None:
fileobj = _XZProxy(fileobj, mode)
else:
fileobj = lzma.LZMAFile(name, mode) # options={'level': compresslevel, 'dict_size': 20 }
try:
t = cls.cpioopen(name, mode, fileobj)
except IOError:
raise ReadError("not a XZ file")
t._extfileobj = False
return t
# All *open() methods are registered here.
示例15: load
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAFile [as 别名]
def load(filename=DEFAULT_GEOZONES_FILE, drop=False):
'''
Load a geozones archive from <filename>
<filename> can be either a local path or a remote URL.
'''
ts = datetime.now().isoformat().replace('-', '').replace(':', '').split('.')[0]
prefix = 'geozones-{0}'.format(ts)
if filename.startswith('http'):
log.info('Downloading GeoZones bundle: %s', filename)
filename, _ = urlretrieve(filename, tmp.path(GEOZONE_FILENAME))
log.info('Extracting GeoZones bundle')
with handle_error(prefix):
with contextlib.closing(lzma.LZMAFile(filename)) as xz:
with tarfile.open(fileobj=xz) as f:
f.extractall(tmp.path(prefix))
log.info('Loading GeoZones levels')
log.info('Loading levels.msgpack')
levels_filepath = tmp.path(prefix + '/levels.msgpack')
if drop and GeoLevel.objects.count():
name = '_'.join((GeoLevel._get_collection_name(), ts))
target = GeoLevel._get_collection_name()
with switch_collection(GeoLevel, name):
with handle_error(prefix, GeoLevel):
total = load_levels(GeoLevel, levels_filepath)
GeoLevel.objects._collection.rename(target, dropTarget=True)
else:
with handle_error(prefix):
total = load_levels(GeoLevel, levels_filepath)
log.info('Loaded {total} levels'.format(total=total))
log.info('Loading zones.msgpack')
zones_filepath = tmp.path(prefix + '/zones.msgpack')
if drop and GeoZone.objects.count():
name = '_'.join((GeoZone._get_collection_name(), ts))
target = GeoZone._get_collection_name()
with switch_collection(GeoZone, name):
with handle_error(prefix, GeoZone):
total = load_zones(GeoZone, zones_filepath)
GeoZone.objects._collection.rename(target, dropTarget=True)
else:
with handle_error(prefix):
total = load_zones(GeoZone, zones_filepath)
log.info('Loaded {total} zones'.format(total=total))
cleanup(prefix)