Browse Source

修改更新工具(及文件路径处理等工具类)

yuzhengyang 8 years ago
parent
commit
8eaf5a8a1a

+ 7 - 1
Fork.Net/Test/Y.Test/Commons/R.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
+using Y.Utils.IOUtils.PathUtils;
 using Y.Utils.WindowsUtils.FormUtils;
 using Y.Utils.WindowsUtils.FormUtils;
 
 
 namespace Y.Test.Commons
 namespace Y.Test.Commons
@@ -11,9 +12,14 @@ namespace Y.Test.Commons
         public static class Paths
         public static class Paths
         {
         {
             public static string Temp = @"D:\Temp\AppUpdate\Oreo.NetMan\Temp";
             public static string Temp = @"D:\Temp\AppUpdate\Oreo.NetMan\Temp";
+            //public static Dictionary<string, string> Relative = new Dictionary<string, string>()
+            //{
+            //    { "|AppRoot|",@"D:\Temp\AppUpdate\Oreo.NetMan"},
+            //};
+            public static string App = AppDomain.CurrentDomain.BaseDirectory;
             public static Dictionary<string, string> Relative = new Dictionary<string, string>()
             public static Dictionary<string, string> Relative = new Dictionary<string, string>()
             {
             {
-                { "|AppRoot|",@"D:\Temp\AppUpdate\Oreo.NetMan"},
+                { "|AppRoot|",DirTool.GetFilePath(App)},
             };
             };
         }
         }
         public static FormManTool Forms = new FormManTool();
         public static FormManTool Forms = new FormManTool();

+ 6 - 0
Fork.Net/Test/Y.Test/Program.cs

@@ -3,8 +3,10 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Windows.Forms;
+using Y.Test.Commons;
 using Y.Test.Views;
 using Y.Test.Views;
 using Y.Utils.IOUtils.FileUtils;
 using Y.Utils.IOUtils.FileUtils;
