我是先从服务器下载zip压缩包,然后解压。
解压出错了,经过调试我发现原因所在。如果不下载,zip压缩包直接放在目录下解压是没问题。如果下载这个zip,然后再用这个zip就有问题,但是文件流已经关闭,不知道问题所在。我做了一个实验,打断点调试。等文件下载完后,马上进入断点,然后我把服务器上的文件拷贝下来,覆盖我这个已经下载的文件,这样再执行下面的解压代码就没问题,所以我才怀疑是不是文件资源被占用了,可是我已经关闭了文件流了,求解。
代码如下
方法:
/// <summary> /// 下载 /// </summary> /// <param name="url">下载的url</param> /// <param name="savefilepath">文件保存的目录</param> /// <param name="pro">需要重启的程序</param> private void LoadZIP(string url, string savefilepath, string pro) {
System.Security.Policy.Url Url = new System.Security.Policy.Url(url); System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url.Value); long fileSizeAll = Request.GetResponse().ContentLength; Request.Abort(); byte[] buffer = new byte[1024]; // 接收缓冲区 int readSize = 0; // 接收字节数 int AllReadSize = 0; System.IO.FileStream fs = new System.IO.FileStream(savefilepath, System.IO.FileMode.Create); System.IO.Stream ns = null;
try { System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url.Value); ns = request.GetResponse().GetResponseStream(); readSize = ns.Read(buffer, 0, 1024); int index = 0; int step = 0; this.txttotal.Text = fileSizeAll.ToString();
while (readSize > 0) { index++; AllReadSize += readSize; fs.Write(buffer, 0, readSize); readSize = ns.Read(buffer, 0, 1024);
this.txtaccept.Text = AllReadSize.ToString(); if (fileSizeAll > 1024) { int cs = (int)fileSizeAll / 1024; if (cs > 100) { step = cs / 100; if (step == index) { if (pro_load.InvokeRequired) { this.Invoke(new Action<int>((x1) => { pro_load.Value += step; }), step); } else { pro_load.Value += step; } } else { index++; } } else { step = 100 / cs; if (pro_load.InvokeRequired) { this.Invoke(new Action<int>((x1) => { pro_load.Value += 1; }), step); } else { pro_load.Value += 1; }
}
} } if (pro_load.Value < 100) { pro_load.Value = 100; } fs.Close(); fs.Dispose(); ns.Close(); ns.Dispose(); toolStripStatusLabel2.Text = "正在解压. . ."; System.Threading.Thread.Sleep(1000); pro_ExtractZip.Value = 50; ExtractZip(savefilepath, Application.StartupPath+@"\"); pro_ExtractZip.Value = 100; toolStripStatusLabel2.Text = "更新完毕"; if (System.IO.File.Exists(savefilepath)) { System.IO.File.Delete(savefilepath); } System.Diagnostics.Process.Start(pro); Application.Exit(); } catch (Exception er) { MessageBox.Show(er.Message); } }
/// <summary> /// 压缩文件 /// </summary> /// <param name="zip">要解压的ZIP文件</param> /// <param name="path">要解压到那个目录</param> private void ExtractZip(string zippath, string path) { (new ICSharpCode.SharpZipLib.Zip.FastZip()).ExtractZip(zippath.ToString(), path.ToString(), ""); }
方法的调用:
private void Mian_Load(object sender, EventArgs e) { //bool result = false; //ExtractZip(@"D:\C#工程\WinUpdate\UpdateClient\bin\Debug\YPH1308002548.zip", @"D:\C#工程\WinUpdate\UpdateClient\bin\Debug\"); try { up.UpdateServerSoapClient client = new UpdateClient.up.UpdateServerSoapClient();
string str = client.Getfile(System.Configuration.ConfigurationManager.AppSettings["DirectoryName"]); if (str != string.Empty) { string[] file = str.Split(','); if (file.Length == 2) { string md5 = UpdateClient.Properties.Settings.Default.MD5.ToString().Trim(); if (file[1] != md5) { if (DialogResult.OK == MessageBox.Show("确定要更新到最新程序?", "程序升级", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)) { this.Visible = true; string proname = System.Configuration.ConfigurationManager.AppSettings["ProcessName"]; foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { if (p.ProcessName.ToUpper().Equals(proname) || p.ProcessName.ToUpper().Equals(proname.Substring(0,proname.Length-4))) //这里不要后缀名 p.Kill(); } string url = System.Configuration.ConfigurationManager.AppSettings["URL"]; if (url.Substring(url.Length - 1, 1) != "/") url += "/"; url += file[0]; LoadZIP(System.Configuration.ConfigurationManager.AppSettings["URL"], Application.StartupPath + @"\"+file[0], Application.StartupPath + @"\" + proname); UpdateClient.Properties.Settings.Default.MD5 = md5; UpdateClient.Properties.Settings.Default.Save(); } } }
} } catch(Exception ex) { //错误日志 Application.Exit(); } Application.Exit();
}
错误就在: (new ICSharpCode.SharpZipLib.Zip.FastZip()).ExtractZip(zippath.ToString(), path.ToString(), "");
文件路劲对的哦
应该是你下载的代码有问题,改变了原有的文件。
是的,谢谢解答。
我把这个代码,负责到一个新的解决方案就好了,不知道啥问题
@夜星冷: 好吧,你赢了。