esign_file.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import base64
  2. import hashlib
  3. import os
  4. # 文件辅助类
  5. class fileHelp:
  6. def __init__(self, fileUrl):
  7. """
  8. 为文件类定义初始化公共参数
  9. :param fileUrl:
  10. """
  11. self.fileOs = os.path.abspath(os.path.join(os.getcwd(), fileUrl))
  12. self.contentMd5 = self.__content_encoding() # 初始化文件MD5
  13. self.fileName = os.path.basename(self.fileOs) # 初始化文件名
  14. self.fileSize = os.path.getsize(self.fileOs) # 初始化文件大小
  15. def __content_encoding(self):
  16. """
  17. 文件转 bytes 加密并使用 base64 编码
  18. :param fileOs: 文件路径
  19. :return: 返回加密编码后的字符串
  20. """
  21. with open(self.fileOs, 'rb') as f:
  22. content = f.read()
  23. content_md5 = hashlib.md5()
  24. content_md5.update(content)
  25. content_base64 = base64.b64encode(content_md5.digest())
  26. return content_base64.decode("utf-8")
  27. def getBinFile(self):
  28. """
  29. 获取文件流
  30. :param fileOs:
  31. :return:
  32. """
  33. with open(self.fileOs, 'rb') as f:
  34. binfile = f.read()
  35. return binfile