Python 2.7 - Extract zip from email message file

I need to restore .zip archives, extract the file to .zip and extract its data. .Zip archives are attached to email message files; I do not use the mail protocol to access the mailbox. I can parse the messages ...

...
from email.parser import Parser
...
for fileName in os.listdir(mailDir):
    ...
    message = Parser().parse(open(mailDir + '/' + fileName, 'r'))
    ...
    for part in message.walk():
        if part.get_content_type() == 'application/octet-stream':

When I first started writing this code, I tested an email message with an attached CSV and had no problems accessing the attachment and pulling out the data, but now that I am working against emails with .zip (containing the previously used .csv) I was stuck. Added ...

import zipfile

... but it seems to me that I need to actually save the attached .zip to the file system in order to be able to use the zipfile. I would prefer not to do this and thought (hoped) that I could just use ...

zipfile.ZipFile(the_base64_string_from_message, 'r')

. .zip ? , , ( , ).

+4
2

StringIO , ; ...

import base64, StringIO, zipfile

# base64 string from the message
attachment = '...'
attachment = base64.b64decode(attachment)
attachment = StringIO.StringIO(attachment)

zipFile = zipfile.ZipFile(attachment, 'r')

zipfile.ZipFile.

+3

, , , , StringIO, , . , base64, . , :

import email
import zipfile
from cStringIO import StringIO
import base64

with open('some_email_with_zip.eml', 'r') as f:
    m = email.message_from_file(f)

for part in m.walk():
    # You might also check to see if the content-type for your zip files is
    # application/zip instead of application/octet-stream
    if part.get_content_type() == 'application/zip':
        zip_bytes = base64.b64decode(part.get_payload())
        file_wrapper = StringIO(zip_bytes)
        if zipfile.is_zipfile(file_wrapper):
            with zipfile.ZipFile(file_wrapper, 'r') as zf:
                zf.extractall()

, , extractall():

zf.extractall('/path/for/unzipped/files')
+4

All Articles