def __handle_file(self, plugin_file): # Uncompress the file. temp_dir = tempfile.gettempdir() if is_zipfile(plugin_file): compress_fd = zipfile.ZipFile(plugin_file, allowZip64=True) compress_fd.extractall(path=temp_dir) elif is_bz2file(plugin_file): #first check if we can handle as tar.bz2 (big chances) try: compress_fd = TarFile(name=plugin_file, mode="r:bz2") compress_fd.extractall(path=temp_dir) except CompressionError: print "Upz!, fail in compressed file handling, Retrying" try: compress_fd = bz2.BZ2File(plugin_file) tmp_fd = tempfile.TemporaryFile(suffix=".tar", prefix="ncmprs") tmp_fd.file.write(compress_fd.read()) tmp_fd.file.flush() tar_fd = TarFile.taropen(name=None, fileobj=tmp_fd) tar_fd.extractall(path=temp_dir) tar_fd.close() tmp_fd.close() except: print "Upz!, fail in compressed file handling, Again! :(" return None elif is_gzipfile(plugin_file): #first check if we can handle as tar.gz (big chances) try: compress_fd = TarFile(name=plugin_file, mode="r:gz") compress_fd.extractall(path=temp_dir) except CompressionError: print "Upz!, fail in compressed file handling, Retrying" try: compress_fd = gzip.GzipFile(plugin_file) tmp_fd = tempfile.TemporaryFile(suffix=".tar", prefix="ncmprs") tmp_fd.file.write(compress_fd.read()) tmp_fd.file.flush() tar_fd = TarFile.taropen(name=None, fileobj=tmp_fd) tar_fd.extractall(path=temp_dir) tar_fd.close() tmp_fd.close() except: print "Upz!, fail in compressed file handling, Again! :(" return None return self.__handle_dir(temp_dir)
class ArchiveManager: def __init__(self): """Summary """ self.archive = None self.archive_type = None self.listfile = None self.listfile_index = 0 self.archive_path = None self.archive_length = 0 self.hit_next = 0 def open_zip(self, archive_path): if self.archive: self.archive.close() self.archive_path = archive_path filename, file_extension = os.path.splitext(archive_path) if file_extension == ".zip" or file_extension == ".cbz": self.archive = ZipFile(archive_path, 'r') self.archive_type = "zip" namelist = self.archive.namelist() elif file_extension == ".tar" or file_extension == ".cbt": self.archive = TarFile(archive_path, 'r') self.archive_type = "tar" namelist = self.archive.getnames() else: raise ("archive not supported") # we sort the files by decimal found, excluding directories / self.listfile = sorted([x for x in namelist if not x.endswith('/')], key=lambda name: alphanum_key(name)) self.archive_length = len(self.listfile) self.listfile_index = 0 def delete_current_archive(self): if not self.archive or not self.archive_path: return self.archive.close() try: os.remove(self.archive_path) return True except Exception as e: print(e) return False def first_page(self): return self.get_file(self.listfile[0]) def last_page(self): self.listfile_index = len(self.listfile) - 1 return self.get_file(self.listfile[self.listfile_index]) def get_file(self, name): image = BytesIO() if self.archive_type == "zip": image.write(self.archive.read(name)) elif self.archive_type == "tar": tarinfo = self.archive.getmember(name) image_file = self.archive.extractfile(tarinfo) image.write(image_file.read()) else: return None return image def next(self): if not self.archive: return None self.listfile_index = self.listfile_index + 1 if self.listfile_index >= self.archive_length: self.listfile_index = self.archive_length - 1 return None filename = self.listfile[self.listfile_index] return self.get_file(filename) def previous(self): if not self.archive: return None self.listfile_index = self.listfile_index - 1 if self.listfile_index < 0: self.listfile_index = 0 return None filename = self.listfile[self.listfile_index] return self.get_file(filename) def get_current_archive_name(self): return os.path.basename(self.archive_path) def get_display_counter(self): return "%i/%i" % (self.listfile_index + 1, self.archive_length)