从 XML 中提取 .zip blob 到文件 python
Extracting .zip blob from XML to file with python
我从数据库中输出了一个很大的 XML 文件,其中包含附加 zip 文件的 Blob 字段。
示例(抱歉文本输出过长,我选择了最短的):
<Attach><FileName>Protocols.zip</FileName><Type>zip</Type><Blob>UEsDBBQAAAAIAAEXolL0xX1/dggAAEEZAAA2AAAAj+Cu4q6qrqsgrqEgrq/gpaSlq6WtqKgg4+eg
4eKtqKquoiDiruCjrqIg/CA3MTA3MzguZG9j1Vlrb9vWGf4cA/4PZwU2t0ZIkbTkJLRlNHGaoUBr
dHCGfaYkyuJCiRpF2fWKAbHdy4pmy5B2xVD0sluBAgMKx6lbxY4boL/g8C/sl+x530NSFEXfhn7Y
kFjmeXnOe33ey5GXO1HXX5mdmZ1Z7rhOC0/LXTdyRCeK+pr7m6G3WV8NepHbi7S7231XNNWi/kLk
vhlV6PCSaHaccOBG9WHU1q6/kLHoOV23/nO354ZOFITjk697zTAYBO1I/CoIW8I0xYttz4/c0G29
xKcjL/LdlR/+Jf8h/y4fyT/Lv8qP5UfyT/LrH46XK+ottg2ibfXwE02bnRGVeXEHyonbbtvreZEX
9AZivoIXL7dB1tpO052dufJWsuh6/rZ91+kEXWcJ5L7TCwauZtoW6bMoqmJB1PBZFZaoLv1OsV8n
gdP8+/rrg2AtCLuOf1X4Xn7V8jbHS5LedcINr2cbzS5JVSutEURR0LV1wzDMfkQvyLma43sbPfvX
w0HktbeJWplnutdrIQL2wqJ+vYbtZOMVtmrg/da1TUs3FJPKfImt8xUyhlW+S44cK6VFQT93OtEt
9DY6Ubm+C6mgZKvvtrOdOQOaUNYNM6sK2kO+7/VcreOyIMswfqriseFqjdB17mlOG8dtZzPwWvQm
Z+n1VIHymDJ1S/FtBH6Lw8iWv+YNojec0NkInX4njdkEMecWNsvMZOVDoCnyXEGvLAIX16tj/j8H
YvGMQMDpHatgnFUtQOcMlI3xnIeZqScpVJ4pZXlyUVRlup2ePwyiW0Fr+y7yMcXPeJ1kfUrIGV9m
5yl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RkAADYA
AAAAAAAAAAAAAAAAAAAAAI/gruKuqq6rIK6hIK6v4KWkpaulraioIOPnoOHiraiqrqIg4q7go66i
IPwgNzEwNzM4LmRvY1BLBQYAAAAAAQABAGQAAADKCAAAAAA=</Blob></Attach>
我正在尝试解压缩 python:
with open("file.zip", "wb") as f_zip:
f_zip.write(base64.b64encode(my_blob.encode('utf-8')))
文件已创建,但尝试打开它时出现错误“无法展开“file.zip”。它的格式不受支持
Maurice 建议的最终解决方案并减轻了可能的不正确填充错误:
with open("file2.zip", "wb") as f_zip:
f_zip.write(base64.b64decode(my_blob+ "==="))
我从数据库中输出了一个很大的 XML 文件,其中包含附加 zip 文件的 Blob 字段。 示例(抱歉文本输出过长,我选择了最短的):
<Attach><FileName>Protocols.zip</FileName><Type>zip</Type><Blob>UEsDBBQAAAAIAAEXolL0xX1/dggAAEEZAAA2AAAAj+Cu4q6qrqsgrqEgrq/gpaSlq6WtqKgg4+eg
4eKtqKquoiDiruCjrqIg/CA3MTA3MzguZG9j1Vlrb9vWGf4cA/4PZwU2t0ZIkbTkJLRlNHGaoUBr
dHCGfaYkyuJCiRpF2fWKAbHdy4pmy5B2xVD0sluBAgMKx6lbxY4boL/g8C/sl+x530NSFEXfhn7Y
kFjmeXnOe33ey5GXO1HXX5mdmZ1Z7rhOC0/LXTdyRCeK+pr7m6G3WV8NepHbi7S7231XNNWi/kLk
vhlV6PCSaHaccOBG9WHU1q6/kLHoOV23/nO354ZOFITjk697zTAYBO1I/CoIW8I0xYttz4/c0G29
xKcjL/LdlR/+Jf8h/y4fyT/Lv8qP5UfyT/LrH46XK+ottg2ibfXwE02bnRGVeXEHyonbbtvreZEX
9AZivoIXL7dB1tpO052dufJWsuh6/rZ91+kEXWcJ5L7TCwauZtoW6bMoqmJB1PBZFZaoLv1OsV8n
gdP8+/rrg2AtCLuOf1X4Xn7V8jbHS5LedcINr2cbzS5JVSutEURR0LV1wzDMfkQvyLma43sbPfvX
w0HktbeJWplnutdrIQL2wqJ+v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EiVIel9mTaEjFZU/VQMY3Fgfcp6lQpJ2aDFOaUQNFBiT9Gwa8M93E93ESBSDfxXHu
WAWHZgGq/uDxncrwEr5Tc8E0S9TRnfRovtOwSyhjnvOEUebOSw4kE5ZkOCK4To06Co2QjNjAR6J8
8iDXTrrHQB2oJRlp2bUa9Sdcil589bYojZ1qN5mokdCEcdMwajXDuHX95uriwg2jat6qWdfuGLXb
q4uv3Lp556WseFYwNqo5qkKjZvKY/YGknP4fUEsBAi0AFAAAAAgAAReiUvTFfX92CAAAQRkAADYA
AAAAAAAAAAAAAAAAAAAAAI/gruKuqq6rIK6hIK6v4KWkpaulraioIOPnoOHiraiqrqIg4q7go66i
IPwgNzEwNzM4LmRvY1BLBQYAAAAAAQABAGQAAADKCAAAAAA=</Blob></Attach>
我正在尝试解压缩 python:
with open("file.zip", "wb") as f_zip:
f_zip.write(base64.b64encode(my_blob.encode('utf-8')))
文件已创建,但尝试打开它时出现错误“无法展开“file.zip”。它的格式不受支持
Maurice 建议的最终解决方案并减轻了可能的不正确填充错误:
with open("file2.zip", "wb") as f_zip:
f_zip.write(base64.b64decode(my_blob+ "==="))