def test_fileandsubdir(self): """ Test with a directory containing files and a subdirectory with files """ self.zip_dir = os.path.basename(self.path) self.expected_files = [self.zip_dir+"/a.txt", self.zip_dir+"/b.txt"] self.subdir = os.path.join(self.path, "d1") self.subdir_files = ["c.txt", "d.txt"] os.mkdir(self.subdir) for fn in self.expected_files: f = open(os.path.join(self.path, os.path.basename(fn)), "w") f.close() for fn in self.subdir_files: f = open(os.path.join(self.subdir, fn), "w") f.close() zipdir.zipdir(self.zipdir_filename, self.path) zf = zipfile.ZipFile(self.zipdir_filename, "r") files_in_archive = zf.namelist() zf.close() observed = set([f.replace("\\","/") for f in files_in_archive]) expected = set(self.expected_files) self.assertEqual(observed, expected)
def test_emptydir(self): """ Test with an empty directory """ zipdir.zipdir(self.zipdir_filename, self.path) zf = zipfile.ZipFile(self.zipdir_filename, "r") files_in_archive = zf.namelist() zf.close() observed = set([f.replace("\\","/") for f in files_in_archive]) expected = set([]) self.assertEqual(observed, expected)
def final_zip(target, destination): zipdir(target, destination)
def zip_directory(target, destination): zipdir(target, destination ) result = get_orginal_and_compressed_file_size( target, destination ) logger.info( "Target orginal size: %s kib, compressed size: %s kib, compression ratio: %s" % ( result.get('orginal_size') / 1024.0, result.get('compressed_size') / 1024.0, result.get('comperssion_ratio') ))
downloadpath = expanduser('~')+'/Downloads/HexoskinData/' recordpath = downloadpath + "records_downloaded.txt" files = glob.glob(downloadpath + '/*/*') with_inprogress = [int(file.rsplit('_', 1)[1].split('.')[0]) for file in files] if set(set(all_recordID) - set(with_inprogress)) == False : print "All records up to date." else: for record in all_recordID[1:len(all_recordID)/2]: if record not in with_inprogress: try: print 'Downloading record %s'%record data = HxApi2_0.getRecordData(auth,recordID=record) HxApi2_0.saveTxt(data, downloadpath) zipdir.zipdir(glob.glob(downloadpath + '/*/*'+ str(record))[0]) shutil.rmtree(glob.glob(downloadpath + '/*/*'+ str(record))[0]) with_inprogress.append(record) except Exception, e: print str(e) print "Problem loading record : " + str(record) ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) with open(recordpath, 'w') as f: f.write(str(len(with_inprogress)) + " records downloaded\n") simplejson.dump(with_inprogress.sort(), f) if __name__ == "__main__": main(sys.argv)
def compress_cv_with_image(img_path, out_path, level): # img_path = str(img_path) if os.path.exists(img_path) == False: print "ERROR: image path not exist: " + (img_path) return # create folder with pic name tmp_folder = out_path + ".tmp/" if os.path.isdir(tmp_folder): shutil.rmtree(tmp_folder) os.mkdir(tmp_folder) # get the image # print type(img_path) # print img_path img_path = img_path.decode("utf-8").encode("gbk") # print type(img_path) # print img_path img_gray = cv2.imread(img_path, 0) # cv2.imwrite('1.png',img) # save as binary # img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 7) # cv2.imwrite('2.png',img_bin) # compress cv with opencv png png_paras = list() png_paras.append(cv2.IMWRITE_PNG_COMPRESSION) png_paras.append(9) if GENERATE_BINARY_CV_IMG: save_path = os.path.join(tmp_folder, CROP_CV_IMG_NAME) save_path = save_path.decode("utf-8").encode("gbk") cv2.imwrite(save_path, img_bin, png_paras) # TODO compress these file with pngout print "========TODO========\ncompress these file with pngout" crop_cv_tmp_path = os.path.join(tmp_folder, CROP_CV_IMG_NAME) # crop_portrait_tmp_path = os.path.join(tmp_folder, CROP_PORT_IMG_NAME) # print crop_cv_tmp_path # crop_cv_tmp_path.decode('utf-8').encode('gbk') # print crop_portrait_tmp_path cmd = "optipng.exe " + (crop_cv_tmp_path) # os.system(cmd) para = level subprocess.call([resource_path("res\\optipng.exe"), para, crop_cv_tmp_path.encode("gbk")]) # rename png file as iipl file iipl_tmp_path = crop_cv_tmp_path.rstrip(".png") + ".iipl" os.rename(crop_cv_tmp_path, iipl_tmp_path) # os.remove(crop_cv_tmp_path) # os.remove(crop_portrait_tmp_path) iipl_head.add_head(iipl_tmp_path) # compress restul to .zip but rename as .iipl iipl_file_path = tmp_folder.rstrip(".tmp/") + ".iipl" zipdir.zipdir(tmp_folder, iipl_file_path) # remove .tmp folder if not KEEP_TMP_FILE: shutil.rmtree(tmp_folder)