def __extract__(self, compressed_file, destination_path): r = False # create destination path if not os.path.exists(destination_path): os.makedirs(destination_path) # delete content of destination path if os.path.exists(destination_path): for i in os.listdir(destination_path): os.unlink(destination_path + '/' + i) # create tempdir temp_dir = self.create_temp_dir() # extract in tempdir if extract_file(compressed_file, temp_dir): # eliminate folders for i in os.listdir(temp_dir): if os.path.isfile(temp_dir + '/' + i): shutil.copy(temp_dir + '/' + i, destination_path) os.unlink(temp_dir + '/' + i) elif os.path.isdir(temp_dir + '/' + i): for f in os.listdir(temp_dir + '/' + i ): if os.path.isfile(temp_dir + '/' + i + '/' + f): shutil.copy(temp_dir + '/' + i + '/' + f, destination_path) os.unlink(temp_dir + '/' + i + '/' + f) else: self.report.write(f + ' is directory and its contents will be ignored.', True, True, True) shutil.rmtree(temp_dir + '/' + i) shutil.rmtree(temp_dir) r = True return r
def extract_package(pkg_file, pkg_work_path): """ Extract files to pkg_work_path from compressed files that are in compressed_path """ r = False if os.path.isfile(pkg_file): if files_extractor.is_compressed_file(pkg_file): if os.path.exists(pkg_work_path): delete_file_or_folder(pkg_work_path) os.makedirs(pkg_work_path) # delete content of destination path # create tempdir temp_dir = tempfile.mkdtemp().replace('\\', '/') # extract in tempdir if files_extractor.extract_file(pkg_file, temp_dir): # eliminate folders for item in os.listdir(temp_dir): _file = temp_dir + '/' + item if os.path.isfile(_file): shutil.copyfile(_file, pkg_work_path + '/' + item) delete_file_or_folder(_file) elif os.path.isdir(_file): for f in os.listdir(_file): if os.path.isfile(_file + '/' + f): shutil.copyfile(_file + '/' + f, pkg_work_path + '/' + f) delete_file_or_folder(_file + '/' + f) shutil.rmtree(_file) shutil.rmtree(temp_dir) r = True return r
def unzip(compressed_filename, destination_path): """ Extract files to destination_path from compressed files that are in compressed_path """ r = False if os.path.isfile(compressed_filename): if files_extractor.is_compressed_file(compressed_filename): if not os.path.isdir(destination_path): os.makedirs(destination_path) # delete content of destination path # create tempdir # extract in tempdir r = files_extractor.extract_file(compressed_filename, destination_path) return r
def __extract__(self, compressed_file, destination_path, temp_dir=None): r = False # create destination path if not os.path.exists(destination_path): os.makedirs(destination_path) # delete content of destination path if os.path.exists(destination_path): for i in os.listdir(destination_path): os.unlink(destination_path + '/' + i) # create tempdir if temp_dir is None: temp_dir = self.create_temp_dir() else: delete_file_or_folder(temp_dir) if not os.path.isdir(temp_dir): os.makedirs(temp_dir) # extract in tempdir if extract_file(compressed_file, temp_dir): copy_all_files_to(temp_dir, destination_path) r = True delete_file_or_folder(temp_dir) return r