许多工具都可以导出.MHT文件。我想要一种方法将该单个文件转换为文件集合,HTML文件,相关图像和CSS文件,然后我可以上传到webhost并供所有浏览器使用。有没有人知道任何工具或库或算法来做到这一点。
答案 0 :(得分:12)
好吧,您可以在IE中打开.MHT文件并将其另存为网页。我用这个页面对此进行了测试,即使它在IE中看起来很奇怪(毕竟它是IE浏览器),它保存然后在Chrome中打开就好了(因为它看起来应该是这样)。
除了该方法,查看文件本身,文本块按原样保存在文件中,所有其他内容都保存在Base64中。每个内容项都以:
开头[Boundary]
Content-Type: [Mime Type]
Content-Transfer-Encoding: [Encoding Type]
Content-Location: [Full path of content]
[Mime类型] , [编码类型] 和 [内容的完整路径] 是可变的。 [编码类型] 似乎是 base64 或 quoted-printable 。 [Boundary] 在.MHT文件的开头定义如下:
From: <Saved by WebKit>
Subject: converter - How can you programmatically (or with a tool) convert .MHT mhtml files to regular HTML and CSS files? - Stack Overflow
Date: Fri, 9 May 2013 13:53:36 -0400
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0C08_58653ABB.B67612B7"
使用它,您可以根据需要创建自己的文件解析器。
答案 1 :(得分:3)
除了IE和MS Word之外,还有一个名为&#39; mht2html&#39;的开源跨平台程序。首先写在2007中,最后更新于2016。它有GUI和终端接口。
我还没有对它进行过测试,但似乎收到了很好的评价。
答案 2 :(得分:3)
MHT文件本质上是MIME。因此,可以使用Chilkat.Mime或完全免费的System.Net.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
答案 3 :(得分:1)
我认为@ XGundam05是正确的。以下是我为使其发挥作用所做的一切。
我从Visual Studio中的Windows窗体项目开始。将WebBrowser添加到表单,然后添加两个按钮。然后这段代码:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.ShowSaveAsDialog();
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("localfile.mht");
}
您应该能够获取此代码并添加一个文件列表,并使用foreach
处理每个文件。 webBrowser
包含一个名为ShowSaveAsDialog()
的方法;这将允许一个保存为.mht或仅保存html或整个页面。
编辑:您可以使用webBrowser的文档并在此处抓取信息。通过在MS中添加richTextBox和公共变量:http://msdn.microsoft.com/en-us/library/ms171713.aspx
public string Code
{
get
{
if (richTextBox1.Text != null)
{
return (richTextBox1.Text);
}
else
{
return ("");
}
}
set
{
richTextBox1.Text = value;
}
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("localfile.mht");
HtmlElement elem;
if (webBrowser1.Document != null)
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
if (elems.Count == 1)
{
elem = elems[0];
Code = elem.OuterHtml;
foreach (HtmlElement elem1 in elems)
{
//look for pictures to save
}
}
}
}
答案 4 :(得分:0)
因此,IE的自动化很难并且不能端到端地使用,因此我认为构建某种代码可以实现这一目标。在github上我发现这个python可能很好
https://github.com/Modified/MHTifier http://decodecode.net/elitist/2013/01/mhtifier/
如果我有时间,我会尝试在PowerShell中做类似的事情。
答案 5 :(得分:-1)
Firefox 有嵌入式工具。转到菜单(如果隐藏则按Alt键)File->Convert saved pages
。
答案 6 :(得分:-3)
步骤1:在浏览器中打开.MHT / .MHTML文件。
步骤2:右键单击以选择查看源代码。
步骤3:复制源代码并将其粘贴到新的.TXT文件,然后将文件扩展名更改为.HTML。