Start a Conversation

해결되지 않음

이 게시글은 5년 이상 된 게시글입니다.

Closed

N

270 Posts

12585

July 28th, 2008 23:00

익스플로러 보관화일 mht(MHTML)확장명으로 저장하기!

mht(MHTML) 확장명으로 저장하는방법!

 

 

~.mht[MHTML 파일의 확장자 또는 MHTML 포맷 자체]

 

여기 저기 많은 사이트를 돌아 다니다 보면 왠만한 사이트들은
일정 기간이 지나면 리뉴얼(ReNewal)되는데 예전에 이런 사이트에서
이런 저런 좋은정보 라든지 새로운기능들을 캡쳐(Capture)해 두고
싶은 사이트가 있었는데 리뉴얼(ReNewal)되서 사라졌거나 해서 너무
안타까워 하거나, 디자이너(Designer)로 재직하면서 여러 많은 사이트
들을 다니면서 자료/정보 수집을 위해 이미지라든가 플래쉬(Flash)같은
정보를 수집하려 할때 ~.html화일로 저장하려면 일일이 이미지 따로
저장 하고 html tag 따로 저장하려면 번거로운데다가 용량도 만만치
않을때가 많은데 이럴때 ~.mht화일로 저장 하게 되면 html화일에 비해
상대적으로 용량도 적을뿐더러 해당 사이트를 캡쳐(Capture)한것과 같이
플래쉬(Flash)와 이미지등등이 그대로 저장이 되면서도 용량은 확실히
html 화일보다 적게 저장이 되고, 더더욱 해당 사이트가 사라지더라도
~.mht화일은 그때 그 사이트 웹페이지(Webpage)가 거의 그대로 보여진다는
것이 꽤 좋은거 같아 ~.mht 확장명으로 저장하는 방법을 소개합니다

 


※ 참고로 네이뇬와 같은 대부분 인지도 있는 사이트들은 ~.mht로 저장하는 기능 막았네영 -ㅅ-

 

네이뇬같은 사이트 처럼 ~.mht로 저장하는것을 막은 사이트는 ~.html 화일로저장해서
MS Word를 이용해서 ~.mht 화일로 저장할 수 있는데 방법은 ...

 

1.일단 저장된 html파일과 폴더를 준비하여 위치를 확인하시고요
2.그다음 Microsoft Word를 실행합니다.
3.그 다음 워드에서 파일>열기 하셔서 해당 html을 선택하시면 Word가 폴더까지 함께 읽습니다.
4.마지막으로 파일>다른 이름으로 저장 하신 후 파일형식을 웹보관파일(*.mht)로 선택하시고 저장 누르시면 됩니다.

 


1. VBscript를 이용한 방법

 

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1 'Binary data
Const adTypeText = 2 '(Default) Text data

 

URL = "http://www.parkoz.com/"
DiskFile = "D:\\tests.mht"

 

Set objMessage = CreateObject("CDO.Message")
objMessage.CreateMHTMLBody URL
SaveToFile objMessage, DiskFile

MsgBox "@end"

 

Sub SaveToFile(Msg, Fn)
Dim Strm, Dsk
Set Strm = CreateObject("ADODB.Stream")

 

Strm.Type = adTypeText
Strm.Charset = "US-ASCII"
Strm.Open

 

Set Dsk = Msg.DataSource
Dsk.SaveToObject Strm, "_Stream"
Strm.SaveToFile Fn, adSaveCreateOverWrite
End Sub

 

 

2. ASP를 이용한 방법

 

Dim iMsg, pFileName, pTargetUrl
Call gAuthUser

 

pTargetUrl = "http://www.parkoz.com/"
pFileName = "tests.mht"
Set iMsg = CreateObject("CDO.Message") 'Create Message object

 

'If you are under a proxy, you need that configuration parameters
'Set iConf = CreateObject("CDO.Configuration") 'Create Message Configuration Object
'Set Flds = iConf.Fields
'Flds("
http://schemas.microsoft.com/cdo/configuration/urlproxyserver")= "server:80"
'Flds("
http://schemas.microsoft.com/cdo/configuration/urlproxybypass")= ""
'Flds("
http://schemas.microsoft.com/cdo/configuration/urlgetlatestversion")= True
'Flds.Update

 

iMsg.CreateMHTMLBody pTargetUrl, 0

 

Response.AddHeader "content-disposition","attachment; filename=" & pFileName
Response.ContentType = "application/x-msdownloadwm"
Response.CacheControl = "public"
Response.Write iMsg.GetStream.readText
Response.end

 

Set iMsg = Nothing

 


3. VC++을 이용한 방법

 

#import "c:\\program files\\common files\\system\\ado\\msado15.dll" _
no_namespace rename("EOF", "EndOfFile")
#import no_namespace rename("EOF", "EndOfFile")

 

...

 

void SaveWholePage(LPCTSTR page_url,LPCTSTR save_filename)
{
CoInitialize(NULL);
{
IMessagePtr iMsg(__uuidof(Message));
IConfigurationPtr iConf(__uuidof(Configuration));
iMsg->Configuration = iConf;
try
{
iMsg->CreateMHTMLBody(
page_url,
cdoSuppressNone,
"domain\\\\username",
"password");
}
catch(_com_error err)
{
// handle exception
}
_StreamPtr pStream=iMsg->GetStream();
pStream->SaveToFile( save_filename,
adSaveCreateOverWrite);
}
CoUninitialize();
}

 

// Usage
SaveWholePage("
http://www.parkoz.com/", "tests.mht");

 

 

4. C#을 이용한 방법

 

//Mht 파일을 저장하기 위한 함수
public bool SaveMHT(string strHTML, string strMHTFilename, string strBoardID, string strFilePath)
{
string DocPath = "";
string mhtfilePath = "";

 

try
{
strHTML = strHTML.Replace("'", "''");
DocPath = strFilePath + "\\\\";

 

if (System.IO.Directory.Exists(DocPath + strBoardID) == false)
{
System.IO.Directory.CreateDirectory(DocPath + strBoardID);
System.IO.Directory.CreateDirectory(DocPath + strBoardID + "
\\\\UploadFile");
System.IO.Directory.CreateDirectory(DocPath + strBoardID + "
\\\\doc");
}

 

mhtfilePath = DocPath + strBoardID + "\\\\doc\\\\" + strMHTFilename + ".htm";
string mhtfilePath2 = DocPath + strBoardID + "
\\\\doc\\\\" + strMHTFilename + ".mht";

 

//StremWriter를 이용하여 임시 HTML파일을 생성한다.
if (System.IO.File.Exists(mhtfilePath)) System.IO.File.Delete(mhtfilePath);
StreamWriter output = new StreamWriter(mhtfilePath, false, System.Text.Encoding.Default);
output.WriteLine(strHTML);
output.Close();

 

//등록된 Html파일의 URL 주소를 가져 와서 CDO객체를 이용하여 MHT를 생성 한다.
string fileURL = @"html을 저장할 절대 경로위치".htm";
CDO.Message iMsg = new CDO.MessageClass();
iMsg.CreateMHTMLBody(fileURL,0,"","");

 

//등록된 Html파일의 URL 주소를 가져 와서 CDO객체를 이용하여 MHT를 생성 한다.
if (System.IO.File.Exists(mhtfilePath2)) System.IO.File.Delete(mhtfilePath2);
ADODB.Stream streamStr = new ADODB.StreamClass();
streamStr = iMsg.GetStream();
streamStr.SaveToFile(mhtfilePath2,ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
streamStr.Close();

 

//등록된 Html파일을 삭제 한다.
if (System.IO.File.Exists(mhtfilePath)) System.IO.File.Delete(mhtfilePath);

 

return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}

 

// Usage
BOOL SaveMHTResult = SaveMHT(h_content, "test.mht", r_BoardID, "
http://www.parkoz.com/");

 

 

5. Delphi를 이용한 방법

 

[Delphi] How to save a web page as HTML or MHT

 

How to save a web page as HTML or MHT Here's how to save a web page displayed inside a WebBrowser (TWebBrowser component) as a raw HTML file or into a single (MHT) file (MHTML format: web archive - single file).

  

 

When working with Delphi, the TWebBrowser component allows you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.

File ... Save As; or how to save a web page from TWebBrowser
When using Internet Explorer, you are allowed you to view the source HTML code of a page and to save that page as a file on your local drive. If you are viewing a page that you wish to keep, go to the File/Save As ... menu item. In the dialog box that opens, you have several file types offered. Saving the page as a different filetype will affect how the page is saved...

SaveAs in IE

The TWebBrowser component (located on the "Internet" page of the Component Palette) provides access to the Web browser functionality from your Delphi applications. In general, you'll want to enable saving of a web page displayed inside a WebBrowser as a HTML file to a disk.

Saving a web page as a raw HTML

If you only want to save a web page as a raw HTML you would select "Web Page, HTML only (*.htm, *.html)". It will simply save the current page's source HTML to your drive intact. This action will NOT save the graphics from the page or any other files used within the page, which means that if you loaded the file back from the local disk, you would see broken image links.

Here's how to save a web page as raw HTML using Delphi code:

 

 

uses ActiveX; ... procedure WB_SaveAs_HTML (WB:TWebBrowser; const FileName : string); var PersistStream: IPersistStreamInit; Stream: IStream; FileStream: TFileStream; begin if not Assigned(WB.Document) then begin ShowMessage('Document not loaded!'); Exit; end; PersistStream := WB.Document as IPersistStreamInit; FileStream := TFileStream.Create(FileName, fmCreate); try Stream := TStreamAdapter.Create(FileStream, soReference) as IStream; if Failed(PersistStream.Save(Stream, True)) then ShowMessage('SaveAs HTML fail!'); finally FileStream.Free; end; end; (* WB_SaveAs_HTML *)

Usage sample:

//first navigate WebBrowser1.Navigate('http://delphi.about.com'); //then save WB_SaveAs_HTML(WebBrowser1,'c:\\WebBrowser1.html');

 

 

Note 1: the IPersistStreamInit and IStream interfaces are declared inside the ActiveX unit.
Note 2: the web page is saved as a raw html to the WebBrowser1.html file on the root folder of the C drive.

MHT : Web archive - single file

When you save a Web page as "Web archive, single file (*.mht)" the web document gets saved in the Multipurpose Internet Mail Extension HTML (MHTML) format with a .mht file extension. All relative links in the Web page are remapped and the embedded content is included in the .mht file, rather than being saved in a separate folder (as the case is with "Web Page, complete (*.htm, *.html)").

MHTML enables you to send and receive Web pages and other HTML documents using e-mail programs such as Microsoft Outlook, and Microsoft Outlook Express; or even your custom Delphi email sending solutions. MHTML enables you to embed images directly into the body of your e-mail messages rather than attaching them to the message.

Here's how to save a web page as a single file (mht format) using Delphi code:

 

 

uses CDO_TLB, ADODB_TLB; ... procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName); var Msg: IMessage; Conf: IConfiguration; Stream: _Stream; URL : widestring; begin if not Assigned(WB.Document) then Exit; URL := WB.LocationURL; Msg := CoMessage.Create; Conf := CoConfiguration.Create; try Msg.Configuration := Conf; Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', ''); Stream := Msg.GetStream; Stream.SaveToFile(FileName, adSaveCreateOverWrite); finally Msg := nil; Conf := nil; Stream := nil; end; end; (* WB_SaveAs_MHT *)  

Sample usage:

//first navigate WebBrowser1.Navigate('http://delphi.about.com'); //then save WB_SaveAs_MHT(WebBrowser1,'c:\\WebBrowser1.mht');

 

 

Note 1: The _Stream class is defined in ADODB_TLB unit that you probably already have created. The IMessage and IConfiguration interfaces code from cdosys.dll library. CDO stands for Collaboration Data Objects - object libraries designed to enable SMTP Messaging.
The CDO_TLB is an auto generated unit by Delphi. To create it, from the main menu select "Import Type Library", select "C:\\WINDOWS\\system32\\cdosys.dll" then click the "Create unit" button.

Saving a web document using TWebBrowser

Note that you could rewrite the WB_SaveAs_MHT procedure to accept an URL string (not TWebBrowser) to be able to save a web page directly - no need to use the WebBrowser component. The URL from WebBrowser is retrieved using the WB.LocationURL property.

WebBrowser to the Max

Here are some more nifty WebBroswer related tips and tricks, like: How to load HTML directly to a WebBrowser, How to print a document/page in a TWebBrowser, How to call the Find dialog in WebBrowser, etc, ...

 

298 Posts

July 31st, 2008 19:00

네 좋은 정보인것 같네요..
이벤트를 찾을 수 없습니다!

Top