简单实现WEB程序在线安装 (附源码)
大家都发觉,在安装WEB程序时,最麻烦之处在于上传网站程序文件,大多数虚似主机只提供FTP功能,但有时网速较慢的时候,上传几M的文件就用了差不多一个多钟,尤其当文件较多的时候。等待上传文件的时间就够等。或者大家都会想到,如果能打包起来,上传后再解包,这样就好了。
这里提供的方法原理基本上是一样,主要利用 ICSharpCode.SharpZipLib.dll组件实现在线解压。
由于还利用到了DLL的动态加载,所以在这里记下来。分享给大家。
源代码如下:
Code
1<%@ Page Language="C#" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<script runat="server">
6 protected void Page_Load(object sender, EventArgs e)
7 {
8 //设置脚本执行时间,防止超时
9 Server.ScriptTimeout = 999999999;
10 }
11
12 /**//// <summary>
13 /// 远程下载文件
14 /// </summary>
15 /// <param name="sURL"></param>
16 /// <param name="Filename"></param>
17 /// <param name="msg"></param>
18 private void DownloadFile(string sURL,string Filename,string msg)
19 {
20 System.Net.HttpWebRequest URLReq;
21 System.Net.HttpWebResponse URLRes;
22 System.IO.FileStream FileStreamer;
23 int iBytesRead = 0;
24 long num = 0;
25 try
26 {
27 FileStreamer = new System.IO.FileStream(Filename, System.IO.FileMode.Create);
28 URLReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
29 URLRes = (System.Net.HttpWebResponse)URLReq.GetResponse();
30 System.IO.Stream sChunks = URLReq.GetResponse().GetResponseStream();
31 long a = URLRes.ContentLength;
32 byte[] bBuffer = new byte[a+100];
33 string jsBlock=string.Empty;
34 do
35 {
36 iBytesRead = sChunks.Read(bBuffer, 0, (int)a+100);
37 FileStreamer.Write(bBuffer, 0, iBytesRead);
38 num += iBytesRead;
39 int i =Convert.ToInt32(((double)num / (double)a)*100);
40 jsBlock = "<script" + ">SetPorgressBar('"+msg+"','" + i.ToString() + "');<" + "/script>";
41 Response.Write(jsBlock);
42 Response.Flush();
43 }
44 while (iBytesRead != 0);
45 sChunks.Close();
46 FileStreamer.Close();
47 }
48 catch (Exception ee)
49 {
50 Response.Write(ee.Message.ToString());
51 Response.End();
52 }
53 }
54 /**//// <summary>
55 ///
56 /// </summary>
57 /// <param name="sender"></param>
58 /// <param name="e"></param>
59 protected void Button1_Click(object sender, EventArgs e)
60 {
61 string jsBlock;
62 // 开始处理
63 Response.Clear();
64 WriteHeard();
65 jsBlock = "<script" + ">document.getElementById(\"main\").style.display = \"block\";<" + "/script>";
66 Response.Write(jsBlock);
67 Response.Flush();
68 jsBlock = "<script" + ">BeginTrans('开始下载安装加载程序');<" + "/script>";
69 Response.Write(jsBlock);
70 Response.Flush();
71 //下载ZIP DLL
72 string URL = "http://www.waybackintolove.cn/love/mp3/ICSharpCode.SharpZipLib.dll";
73
74 System.IO.Directory.CreateDirectory(Server.MapPath("~/setuptemp"));
75 string file = Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
76 DownloadFile(URL, file, "下载安装加载程序");
77 jsBlock = "<script" + ">EndTrans('处理完成。');<" + "/script>";
78 Response.Write(jsBlock);
79 Response.Flush();
80
81 jsBlock = "<script" + ">BeginTrans('开始下载安装程序');<" + "/script>";
82 Response.Write(jsBlock);
83 Response.Flush();
84 //远程程序地址
85 URL = "http://www.waybackintolove.cn/uploadfiles/WebSite.zip";
86 file = Server.MapPath("~/setuptemp/WebSite.zip");
87 DownloadFile(URL, file, "下载安装程序");
88 jsBlock = "<script" + ">EndTrans('处理完成。');<" + "/script>";
89 Response.Write(jsBlock);
90 Response.Flush();
91
92 jsBlock = "<script" + ">BeginTrans('正在解压安装程序');<" + "/script>";
93 Response.Write(jsBlock);
94 Response.Flush();
95 //动态加载DLL
96 try
97 {
98 string zippath=Server.MapPath("~/setuptemp/WebSite.zip");
99 string dllpath = Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
100 //System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(dllpath);
101 //加载二进制代码,便于卸载dll
102 byte[] assemblyInfo = System.IO.File.ReadAllBytes(dllpath);
103 System.Reflection.Assembly asm = System.Reflection.Assembly.Load(assemblyInfo);
104 Type type = asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipInputStream");
105 object obj = Activator.CreateInstance(type, System.IO.File.OpenRead(zippath));
106 System.Reflection.MethodInfo method = type.GetMethod("GetNextEntry");//方法的名称
107 Type zipEntry = asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipEntry");
108 object zipEntryObj = method.ReturnType;
109 string TargetFilePath = Server.MapPath("~/");
110 while ((zipEntryObj = (object)method.Invoke(obj, null)) != null)
111 {
112 string DirectoryName = System.IO.Path.GetDirectoryName((string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null));
113 string FileName = System.IO.Path.GetFileName((string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null));
114
115 jsBlock = "<script" + ">BeginTrans('正在解压:"+FileName +"');<" + "/script>";
116 Response.Write(jsBlock);
117 Response.Flush();
118
119 if (DirectoryName.Length > 0)
120 {
121 if (!System.IO.Directory.Exists(TargetFilePath + DirectoryName))
122 System.IO.Directory.CreateDirectory(TargetFilePath + DirectoryName);
123 }
124 if (!DirectoryName.EndsWith("\\"))
125 {
126 DirectoryName += "\\";
127 }
128 if (FileName != String.Empty)
129 {
130 using (System.IO.FileStream streamWriter = System.IO.File.Create(TargetFilePath + (string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null)))
131 {
132 int size = 2048;
133 byte[] data = new byte[2048];
134 while (true)
135 {
136 System.Reflection.MethodInfo Zipmethod = type.GetMethod("Read");//方法的名称
137 size = (int)Zipmethod.Invoke(obj, new object[] { data, 0, data.Length });
138 if (size > 0)
139 {
140 streamWriter.Write(data, 0, size);
141 }
142 else
143 {
144 break;
145 }
146 }
147 }
148 }
149
150 }
151 }
152 catch (Exception ee)
153 {
154 Response.Write(ee.Message.ToString());
155 //Response.End();
156 }
157
158 jsBlock = "<script" + ">BeginTrans('正在删除临时文件');<" + "/script>";
159 Response.Write(jsBlock);
160 Response.Flush();
161 System.IO.Directory.Delete(Server.MapPath("~/setuptemp"), true);
162 jsBlock = "<script" + ">EndTrans('删除临时文件完成。');<" + "/script>";
163 Response.Write(jsBlock);
164 Response.Flush();
165
166 jsBlock = "<script" + ">BeginTrans('正在转正安装页');<" + "/script>";
167 Response.Write(jsBlock);
168 Response.Flush();
169 jsBlock = "<script" + ">window.location.href='Defalut.aspx';<" + "/script>";
170 Response.Write(jsBlock);
171 Response.Flush();
172 }
173
174 protected void WriteHeard()
175 {
176 string str=@"<html xmlns=""http://www.w3.org/1999/xhtml"">
177<head runat=""server"">
178 <title></title>
179</head>
180<script" + @" language=""javascript"">
181
182 //开始处理
183 function BeginTrans(msg) {
184 WriteText(""Msg1"", msg);
185 }
186
187 //设置进度条进度
188 function SetPorgressBar(msg, pos) {
189 ProgressBar.style.width = pos + ""%"";
190 WriteText(""Msg1"", msg + "" 已完成"" + pos + ""%"");
191 }
192
193 //处理结束
194 function EndTrans(msg) {
195 if (msg == """")
196 WriteText(""Msg1"", ""完成。"");
197 else
198 WriteText(""Msg1"", msg);
199 }
200
201 //设置时间信息
202 function SetTimeInfo(msg) {
203 WriteText(""Msg2"", msg);
204 }
205
206 // 更新文本显示信息
207 function WriteText(id, str) {
208 var strTag = '<font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><B>' + str + '</B></font>';
209 if (document.all) document.all[id].innerHTML = strTag;
210 }
211<" + @"/script>
212<body>
213<table align=""center"" style=""height:100%;display:none"" id=""main"">
214 <tr style=""height:45%""><td></td></tr>
215 <tr>
216 <td>
217 <div id=""Msg1"" style=""height:16px;""><font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><b>正在加载</b></font></div>
218 <div id=""ProgressBarSide"" style=""width:300px; color:Silver;border-width:1px; border-style:Solid;"">
219 <div id=""ProgressBar"" align=""center"" style=""height:20px; width:0%; background-color:#316AC5;""></div>
220 </div>
221 <div id=""Msg2"" style=""height:16px;""><font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><b></b></font></div>
222 </td>
223 </tr>
224 <tr style=""height:50%""><td></td></tr>
225</table>
226</body>
227</html>";
228 Response.Write(str);
229 Response.Flush();
230 }
231
232</script>
233
234<html xmlns="http://www.w3.org/1999/xhtml">
235<head runat="server">
236 <title></title>
237</head>
238<body>
239 <form id="form1" runat="server">
240 <div>
241 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="在线安装" />
242 </div>
243 </form>
244</body>
245</html>
246
1<%@ Page Language="C#" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<script runat="server">
6 protected void Page_Load(object sender, EventArgs e)
7 {
8 //设置脚本执行时间,防止超时
9 Server.ScriptTimeout = 999999999;
10 }
11
12 /**//// <summary>
13 /// 远程下载文件
14 /// </summary>
15 /// <param name="sURL"></param>
16 /// <param name="Filename"></param>
17 /// <param name="msg"></param>
18 private void DownloadFile(string sURL,string Filename,string msg)
19 {
20 System.Net.HttpWebRequest URLReq;
21 System.Net.HttpWebResponse URLRes;
22 System.IO.FileStream FileStreamer;
23 int iBytesRead = 0;
24 long num = 0;
25 try
26 {
27 FileStreamer = new System.IO.FileStream(Filename, System.IO.FileMode.Create);
28 URLReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
29 URLRes = (System.Net.HttpWebResponse)URLReq.GetResponse();
30 System.IO.Stream sChunks = URLReq.GetResponse().GetResponseStream();
31 long a = URLRes.ContentLength;
32 byte[] bBuffer = new byte[a+100];
33 string jsBlock=string.Empty;
34 do
35 {
36 iBytesRead = sChunks.Read(bBuffer, 0, (int)a+100);
37 FileStreamer.Write(bBuffer, 0, iBytesRead);
38 num += iBytesRead;
39 int i =Convert.ToInt32(((double)num / (double)a)*100);
40 jsBlock = "<script" + ">SetPorgressBar('"+msg+"','" + i.ToString() + "');<" + "/script>";
41 Response.Write(jsBlock);
42 Response.Flush();
43 }
44 while (iBytesRead != 0);
45 sChunks.Close();
46 FileStreamer.Close();
47 }
48 catch (Exception ee)
49 {
50 Response.Write(ee.Message.ToString());
51 Response.End();
52 }
53 }
54 /**//// <summary>
55 ///
56 /// </summary>
57 /// <param name="sender"></param>
58 /// <param name="e"></param>
59 protected void Button1_Click(object sender, EventArgs e)
60 {
61 string jsBlock;
62 // 开始处理
63 Response.Clear();
64 WriteHeard();
65 jsBlock = "<script" + ">document.getElementById(\"main\").style.display = \"block\";<" + "/script>";
66 Response.Write(jsBlock);
67 Response.Flush();
68 jsBlock = "<script" + ">BeginTrans('开始下载安装加载程序');<" + "/script>";
69 Response.Write(jsBlock);
70 Response.Flush();
71 //下载ZIP DLL
72 string URL = "http://www.waybackintolove.cn/love/mp3/ICSharpCode.SharpZipLib.dll";
73
74 System.IO.Directory.CreateDirectory(Server.MapPath("~/setuptemp"));
75 string file = Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
76 DownloadFile(URL, file, "下载安装加载程序");
77 jsBlock = "<script" + ">EndTrans('处理完成。');<" + "/script>";
78 Response.Write(jsBlock);
79 Response.Flush();
80
81 jsBlock = "<script" + ">BeginTrans('开始下载安装程序');<" + "/script>";
82 Response.Write(jsBlock);
83 Response.Flush();
84 //远程程序地址
85 URL = "http://www.waybackintolove.cn/uploadfiles/WebSite.zip";
86 file = Server.MapPath("~/setuptemp/WebSite.zip");
87 DownloadFile(URL, file, "下载安装程序");
88 jsBlock = "<script" + ">EndTrans('处理完成。');<" + "/script>";
89 Response.Write(jsBlock);
90 Response.Flush();
91
92 jsBlock = "<script" + ">BeginTrans('正在解压安装程序');<" + "/script>";
93 Response.Write(jsBlock);
94 Response.Flush();
95 //动态加载DLL
96 try
97 {
98 string zippath=Server.MapPath("~/setuptemp/WebSite.zip");
99 string dllpath = Server.MapPath("~/setuptemp/ICSharpCode.SharpZipLib.dll");
100 //System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(dllpath);
101 //加载二进制代码,便于卸载dll
102 byte[] assemblyInfo = System.IO.File.ReadAllBytes(dllpath);
103 System.Reflection.Assembly asm = System.Reflection.Assembly.Load(assemblyInfo);
104 Type type = asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipInputStream");
105 object obj = Activator.CreateInstance(type, System.IO.File.OpenRead(zippath));
106 System.Reflection.MethodInfo method = type.GetMethod("GetNextEntry");//方法的名称
107 Type zipEntry = asm.GetType("ICSharpCode.SharpZipLib.Zip.ZipEntry");
108 object zipEntryObj = method.ReturnType;
109 string TargetFilePath = Server.MapPath("~/");
110 while ((zipEntryObj = (object)method.Invoke(obj, null)) != null)
111 {
112 string DirectoryName = System.IO.Path.GetDirectoryName((string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null));
113 string FileName = System.IO.Path.GetFileName((string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null));
114
115 jsBlock = "<script" + ">BeginTrans('正在解压:"+FileName +"');<" + "/script>";
116 Response.Write(jsBlock);
117 Response.Flush();
118
119 if (DirectoryName.Length > 0)
120 {
121 if (!System.IO.Directory.Exists(TargetFilePath + DirectoryName))
122 System.IO.Directory.CreateDirectory(TargetFilePath + DirectoryName);
123 }
124 if (!DirectoryName.EndsWith("\\"))
125 {
126 DirectoryName += "\\";
127 }
128 if (FileName != String.Empty)
129 {
130 using (System.IO.FileStream streamWriter = System.IO.File.Create(TargetFilePath + (string)zipEntry.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, zipEntryObj, null)))
131 {
132 int size = 2048;
133 byte[] data = new byte[2048];
134 while (true)
135 {
136 System.Reflection.MethodInfo Zipmethod = type.GetMethod("Read");//方法的名称
137 size = (int)Zipmethod.Invoke(obj, new object[] { data, 0, data.Length });
138 if (size > 0)
139 {
140 streamWriter.Write(data, 0, size);
141 }
142 else
143 {
144 break;
145 }
146 }
147 }
148 }
149
150 }
151 }
152 catch (Exception ee)
153 {
154 Response.Write(ee.Message.ToString());
155 //Response.End();
156 }
157
158 jsBlock = "<script" + ">BeginTrans('正在删除临时文件');<" + "/script>";
159 Response.Write(jsBlock);
160 Response.Flush();
161 System.IO.Directory.Delete(Server.MapPath("~/setuptemp"), true);
162 jsBlock = "<script" + ">EndTrans('删除临时文件完成。');<" + "/script>";
163 Response.Write(jsBlock);
164 Response.Flush();
165
166 jsBlock = "<script" + ">BeginTrans('正在转正安装页');<" + "/script>";
167 Response.Write(jsBlock);
168 Response.Flush();
169 jsBlock = "<script" + ">window.location.href='Defalut.aspx';<" + "/script>";
170 Response.Write(jsBlock);
171 Response.Flush();
172 }
173
174 protected void WriteHeard()
175 {
176 string str=@"<html xmlns=""http://www.w3.org/1999/xhtml"">
177<head runat=""server"">
178 <title></title>
179</head>
180<script" + @" language=""javascript"">
181
182 //开始处理
183 function BeginTrans(msg) {
184 WriteText(""Msg1"", msg);
185 }
186
187 //设置进度条进度
188 function SetPorgressBar(msg, pos) {
189 ProgressBar.style.width = pos + ""%"";
190 WriteText(""Msg1"", msg + "" 已完成"" + pos + ""%"");
191 }
192
193 //处理结束
194 function EndTrans(msg) {
195 if (msg == """")
196 WriteText(""Msg1"", ""完成。"");
197 else
198 WriteText(""Msg1"", msg);
199 }
200
201 //设置时间信息
202 function SetTimeInfo(msg) {
203 WriteText(""Msg2"", msg);
204 }
205
206 // 更新文本显示信息
207 function WriteText(id, str) {
208 var strTag = '<font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><B>' + str + '</B></font>';
209 if (document.all) document.all[id].innerHTML = strTag;
210 }
211<" + @"/script>
212<body>
213<table align=""center"" style=""height:100%;display:none"" id=""main"">
214 <tr style=""height:45%""><td></td></tr>
215 <tr>
216 <td>
217 <div id=""Msg1"" style=""height:16px;""><font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><b>正在加载</b></font></div>
218 <div id=""ProgressBarSide"" style=""width:300px; color:Silver;border-width:1px; border-style:Solid;"">
219 <div id=""ProgressBar"" align=""center"" style=""height:20px; width:0%; background-color:#316AC5;""></div>
220 </div>
221 <div id=""Msg2"" style=""height:16px;""><font face=""Verdana, Arial, Helvetica"" size=""2"" color=""#ea9b02""><b></b></font></div>
222 </td>
223 </tr>
224 <tr style=""height:50%""><td></td></tr>
225</table>
226</body>
227</html>";
228 Response.Write(str);
229 Response.Flush();
230 }
231
232</script>
233
234<html xmlns="http://www.w3.org/1999/xhtml">
235<head runat="server">
236 <title></title>
237</head>
238<body>
239 <form id="form1" runat="server">
240 <div>
241 <asp:Button ID="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,应用程序重启,所以不能删除自身,不能做到完美。
有解决方法的可以提出。
源码下载:
by : jannock
http://www.cnblogs.com/jannock/
-----------------------------------------
晕。。太多人在测试,大家下载源码后测试吧。。
那文件我在删了