Odoo 12.如何将二进制文件转换为Zip?

问题描述

我需要导入一个仅包含xml文件的zip文件

我的向导如下:

class ZipImportsWizard(models.Model):
    _name = 'import.zip.dte'

    type = fields.Selection([('purchase','Purchases'),('sale','Sales'),],string="Type",default="purchase")
    file = fields.Binary(string='ZIP File',store=True)

我需要打开此zip文件并检查内容。 如果内容还可以,我必须将其发送给另一种方法

问题是,当我上传文件时,它会转换为二进制文件,因此我无法使用zipfile库来使用它。

如何再次将此二进制文件转换为Zip文件以使用它?

解决方法

我对ood没有经验;但是:

如果您有一个字节变量(二进制数据),则可以使用io.BytesIOzipfile将其作为带有标准python库的Zipfile读取:

from io import BytesIO
import zipfile

 # I assume ths contains the zipfile uploaded by the user.
 uploaded_zipfile = fields.Binary(string='ZIP File',store=True)

with BytesIO(uploaded_zipfile) as fp:
   userzip = zipfile.ZipFile(fp,'r')

   # You can extract the zip like this:
   userzip.extractall()

   # Or you can check the contents without extracting all the file
   whats_inside_the_zip = userzip.infolist()


,

我能够自己找到解决方案,也要感谢Similar question

中的@astronautlevel anwers
$annual = $amountSum * 12%