def unarc(name): info('extract files from "%s"', topdir(name)) if name.endswith('.lha'): import lhafile arc = lhafile.LhaFile(name) for item in arc.infolist(): filename = os.sep.join(item.filename.split('\\')) mkdir(path.dirname(filename)) debug('extract "%s"', filename) if path.isdir(filename): continue with open(filename, 'wb') as f: f.write(arc.read(item.filename)) elif name.endswith('.tar.gz') or name.endswith('.tar.bz2'): with tarfile.open(name) as arc: for item in arc.getmembers(): debug('extract "%s"' % item.name) arc.extract(item) elif name.endswith('.zip'): with zipfile.ZipFile(name) as arc: for item in arc.infolist(): debug('extract "%s"' % item.filename) arc.extract(item) else: raise RuntimeError('Unrecognized archive: "%s"', name)
def _create_archive_obj(self, sf, scanner): if lhafile: try: fobj = sf.get_fobj() return lhafile.LhaFile(fobj, "r") except Exception as e: scanner.warning(sf, "error reading archive: %s" % e) else: scanner.warning(sf, "can't handle archive. missing 'lhafile' module.")
def load(fo): is_lha = fo.read(5).endswith(b'-lh') fo.seek(0) if is_lha: if not lhafile: raise YMFileException("Compressed file; install 'lhafile'") else: lha = lhafile.LhaFile(fo) lha_filename = lha.namelist()[0] fo = io.BytesIO(lha.read(lha_filename)) ym = {} for field, data_type, length in header: if data_type == 'skip': fo.seek(length, 1) continue if data_type == 'digidrum': if ym['digidrums']: for i in range(ym['digidrums']): sample_size = read_int(fo, 4) fo.seek(sample_size, 1) continue read = read_int if data_type == 'int' else read_str value = read(fo, length) if field == 'file_id' and value not in supported_formats: raise YMFileException("Unsupported format: " + value) ym[field] = value interleaved = bool(ym['attributes'] & 1) if interleaved: chunks = 16 chunk_size = ym['frames'] else: chunks = ym['frames'] chunk_size = 16 data = [] for i in range(chunks): chunk = fo.read(chunk_size) if len(chunk) < chunk_size: raise YMFileException("Truncated file") data.append(chunk) end_marker = fo.read(4) if len(end_marker) < 4: raise YMFileException("Truncated file") elif end_marker != b'End!': raise YMFileException("Wrong end marker: " + str(end_marker)) ym['interleaved'] = interleaved ym['data'] = data ym['compressed'] = is_lha if is_lha: ym['compressed_filename'] = lha_filename fo.close() ym['length'] = round(ym['frames'] / ym['frame_freq']) return ym
def _lhafile(src_path, dest_path, image_id): if NO_LHA: raise Exception("No lhafile available.") try: with lhafile.LhaFile(src_path, 'r') as lfd: content = lfd.namelist() if len(content) != 1: raise Exception("Archive contains more than one file.") else: lfd.extract(content[0], dest_path) except Exception as e: LOG.debug("LHA: Error decompressing image %(iid)s: %(msg)s", { "iid": image_id, "msg": encodeutils.exception_to_unicode(e)}) raise
def _create_archive_obj(self, file_name): return lhafile.LhaFile(file_name, "r")
#!/usr/sbin/python3 import lhafile import struct import sys import pathlib inf = sys.argv[1] a = lhafile.LhaFile(inf) for info in a.infolist(): name = b''.join([struct.pack('B', ord(i)) for i in info.filename]) name = name.decode('shift_jis') if '\\' in name: sep = name.split('\\') if '/' in name: sep = name.split('/') if len(sep) > 1: d = pathlib.Path(*sep[:-1]) d.mkdir(parents=True, exist_ok=True) pathlib.Path(*sep).write_bytes(a.read(info.filename))