+using Y.Utils.IOUtils.PathUtils;
 
 
 namespace Y.Test
 namespace Y.Test
 {
 {
@@ -16,6 +18,10 @@ namespace Y.Test
         [STAThread]
         [STAThread]
         static void Main()
         static void Main()
         {
         {
+            //var a = DirTool.Parent(@"D:\Temp\流量测试\");
+            //var b = DirTool.Parent(@"D:\Temp");
+            //var c = DirTool.Parent(@"D:\");
+            //var d = DirTool.Parent(@"\\\\");
             //FilePackageTool.Pack(@"D:\Temp\流量测试\Oreo.NetMan", @"D:\Temp\流量测试\bag.bag");
             //FilePackageTool.Pack(@"D:\Temp\流量测试\Oreo.NetMan", @"D:\Temp\流量测试\bag.bag");
             Application.EnableVisualStyles();
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.SetCompatibleTextRenderingDefault(false);

+ 7 - 7
Fork.Net/Test/Y.Test/Views/TestUpdateForm.cs

@@ -25,19 +25,19 @@ namespace Y.Test.Views
 
 
         private void TestUpdateForm_Load(object sender, EventArgs e)
         private void TestUpdateForm_Load(object sender, EventArgs e)
         {
         {
-            FilePackageTool.Pack(@"D:\Temp\AppUpdate\Temp\6.0.0.2版本打包", @"D:\FTP\AppUpdateBag\Oreo.NetMan[6.0.0.2].updatebag");
+            FilePackageTool.Pack(@"D:\CoCo\Work\IMOS小助手\IMOS.Assistant\程序发布\1.0.0.0", @"D:\CoCo\Work\IMOS小助手\IMOS.Assistant\程序发布\所有版本打包\Assistant[1.0.0.0].udp");
         }
         }
 
 
         private void button1_Click(object sender, EventArgs e)
         private void button1_Click(object sender, EventArgs e)
         {
         {
             Task.Factory.StartNew(() =>
             Task.Factory.StartNew(() =>
             {
             {
-                AppUpdateTool aut = new AppUpdateTool();
-                int updateCode = aut.Update("Oreo.NetMan", new Version(Application.ProductVersion),
-                    "http://localhost:20001/Update/Get?name=lalala",
-                    R.Paths.Temp, R.Paths.Relative,
-                    UIDownProgress, null, UIUnpackProgress, null);
-                UIUpdateCode(updateCode);
+                //AppUpdateTool aut = new AppUpdateTool();
+                //int updateCode = aut.Update("Oreo.NetMan", new Version(Application.ProductVersion),
+                //    "http://localhost:20001/Update/Get?name=lalala",
+                //    R.Paths.Temp, R.Paths.Relative,
+                //    UIDownProgress, null, UIUnpackProgress, null);
+                //UIUpdateCode(updateCode);
             });
             });
         }
         }
         private void UIDownProgress(object sender, ProgressEventArgs e)
         private void UIDownProgress(object sender, ProgressEventArgs e)

+ 2 - 0
Fork.Net/Y.Utils/IOUtils/FileUtils/FilePackageTool.cs

@@ -40,6 +40,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -11;//要打包的路径不存在
         /// -11;//要打包的路径不存在
         /// -12;//打包后的目标文件已存在
         /// -12;//打包后的目标文件已存在
         /// -13;//要打包的路径中没有文件
         /// -13;//要打包的路径中没有文件
+        /// -14;//输出文件夹不存在
         /// -404;//未知错误,操作失败
         /// -404;//未知错误,操作失败
         /// </returns>
         /// </returns>
         public static int Pack(string srcPath, string dstFile, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
         public static int Pack(string srcPath, string dstFile, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
@@ -47,6 +48,7 @@ namespace Y.Utils.IOUtils.FileUtils
             DateTime beginTime = DateTime.Now;
             DateTime beginTime = DateTime.Now;
             if (!Directory.Exists(srcPath)) return -11;//要打包的路径不存在
             if (!Directory.Exists(srcPath)) return -11;//要打包的路径不存在
             if (File.Exists(dstFile) && !overwrite) return -12;//打包后的目标文件已存在
             if (File.Exists(dstFile) && !overwrite) return -12;//打包后的目标文件已存在
+            if (!DirTool.Create(DirTool.GetFilePath(dstFile))) return -14;//输出文件夹不存在
 
 
             List<string> tempfiles = FileTool.GetAllFile(srcPath);
             List<string> tempfiles = FileTool.GetAllFile(srcPath);
             List<FilePackageModel> files = CreateFilePackageModel(tempfiles, srcPath);
             List<FilePackageModel> files = CreateFilePackageModel(tempfiles, srcPath);

+ 14 - 5
Fork.Net/Y.Utils/IOUtils/PathUtils/DirTool.cs

@@ -1,7 +1,7 @@
 //************************************************************************
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.3.29 - 2017.6.27
+//      date:       2017.3.29 - 2017.8.7
 //      desc:       文件目录工具类
 //      desc:       文件目录工具类
 //      Copyright (c) yuzhengyang. All rights reserved.
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 //************************************************************************
@@ -12,6 +12,7 @@ using System.IO;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
 using Y.Utils.DataUtils.Collections;
 using Y.Utils.DataUtils.Collections;
+using Y.Utils.DataUtils.StringUtils;
 
 
 namespace Y.Utils.IOUtils.PathUtils
 namespace Y.Utils.IOUtils.PathUtils
 {
 {
@@ -49,12 +50,20 @@ namespace Y.Utils.IOUtils.PathUtils
         /// <returns></returns>
         /// <returns></returns>
         public static string Parent(string path)
         public static string Parent(string path)
         {
         {
-            try
+            string p = path;
+            if (!string.IsNullOrWhiteSpace(p))
             {
             {
-                return Directory.GetParent(path).ToString();
+                while (p.EndsWith("\\")) p = p.Substring(0, p.Length - 1);
+                if (StringTool.SubStringCount(p, "\\") >= 1)
+                {
+                    try
+                    {
+                        return Directory.GetParent(p).ToString();
+                    }
+                    catch (Exception e) { }
+                }
             }
             }
-            catch (Exception e) { }
-            return null;
+            return p;
         }
         }
         /// <summary>
         /// <summary>
         /// 获取目录下的目录(一层)
         /// 获取目录下的目录(一层)

+ 8 - 1
Fork.Net/Y.Utils/UpdateUtils/AppUpdateInfo.cs

@@ -1,7 +1,7 @@
 //************************************************************************
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.8.1 - 2017.8.3
+//      date:       2017.8.1 - 2017.8.7
 //      desc:       程序更新工具模型
 //      desc:       程序更新工具模型
 //      Copyright (c) yuzhengyang. All rights reserved.
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 //************************************************************************
@@ -12,6 +12,9 @@ using System.Text;
 
 
 namespace Y.Utils.UpdateUtils
 namespace Y.Utils.UpdateUtils
 {
 {
+    /// <summary>
+    /// 程序更新信息
+    /// </summary>
     public class AppUpdateInfo
     public class AppUpdateInfo
     {
     {
         /// <summary>
         /// <summary>
@@ -23,6 +26,10 @@ namespace Y.Utils.UpdateUtils
         /// </summary>
         /// </summary>
         public string Version { get; set; }
         public string Version { get; set; }
         /// <summary>
         /// <summary>
+        /// 新版本描述(Readme)
+        /// </summary>
+        public string Desc { get; set; }
+        /// <summary>
         /// 作者
         /// 作者
         /// </summary>
         /// </summary>
         public string Author { get; set; }
         public string Author { get; set; }

+ 59 - 42
Fork.Net/Y.Utils/UpdateUtils/AppUpdateTool.cs

@@ -25,74 +25,91 @@ namespace Y.Utils.UpdateUtils
     public class AppUpdateTool
     public class AppUpdateTool
     {
     {
         /// <summary>
         /// <summary>
+        /// 获取新版本
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="name"></param>
+        /// <param name="version"></param>
+        /// <param name="info"></param>
+        /// <returns>
+        /// -10;//请求版本失败
+        /// -20;//没有更新的版本
+        /// </returns>
+        public int GetNewVersion(string url, string name, Version version, out AppUpdateInfo info)
+        {
+            Stopwatch stopwatch = new Stopwatch();
+            stopwatch.Start();
+
+            info = HttpTool.Get<AppUpdateInfo>(string.Format("{0}?name={1}", url, name));
+            if (info != null)
+            {
+                Version newVersion = FormatVersion(info.Version);
+                if (newVersion != null && newVersion > version)
+                {
+                    stopwatch.Stop();
+                    return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
+                }
+                else
+                {
+                    return -20;//没有更新的版本
+                }
+            }
+            return -10;//请求版本失败
+        }
+        /// <summary>
         /// 更新
         /// 更新
         /// </summary>
         /// </summary>
-        /// <param name="name">功能名称</param>
-        /// <param name="version">当前版本号</param>
-        /// <param name="url">请求新版本地址</param>
-        /// <param name="path">文件下载位置</param>
+        /// <param name="info">新版本信息</param>
+        /// <param name="tempPath">文件下载位置</param>
         /// <param name="dictionary">文件相对位置字典</param>
         /// <param name="dictionary">文件相对位置字典</param>
-        /// <param name="downprogress">下载进度回调</param>
-        /// <param name="downsender">下载进度事件数据</param>
-        /// <param name="releaseprogress">释放进度回调</param>
-        /// <param name="releasesender">释放进度事件数据</param>
+        /// <param name="downProgress">下载进度回调</param>
+        /// <param name="downSender">下载进度事件数据</param>
+        /// <param name="releaseProgress">释放进度回调</param>
+        /// <param name="releaseSender">释放进度事件数据</param>
         /// <returns>
         /// <returns>
-        /// -10000;//无最新版本,停止操作
-        /// -20000;//请求服务器最新版本失败
-        /// -30000;//新版本号格式不正确,解析失败
-        /// -40000;//文件下载失败
-        /// -50000;//文件释放失败
+        /// -10000;//没有新版本
+        /// -20000;//文件下载失败
+        /// -30000;//文件释放失败
         /// </returns>
         /// </returns>
-        public int Update(string name, Version version, string url, string path, Dictionary<string, string> dictionary,
-            ProgressDelegate.ProgressHandler downprogress = null, object downsender = null,
-            ProgressDelegate.ProgressHandler releaseprogress = null, object releasesender = null)
+        public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
+            ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
+            ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
         {
         {
             Stopwatch stopwatch = new Stopwatch();
             Stopwatch stopwatch = new Stopwatch();
             stopwatch.Start();
             stopwatch.Start();
             //请求最新版本信息
             //请求最新版本信息
-            AppUpdateInfo info = HttpTool.Get<AppUpdateInfo>(string.Format("{0}?name={1}", url, name));
             if (info != null)
             if (info != null)
             {
             {
-                Version newVersion = GerVersion(info.Version);
-                if (newVersion == null) return -30000;//新版本号格式不正确,解析失败
-
-                if (newVersion > version)
+                string file = DirTool.Combine(tempPath, info.Name + info.Version);
+                //准备更新(下载) 
+                string downfile = Download(file, info, downProgress, downSender);
+                if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
                 {
                 {
-                    string file = DirTool.Combine(path, name + newVersion.ToString());
-                    //准备更新(下载) 
-                    string downfile = Download(file, info, downprogress, downsender);
-                    if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
+                    //格式化释放文件目录(相对路径转换为绝对路径)
+                    string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
+                    //释放文件
+                    int unpackCode = 0;
+                    if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender)) > 0)
                     {
                     {
-                        //格式化释放文件目录
-                        string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
-                        //释放文件
-                        int unpackCode = 0;
-                        if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender)) > 0)
-                        {
-                            stopwatch.Stop();
-                            return (int)stopwatch.Elapsed.TotalSeconds;
-                        }
-                        else
-                        {
-                            return -50000 + unpackCode;//文件释放失败
-                        }
+                        stopwatch.Stop();
+                        return (int)stopwatch.Elapsed.TotalSeconds;
                     }
                     }
                     else
                     else
                     {
                     {
-                        return -40000;//文件下载失败
+                        return -30000 + unpackCode;//文件释放失败
                     }
                     }
                 }
                 }
                 else
                 else
                 {
                 {
-                    return -10000;//无最新版本,停止操作
+                    return -20000;//文件下载失败
                 }
                 }
             }
             }
             else
             else
             {
             {
-                return -20000;//请求服务器最新版本失败
+                return -10000;//没有新版本
             }
             }
         }
         }
-        private Version GerVersion(string s)
+        private Version FormatVersion(string s)
         {
         {
             //解析最新版本号
             //解析最新版本号
             Version version = null;
             Version version = null;