MHT文件本质上是MIME。因此,可以使用Chilkat.Mime或完全免费的.Mime组件来访问其内部结构。例如,如果MHT包含图像,则可以用输出HTML中的base64字符串替换它们。
Imports HtmlAgilityPack
Imports Fizzler.Systems.HtmlAgilityPack
Imports Chilkat
Public Function ConvertMhtToHtml(ByVal mhtFile As String) As String
Dim chilkatWholeMime As New Chilkat.Mime
'Load mime'
chilkatWholeMime.LoadMimeFile(mhtFile)
'Get html string, which is 1-st part of mime'
Dim html As String = chilkatWholeMime.GetPart(0).GetBodyDecoded
'Create collection for storing url of images and theirs base64 representations'
Dim allImages As New Specialized.NameValueCollection
'Iterate through mime parts'
For i = 1 To chilkatWholeMime.NumParts - 1
Dim m As Chilkat.Mime = chilkatWholeMime.GetPart(i)
'See if it is image'
If m.IsImage AndAlso m.Encoding = "base64" Then
allImages.Add(m.GetHeaderField("Content-Location"), "data:" + m.ContentType + ";base64," + m.GetBodyEncoded)
End If : m.Dispose()
Next : chilkatWholeMime.Dispose()
'Now it is time to replace the source attribute of all images in HTML with dataURI'
Dim htmlDoc As New HtmlDocument : htmlDoc.LoadHtml(html) : Dim docNode As HtmlNode = htmlDoc.DocumentNode
For i = 0 To allImages.Count - 1
'Select all images, whose src attribute is equal to saved URL'
Dim keyURL As String = allImages.GetKey(i) 'Saved url from MHT'
Dim elementsWithPics() As HtmlNode = docNode.QuerySelectorAll("img[src='" + keyURL + "']").ToArray
Dim imgsrc As String = allImages.GetValues(i)(0) 'dataURI as base64 string'
For j = 0 To elementsWithPics.Length - 1
elementsWithPics(j).SetAttributeValue("src", imgsrc)
Next
'Select all elements, whose style attribute contains saved URL'
elementsWithPics = docNode.QuerySelectorAll("[style~='" + keyURL + "']").ToArray
For j = 0 To elementsWithPics.Length - 1
'Get and modify style'
Dim modStyle As String = Strings.Replace(elementsWithPics(j).GetAttributeValue("style", String.Empty), keyURL, imgsrc, 1, 1, 1)
elementsWithPics(j).SetAttributeValue("style", modStyle)
Next : Erase elementsWithPics
Next
'Get final html'
Dim tw As New StringWriter()
htmlDoc.Save(tw) : html = tw.ToString : tw.Close() : tw.Dispose()
Return html
End Function
编程方式加载css文件夹 如何以编程方式(或使用工具)将.MHT mhtml文件转换为常规的HTML和CSS文件?...