def bz2_compress(self,file,type=True): # Compress/Decompress files into/from the bz2 format. compress if type else decompess
if not os.path.exists(file) or os.path.isdir(file): return False
try: filesize = os.path.getsize(file)
except: return False
if not type and not file.endswith(".bz2"): return False
blocksize = 102400
if type: compressor = bz2.BZ2Compressor()
else: decompressor = bz2.BZ2Decompressor()
handle1 = open(file,"rb")
handle2 = open(file+".bz2","wb") if type else open(file[:-4],"wb")
for i in range(int(math.ceil(float(filesize)/blocksize))):
if type: handle2.write(compressor.compress(handle1.read(blocksize)))
else: handle2.write(decompressor.decompress(handle1.read(blocksize)))
if type: handle2.write(compressor.flush())
handle1.close(); handle2.close()
self.debug("Successfully "+("" if type else "de")+"compressed file : "+file)
return True
################################################## Client Behaviour Functions ##################################################
评论列表
文章目录