def encZip(self): #Must close the zip before encrypt it, otherwise, the file are not integrate if self.curArchive is None: return self.curArchive.close() self.curArchive = None ############################ # Encrypt the zip file ############################ targetPath = transform.transformDirToInternal( fileTools.getTimestampWithFreeName(self.zipStorageDir, '.enc')) print 'copying "%s" to "%s"'%(self.curArchiveName, targetPath) #import shutil #shutil.copy(self.curArchiveName, targetPath+'.backup.zip') self.encCopier.copy(self.curArchiveName, targetPath) ############################ # Save info for zipped files ############################ s = json.dumps(self.zippedFileInfo, sort_keys=True, indent=4) f = open(self.curArchiveName.replace('.zip', '.log'),'w') f.write(s) f.close() self.encCopier.copy(self.curArchiveName.replace('.zip', '.log'), targetPath.replace('.enc', '.enclog')) ############################ # Update state in storage state ############################ self.updateZipLog(self.zippedFileInfo) #Clean the current zipped file info self.zippedFileInfo = {} zipFileFolderStorageItem = folderStorage.folderStorageItem(self.zipStorageDir, targetPath) self.lastState.zipFileUpdate(zipFileFolderStorageItem)
def getNextUpdatedItem(self): #print 'zipdir:',self.zipStorageDir for walkingItem in os.walk(self.zipStorageDir): #print walkingItem for j in walkingItem[2]: encZipFileFullPath = transform.transformDirToInternal(os.path.join(walkingItem[0], j)) print encZipFileFullPath.encode('gbk','replace') zipFileFolderStorageItem = folderStorage.folderStorageItem(self.zipStorageDir, encZipFileFullPath) if self.lastState.isZipFileUpdated(zipFileFolderStorageItem): ################################################################## #For zip storage, if the zip file was updated (or newly created) we #should enumerate all element in this zip file ################################################################## #First decrypt the zip file import re if re.search('\.enc$', encZipFileFullPath) is None: #Not an encrypted zip file, continue print 'not a encrypted zip file: ',encZipFileFullPath.encode('gbk','replace') continue zipFileFullPath = self.getZipFile(encZipFileFullPath) #Enumerate all files in the decrypted zip file zf = zipClass.ZFile(zipFileFullPath, 'r') #Generate a log file if it does not exist for i in zf.list(): #zf.extract(i, self.workingDir) extractedItemFullPath = os.path.join(self.workingDir, i) extractedItemInfo = self.getItemState(i) ''' try: extractedItem = extractedZipStorageItem(self.workingDir, os.path.join(self.workingDir, i), extractedItemInfo) except KeyError: print self.workingDir, i, extractedItemInfo raise KeyError ''' extractedItem = extractedZipStorageItem(self.workingDir, extractedItemFullPath, extractedItemInfo, zf, i) yield extractedItem self.lastState.zipFileUpdate(zipFileFolderStorageItem)