解码base64编码的zip文件以获得原始zip文件

时间:2019-05-13 05:12:09

标签: python python-3.x

我正在前端上传一个.zip文件,并使用readAsDataURL()方法将其转换为base64 URL。我正在将URL字符串发送到后端。我无法解码字符串以获取上载的.zip文件的原始内容

import base64
from io import BytesIO

image_64_encode = "data:application/zip;base64,UEsDBAoAAAAAAC93jk4AAAAAAAAAAAAAAAATABAAQ2VydGlmaWNhdGUgUGFyc2VyL1VYDADj0rNcAv2yXPUBFABQSwMEFAAIAAgAS0KOTgAAAAAAAAAAAAAAACAAEABDZXJ0aWZpY2F0ZSBQYXJzZXIvbWVzc2lncmF5LnBuZ1VYDAB2n7JcdZ+yXPUBFACU+gk41Iv7Po6bMcNrBpmZrCEzY2TLWofCYWaaqZHKcpA0ItKijH0ry8wYNZYYSiphiJNsUcRYIkdGCG1UtuzJFopk+0113p/zfn8+73O+//91dekKXddzP89938/9vOYVY31onwR8C1xISEjCgkKyFRKSHxASwsUDgm8IGT1uPC0khBayIBHsQkBTjyX8FLufl/ecOpXhSS/b/iR43Jzcmr1OCUhvCy8gWCwqMEDsvcWqO5rk3OP1//ryCGPwH/9ukhtWwP7NL31Vdo2HPKITTa/HcZ+RQcW229zjl57WHIyvk1/Y9vM3+yUXRHrAbVoWY351keufJ5ZWaqedt7GW0ikISUNX/Jtyy9iepNi+awaJLpG/eldcnRmXmQw6+zGiLnypr3Wt0PZEcKZaklXH2Uz72oS2vAWIx2debvNUbU9vAp2WMNz8pcc7uDL8q7Wzrktz4IUXqR3tZX0eGoeZ1d0vzCR44O7+2ECTPK9V+jNlKQ09lxv3pFz99cckA827WxCG6uAL3jSjdxcBbhUBHjfTDC0uKpdaG/W/MdxM+8M4u6A9PVSxYm0++cbkh5Ujz5zufp23VTaDhC3XDIaso79FnPLoQT819OLkGaBj2IihlA9+R2xPnsArpL379nXu0sbSRh/tUknfs1h8je2P9m1ADVGuwqg3MzQD4n0eBV+O/aunRs2GDauE41Ez46336ta/9XiPG4KrtBUYISWPqaSevep74CpSL1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owT0oVGU4SMoW3SurQCCp4zA9clKSrsFm5pFf+eZSGn7aqn2flP5TPf2/VEdXysXEVW9JwkCa0ALAs9dD0+Qid4wddPthLjXKYxIPk+JDOSd07MfHO8zXJvm6uhER+/1nzDa1g0G/ez2yDT7BrN4+up8by221PUk8LO/xqrXV96h8voiB12JxZ8vw6Pr92dDeVR9T8z3pd7pQa/ND8cMyF44+P4imTtup598adFanaXWaKZQlLEwNA2UVmO77KxGHdRCyc2idmSErlCQzX2RBFBZjqQ4Zwpa8DkI3RiEB8b1J4Rl7+c9OpT+5NJ73qO6z092aOrsRp4qV0JtfVmvOdvMyMi53vITHaN6mHvf0ZgtUh5GGD9N68BelmoMOVyDVDWwpnvFIX4vgo8uD80iLEd9GW6+vav+tPaz6IBUcAySGEdgK/7/Yyom2KJz/hFnkxtod5cjzvKVpYFfwvZwtkOsPl/fvRb6nWZOEvJg0nAe99SI7PPII3+nuGOr+29e2JzOWaiu67jO+BET1px0YXumTn2RrYg7gq9/dyHrTI6+zp1D6dCqhtP1BN5xcjnEL7A5NFhOECIRs8JxajCjBfkEZBbDxDa4cIXl/mCAx0EEaNa2cVvvXvRmvzDraxZ/wtLBd7y70hCqOzrT0PAvx1pJ6ZdiSEjIrYbxp1iCHeLzXS6GZA46vPqeSgyaiDJooc+9pV5QzMs3DcICfEtIvLnJ+/0nBwUf2WkDrgDGCgDYlA/odBJUcS/HSSeYI2OH+f5PZ"
starter = image_64_encode.find(',')
zip_data = image_64_encode[starter+1:]
file_data = bytes(zip_data, encoding="ascii")

d=base64.b64decode(file_data)

with open("zip_decode.zip", 'wb') as f:  

    f.write(d)

请建议我从此URL提取zip文件原始内容的方法

这是我用来编码zip文件的功能

andleFileUpload(e){
    let files = e.target.files;
    console.log("files are", files);
    let reader = new FileReader();
    reader.readAsDataURL(files[0]);
    reader.onload=(e)=>{
        console.log("inside reader", e.target.result);
    const url="http://ec2-3-85-140-138.compute-1.amazonaws.com:5000/api/upload";
    const formData = {file: e.target.result}
    return post(url, formData)
    .then(response => console.log("result", response))
    }
}

1 个答案:

答案 0 :(得分:0)

要将上传的.zip文件的原始内容写入zip_decode.zip

with open("zip_decode.zip", 'wb') as f:  
    f.write(base64.b64decode(image_64_encode.split(',', 1)[1]))