/// <summary> /// 解压ZIP压缩包到指定的目录下 /// </summary> /// <param name="fileToUnZip"></param> /// <param name="zipedFolder"></param> /// <param name="password"></param> /// <returns></returns> private bool UnZip(string fileToUnZip, string zipedFolder, string password = null) { bool result = true; FileStream fs = null; ZipInputStream zipStream = null; ZipEntry ent = null; string fileName; if (!File.Exists(fileToUnZip)) return false; if (!Directory.Exists(zipedFolder)) Directory.CreateDirectory(zipedFolder); try { zipStream = new ZipInputStream(File.OpenRead(fileToUnZip.Trim())); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; while ((ent = zipStream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(ent.Name)) { fileName = Path.Combine(zipedFolder, ent.Name); fileName = fileName.Replace('/', '\\'); if (fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue; } using (fs = File.Create(fileName)) { int size = 1024 * 50; byte[] data = new byte[size]; while (true) { size = zipStream.Read(data, 0, data.Length); if (size > 0) fs.Write(data, 0, size); else break; } } } } } catch { result = false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (zipStream != null) { zipStream.Close(); zipStream.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } return result; }
使用
string str = System.Windows.Forms.Application.StartupPath + @"\NewVersions"; if (!Directory.Exists(str)) Directory.CreateDirectory(str); zips = service.GetZips(); string url = str + @"\" + zips[0]; string DownUrl = service.GetUrl(); bool flag = DownloadFile(DownUrl, url, this.progressBar1, label6); if (flag) { // 解压到 NewVersions if (UnZip(url, str) == false) { MessageBox.Show("升级包解压失败."); return; } // 删除 NewVersions 下的压缩包 System.IO.File.Delete(url); // 更新版本信息文件 IniClass ini = new IniClass(System.Windows.Forms.Application.StartupPath + @"\config\UpdateVersions.ini"); ini.IniWriteValue("UpdateVersions", "version", label4.Text); // 启动 bat 脚本 cmd("reboot.bat"); // 退出当前应用程序 Process.GetCurrentProcess().Kill();//此方法完全奏效,绝对是完全退出。 }
上一篇: c# 下载ZIP压缩包到指定的目录下
下一篇: zblog 获取数据的2个函数
0条评论