Skip to content

Instantly share code, notes, and snippets.

@m0smith
Created March 8, 2012 17:03
Show Gist options
  • Save m0smith/2002085 to your computer and use it in GitHub Desktop.
Save m0smith/2002085 to your computer and use it in GitHub Desktop.
A zipper for multi part mime parsing mht and mhtml files. Does not support updating
(ns gist.multipartmime-zip
(:require [clojure.zip :as zip])
(:import [javax.mail.util ByteArrayDataSource]
[javax.mail.internet MimeMultipart]))
(defn multipart? [part]
(.startsWith (.getContentType part) "multipart"))
(defn zip-children [mime-multi-part]
(let [count (.getCount mime-multi-part)]
(for [part (map #(.getBodyPart mime-multi-part %) (range count))]
(if (multipart? part)
(.getContent part)
part))))
(defn multi-part-mime-zipper
"root is a MimeMultipart instance"
[root]
(zip/zipper multipart?
zip-children
(fn [a b] nil)
root))
(defn p-zip [file]
(let [ ds (ByteArrayDataSource. (slurp file) "multipart/alternative")
mime-part (MimeMultipart. ds)]
(multi-part-mime-zipper mime-part)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment