大家都发觉,在安装WEB程序时,最麻烦之处在于上传网站程序文件,大多数虚似主机只提供FTP功能,但有时网速较慢的时候,上传几M的文件就用了差不多一个多钟,尤其当文件较多的时候。等待上传文件的时间就够等。或者大家都会想到,如果能打包起来,上传后再解包,这样就好了。
Code
1<%@PageLanguage="C#"%>
2
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<scriptrunat="server">
6protectedvoidPage_Load(objectsender,EventArgse)
7{
8//设置脚本执行时间,防止超时
9Server.ScriptTimeout=999999999;
10}
11
12/**////<summary>
13///远程下载文件
14///</summary>
15///<paramname="sURL"></param>
16///<paramname="Filename"></param>
17///<paramname="msg"></param>
18privatevoidDownloadFile(stringsURL,stringFilename,stringmsg)
19{
20System.Net.HttpWebRequestURLReq;
21System.Net.HttpWebResponseURLRes;
22System.IO.FileStreamFileStreamer;
23intiBytesRead=0;
24longnum=0;
25try
26{
27FileStreamer=newSystem.IO.FileStream(Filename,System.IO.FileMode.Create);
28URLReq=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
29URLRes=(System.Net.HttpWebResponse)URLReq.GetResponse();
30System.IO.StreamsChunks=URLReq.GetResponse().GetResponseStream();
31longa=URLRes.ContentLength;
32byte[]bBuffer=newbyte[a+100];
33stringjsBlock=string.Empty;
34do
35{
36iBytesRead=sChunks.Read(bBuffer,0,(int)a+100);
37FileStreamer.Write(bBuffer,0,iBytesRead);
38num+=iBytesRead;
39inti=Convert.ToInt32(((double)num/(double)a)*100);
40jsBlock="<script"+">SetPorgressBar('"+msg+"','"+i.ToString()+"');<"+"/script>";
41Response.Write(jsBlock);
42Response.Flush();
43}
44while(iBytesRead!=0);
45sChunks.Close();
46FileStreamer.Close();
47}
48catch(Exceptionee)
49{
50Response.Write(ee.Message.ToString());
51Response.End();
52}
53}
54/**////<summary>
55///
56///</summary>
57///<paramname="sender"></param>
58///<paramname="e"></param>
59protectedvoidButton1_Click(objectsender,EventArgse)
60{
61stringjsBlock;
62//开始处理
63Response.Clear();
64WriteHeard();
65jsBlock="<script"+">document.getElementById(\"main\").style.display=\"block\";<"+"/script>";
66Response.Write(jsBlock);
67Response.Flush();
68jsBlock="<script"+">BeginTrans('开始下载安装加载程序');<"+"/script>";
69Response.Write(jsBlock);
70Response.Flush();
71//下载ZIPDLL
72stringURL="http://www.waybackintolove.cn/love/mp3/ICSharpCode.SharpZipLib.dll";
73
74System.IO.Directory.CreateDirectory(Server.MapPath("~/setuptemp"));
75stringfile=Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
76DownloadFile(URL,file,"下载安装加载程序");
77jsBlock="<script"+">EndTrans('处理完成。');<"+"/script>";
78Response.Write(jsBlock);
79Response.Flush();
80
81jsBlock="<script"+">BeginTrans('开始下载安装程序');<"+"/script>";
82Response.Write(jsBlock);
83Response.Flush();
84//远程程序地址
85URL="http://www.waybackintolove.cn/uploadfiles/WebSite.zip";
86file=Server.MapPath("~/setuptemp/WebSite.zip");
87DownloadFile(URL,file,"下载安装程序");
88jsBlock="<script"+">EndTrans('处理完成。');<"+"/script>";
89Response.Write(jsBlock);
90Response.Flush();
91
92jsBlock="<script"+">BeginTrans('正在解压安装程序');<"+"/script>";
93Response.Write(jsBlock);
94Response.Flush();
95//动态加载DLL
96try
97{
98stringzippath=Server.MapPath("~/setuptemp/WebSite.zip");
99stringdllpath=Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
100//System.Reflection.Assemblyasm=System.Reflection.Assembly.LoadFile(dllpath);
101//加载二进制代码,便于卸载dll
102byte[]assemblyInfo=System.IO.File.ReadAllBytes(dllpath);
103System.Reflection.Assemblyasm=System.Reflection.Assembly.Load(assemblyInfo);
104Typetype=asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipInputStream");
105objectobj=Activator.CreateInstance(type,System.IO.File.OpenRead(zippath));
106System.Reflection.MethodInfomethod=type.GetMethod("GetNextEntry");//方法的名称
107TypezipEntry=asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipEntry");
108objectzipEntryObj=method.ReturnType;
109stringTargetFilePath=Server.MapPath("~/");
110while((zipEntryObj=(object)method.Invoke(obj,null))!=null)
111{
112stringDirectoryName=System.IO.Path.GetDirectoryName((string)zipEntry.InvokeMember("Name",System.Reflection.BindingFlags.GetProperty,null,zipEntryObj,null));
113stringFileName=System.IO.Path.GetFileName((string)zipEntry.InvokeMember("Name",System.Reflection.BindingFlags.GetProperty,null,zipEntryObj,null));
114
115jsBlock="<script"+">BeginTrans('正在解压:"+FileName+"');<"+"/script>";
116Response.Write(jsBlock);
117Response.Flush();
118
119if(DirectoryName.Length>0)
120{
121if(!System.IO.Directory.Exists(TargetFilePath+DirectoryName))
122System.IO.Directory.CreateDirectory(TargetFilePath+DirectoryName);
123}
124if(!DirectoryName.EndsWith("\\"))
125{
126DirectoryName+="\\";
127}
128if(FileName!=String.Empty)
129{
130using(System.IO.FileStreamstreamWriter=System.IO.File.Create(TargetFilePath+(string)zipEntry.InvokeMember("Name",System.Reflection.BindingFlags.GetProperty,null,zipEntryObj,null)))
131{
132intsize=2048;
133byte[]data=newbyte[2048];
134while(true)
135{
136System.Reflection.MethodInfoZipmethod=type.GetMethod("Read");//方法的名称
137size=(int)Zipmethod.Invoke(obj,newobject[]{data,0,data.Length});
138if(size>0)
139{
140streamWriter.Write(data,0,size);
141}
142else
143{
144break;
145}
146}
147}
148}
149
150}
151}
152catch(Exceptionee)
153{
154Response.Write(ee.Message.ToString());
155//Response.End();
156}
157
158jsBlock="<script"+">BeginTrans('正在删除临时文件');<"+"/script>";
159Response.Write(jsBlock);
160Response.Flush();
161System.IO.Directory.Delete(Server.MapPath("~/setuptemp"),true);
162jsBlock="<script"+">EndTrans('删除临时文件完成。');<"+"/script>";
163Response.Write(jsBlock);
164Response.Flush();
165
166jsBlock="<script"+">BeginTrans('正在转正安装页');<"+"/script>";
167Response.Write(jsBlock);
168Response.Flush();
169jsBlock="<script"+">window.location.href='Defalut.aspx';<"+"/script>";
170Response.Write(jsBlock);
171Response.Flush();
172}
173
174protectedvoidWriteHeard()
175{
176stringstr=@"<htmlxmlns=""http://www.w3.org/1999/xhtml"">
177<headrunat=""server"">
178<title></title>
179</head>
180<script"+@"language=""javascript"">
181
182//开始处理
183functionBeginTrans(msg){
184WriteText(""Msg1"",msg);
185}
186
187//设置进度条进度
188functionSetPorgressBar(msg,pos){
189ProgressBar.style.width=pos+""%"";
190WriteText(""Msg1"",msg+""已完成""+pos+""%"");
191}
192
193//处理结束
194functionEndTrans(msg){
195if(msg=="""")
196WriteText(""Msg1"",""完成。"");
197else
198WriteText(""Msg1"",msg);
199}
200
201//设置时间信息
202functionSetTimeInfo(msg){
203WriteText(""Msg2"",msg);
204}
205
206//更新文本显示信息
207functionWriteText(id,str){
208varstrTag='<fontface=""Verdana,Arial,Helvetica""size=""2""color=""#ea9b02""><B>'+str+'</B></font>';
209if(document.all)document.all[id].innerHTML=strTag;
210}
211<"+@"/script>
212<body>
213<tablealign=""center""style=""height:100%;display:none""id=""main"">
214<trstyle=""height:45%""><td></td></tr>
215<tr>
216<td>
217<divid=""Msg1""style=""height:16px;""><fontface=""Verdana,Arial,Helvetica""size=""2""color=""#ea9b02""><b>正在加载</b></font></div>
218<divid=""ProgressBarSide""style=""width:300px;color:Silver;border-width:1px;border-style:Solid;"">
219<divid=""ProgressBar""align=""center""style=""height:20px;width:0%;background-color:#316AC5;""></div>
220</div>
221<divid=""Msg2""style=""height:16px;""><fontface=""Verdana,Arial,Helvetica""size=""2""color=""#ea9b02""><b></b></font></div>
222</td>
223</tr>
224<trstyle=""height:50%""><td></td></tr>
225</table>
226</body>
227</html>";
228Response.Write(str);
229Response.Flush();
230}
231
232</script>
233
234<htmlxmlns="http://www.w3.org/1999/xhtml">
235<headrunat="server">
236<title></title>
237</head>
238<body>
239<formid="form1"runat="server">
240<div>
241<asp:ButtonID="Button1"runat="server"onclick="Button1_Click"Text="在线安装"/>
242</div>
243</form>
244</body>
245</html>
246
进度条利用到了
http://www.cnblogs.com/anjou/archive/2006/10/27/541741.html
下面是安装过程。
1,上传单个文件到网站根目录
运行
http://beachblue.cn/Setup.aspx
点击在线安装
这样,本来要上传几十分钟的网站文件,不用一分钟就上传完成。
(因为是远程下载,服务器下载较快)
但由于解压后,增加了web.config,应用程序重启,所以不能删除自身,不能做到完美。
有解决方法的可以提出。
源码下载:
/Files/jannock/setup.rar
by : jannock
http://www.cnblogs.com/jannock/
-----------------------------------------
晕。。太多人在测试,大家下载源码后测试吧。。
那文件我在删了
优质内容筛选与推荐>>
1、算法一小时-选择排序2、CF449C:Jzzhu and Apples——题解3、html中事件调用JavaScript函数时有return与没有return的区别4、final 、finalize和finally的区别5、GIS部分理论知识备忘随笔