class LhaHandler(object): def __init__(self, path): self.path = path self.zip = LhaFile(self.path, "r") def list_files(self, sub_path): if sub_path: return for name in self.zip.namelist(): # if name.endswith(str("/")): # continue yield self.decode_name(name) def open(self, name): name = self.encode_name(name) # LhaFile does not have open method data = self.zip.read(name) return io.BytesIO(data) def exists(self, name): name = self.encode_name(name) items = self.zip.infolist() for item in items: if item.filename == name: return True return False # try: # self.zip.getinfo(name) # except KeyError: # return False # else: # return True def encode_name(self, name): name = name.replace("\\", "/") name = name.replace("%5f", "\\") name = name.replace("%25", "%") # FIXME: a little hack here, LhaFile uses os.sep # as path separator name = name.replace("/", os.sep) # name = name.encode("ISO-8859-1") return name def decode_name(self, name): # print("decode_name", name) # name = name.decode("ISO-8859-1") # FIXME: a little hack here, LhaFile uses os.sep # as path separator, normalizing to / name = name.replace(os.sep, "/") name = name.replace("%", "%25") name = name.replace("\\", "%5f") name = name.replace("/", os.sep) return name
def lha_extract(arcpath, outpath): arc = LhaFile(arcpath) for filename in [info.filename for info in arc.infolist()]: path = filename.replace("\\", "/") dirname = os.path.dirname(path) basename = os.path.basename(path) if dirname: make_dir(os.path.join(outpath, dirname)) if basename: make_dir(os.path.join(outpath, dirname)) open(os.path.join(outpath, dirname, basename), "wb").write(arc.read(filename))
class LhaHandler(object): def __init__(self, path): self.path = path self._lhafile = LhaFile(self.path, "r") def decode_name(self, name): # FIXME: a little hack here, LhaFile uses os.sep # as path separator, normalizing to / name = name.replace(os.sep, "/") name = name.replace("%", "%25") name = name.replace("\\", "%5c") name = name.replace("/", os.sep) return name def encode_name(self, name): name = name.replace("\\", "/") name = name.replace("%5c", "\\") name = name.replace("%25", "%") # FIXME: Legacy workaround for existing entries with incorrect escape. name = name.replace("%5f", "\\") # FIXME: a little hack here, LhaFile uses os.sep # as path separator name = name.replace("/", os.sep) return name def exists(self, name): # FIXME: Maybe look up in NameToInfo instead for quicker lookups name = self.encode_name(name) items = self._lhafile.infolist() for item in items: if item.filename == name: return True return False def getinfo(self, name): # FIXME: Should instead add getinfo to LhaFile... return self._lhafile.NameToInfo[self.encode_name(name)] def list_files(self, sub_path): if sub_path: return for name in self._lhafile.namelist(): # if name.endswith(str("/")): # continue yield self.decode_name(name) def open(self, name): # LhaFile does not have open method data = self._lhafile.read(self.encode_name(name)) return io.BytesIO(data)
class LhaHandler(object): def __init__(self, path): self.path = path self._lhafile = LhaFile(self.path, "r") def decode_name(self, name): # FIXME: a little hack here, LhaFile uses os.sep # as path separator, normalizing to / name = name.replace(os.sep, "/") name = name.replace("%", "%25") name = name.replace("\\", "%5c") name = name.replace("/", os.sep) return name def encode_name(self, name): name = name.replace("\\", "/") name = name.replace("%5c", "\\") name = name.replace("%25", "%") # FIXME: a little hack here, LhaFile uses os.sep # as path separator name = name.replace("/", os.sep) return name def exists(self, name): # FIXME: Maybe look up in NameToInfo instead for quicker lookups name = self.encode_name(name) items = self._lhafile.infolist() for item in items: if item.filename == name: return True return False def _formatinfo(self, info): comment = info.comment if comments_as_bytes and isinstance(comment, str): # It might have been a mistake for LhaFile to decode the comment # as ISO-8859-1. We encode back and keep it as bytes, for # consistency with the ZipFile module... maybe. Alternatively, # decode zipfile comments as ISO-8859-1 instead... comment = comment.encode("ISO-8859-1") return ArchiveFileInfo( filename=info.filename, file_size=info.file_size, comment=comment, ) def getinfo(self, name): # FIXME: Should instead add getinfo to LhaFile... info = self._lhafile.NameToInfo[self.encode_name(name)] return self._formatinfo(info) def infolist(self): result = [] for info in self._lhafile.infolist(): result.append(self._formatinfo(info)) return result def list_files(self, sub_path): if sub_path: return for name in self._lhafile.namelist(): # if name.endswith(str("/")): # continue yield self.decode_name(name) def open(self, name): # LhaFile does not have open method data = self._lhafile.read(self.encode_name(name)) return io.BytesIO(data)
except FileNotFoundError: print("File not found: " + args.input) sys.exit() data = f.read() f.close() fileHeader = checkFileHeader(data) if fileHeader == fileTypes.Garbage: # File isn't already decompressed try: archive = LhaFile(args.input) except BadLhafile: print( "Unable to decompress the file. Try decompressing it in 7Zip or another archive tool" ) sys.exit() # Read first file out of archive (they only ever have 1 file) data = archive.read(archive.infolist()[0].filename) fileHeader = checkFileHeader(data) if fileHeader == fileTypes.Garbage: print( "File was a valid LHA archive, but it didn't contain a valid YM file" ) sys.exit() elif fileHeader == fileTypes.Unsupported: print("Error: Unsupported file type") sys.exit() elif fileHeader == fileTypes.Unsupported: print("Error: Unsupported file type") sys.exit() vgmOutput = [] numFrames = 0 framerate = args.rate
def _cli_slaves_add_archive(self, source_file: BinaryIO, source_root_path: str, installed_root_path: str): file_path = source_file.name file_dir = os.path.dirname(file_path) file_name = os.path.basename(file_path) file_ext = os.path.splitext(file_name)[1].lower() if file_ext == ".lha": lha_file = None try: lha_file = LhaFile(source_file) except BadLhafile: DataSet.logger.error( f"Unable to open LHA file '{file_name}'. Skipping.") if lha_file is not None: archived_files = [x for x in lha_file.infolist()] for archived_file in archived_files: archived_file_path = archived_file.filename archived_file_name = os.path.basename(archived_file_path) archived_file_ext = os.path.splitext( archived_file_path)[1].lower() archived_file_dir = archived_file.directory archived_file_datetime = archived_file.date_time if archived_file_ext == ".slave": with tempfile.TemporaryDirectory() as temp_dir: extracted_dir = None try: extracted_dir = os.path.join( temp_dir, archived_file_dir) except TypeError: DataSet.logger.error( f"Unable to extract archive '{file_name}'. Skipping." ) if extracted_dir is not None: extracted_file_path = os.path.join( extracted_dir, archived_file_name) os.makedirs(extracted_dir) open(extracted_file_path, "wb").write( lha_file.read(archived_file_path)) with open(extracted_file_path, "rb") as temp_file: installed_dir = file_dir.replace( source_root_path, "") installed_path = self._reformat_amiga_path( installed_root_path, f"{installed_dir}/{archived_file_path}" ) self._add_slave( temp_file, file_datetime=archived_file_datetime, installed_path=installed_path) slave = self._get_slave_by_file_path( extracted_file_path) if slave is not None: fake_file_path = os.path.join( file_dir, archived_file_path) slave.file_path = fake_file_path slave.file_datetime = archived_file_datetime self.save() else: DataSet.logger.error(f"Unsupported file '{file_name}'.")