def payload_iter(self): from libarchive import stream_reader with self.open_payload() as payload_fobj: with stream_reader( payload_fobj, format_name=payload_fobj.format or 'all', filter_name=payload_fobj.compressor or 'all', ) as payload: for entry in payload: yield entry
def test_custom_writer_and_stream_reader(): # Collect information on what should be in the archive tree = treestat('libarchive') # Create an archive of our libarchive/ directory stream = io.BytesIO() with libarchive.custom_writer(stream.write, 'zip') as archive: archive.add_files('libarchive/') stream.seek(0) # Read the archive and check that the data is correct with libarchive.stream_reader(stream, 'zip') as archive: check_archive(archive, tree)
def payload_iter(self): ''' Iterate through the files in the RPM payload using libarchive. Yields a libarchive.ArchiveEntry for each file in the payload. ''' from libarchive import stream_reader with self.open_payload() as payload_fobj: with stream_reader( payload_fobj, format_name=payload_fobj.format or 'all', filter_name=payload_fobj.compressor or 'all', ) as payload: for entry in payload: yield entry
def extract_from_url(url, progress_bar=None): request = urllib.request.urlopen(url) if progress_bar is not None: progress_bar.desc = filename = request.info().get_filename() progress_bar.total = int(request.headers.get("Content-length", 0)) with libarchive.stream_reader(request.fp) as stream: for member in stream: if member.isdir: Path(member.path).mkdir() continue with open(member.path, "wb") as fp: for block in member.get_blocks(): fp.write(block) if progress_bar is not None: progress_bar.update(len(block))
def get_arm_z_image_version(fp): """Returns Linux kernel version of an ARM zImage.""" # In ARM uImage kernel is compressed within the image. To retrive # its version, we need find the compressed kernel, uncompress it, # and extract the version from the uncompressed data. for header in [ # Headers taken from: # https://github.com/torvalds/linux/blob/master/scripts/extract-vmlinux b'\x1f\x8b\x08', # gzip b'\xfd7zXZ\x00', # xz b'CZh', # bzip2 b'\x5d\x00\x00', # lzma b'\x89\x4c\x5a', # lzo b'\x02!L\x18', # lz4 b'(\xb5/\xfd', # zstd ]: fp.seek(0) try: # This could be improved to avoid to reading all file in memory fp.seek(fp.read().index(header)) with libarchive.stream_reader( fp, format_name='raw', filter_name='all', block_size=512, ) as archive: data_entry = next(iter(archive)) iterable = data_entry.get_blocks(512) pattern = br'Linux version (\S+).*' result = find(pattern, iterable) if result: return result except ValueError: continue except libarchive.exception.ArchiveError: continue return