Browse Source

优化文件打包和加密的过程回调

yuzhengyang 8 years ago
parent
commit
5a5bfbd25b

+ 25 - 1
Fork.Net/Oreo.Plugins/Oreo.FileMan/Views/MainForm.Designer.cs

@@ -35,6 +35,8 @@
             this.BtFileDecrypt = new System.Windows.Forms.Button();
             this.button1 = new System.Windows.Forms.Button();
             this.progressBar1 = new System.Windows.Forms.ProgressBar();
+            this.label1 = new System.Windows.Forms.Label();
+            this.button5 = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // BtFileEncrypt
@@ -59,7 +61,7 @@
             // 
             // button3
             // 
-            this.button3.Location = new System.Drawing.Point(51, 148);
+            this.button3.Location = new System.Drawing.Point(158, 201);
             this.button3.Name = "button3";
             this.button3.Size = new System.Drawing.Size(75, 23);
             this.button3.TabIndex = 2;
@@ -102,11 +104,31 @@
             this.progressBar1.Size = new System.Drawing.Size(281, 23);
             this.progressBar1.TabIndex = 6;
             // 
+            // label1
+            // 
+            this.label1.Location = new System.Drawing.Point(140, 269);
+            this.label1.Name = "label1";
+            this.label1.Size = new System.Drawing.Size(153, 12);
+            this.label1.TabIndex = 7;
+            this.label1.Text = "label1";
+            // 
+            // button5
+            // 
+            this.button5.Location = new System.Drawing.Point(51, 152);
+            this.button5.Name = "button5";
+            this.button5.Size = new System.Drawing.Size(75, 23);
+            this.button5.TabIndex = 8;
+            this.button5.Text = "button5";
+            this.button5.UseVisualStyleBackColor = true;
+            this.button5.Click += new System.EventHandler(this.button5_Click);
+            // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(305, 319);
+            this.Controls.Add(this.button5);
+            this.Controls.Add(this.label1);
             this.Controls.Add(this.progressBar1);
             this.Controls.Add(this.button1);
             this.Controls.Add(this.BtFileDecrypt);
@@ -130,5 +152,7 @@
         private System.Windows.Forms.Button BtFileDecrypt;
         private System.Windows.Forms.Button button1;
         private System.Windows.Forms.ProgressBar progressBar1;
+        private System.Windows.Forms.Label label1;
+        private System.Windows.Forms.Button button5;
     }
 }

+ 12 - 2
Fork.Net/Oreo.Plugins/Oreo.FileMan/Views/MainForm.cs

@@ -14,6 +14,7 @@ using System.Xml.Linq;
 using Y.Utils.DataUtils.EncryptUtils;
 using Y.Utils.IOUtils.FileUtils;
 using Y.Utils.IOUtils.PathUtils;
+using Y.Utils.NetUtils.HttpUtils;
 
 namespace Oreo.FileMan.Views
 {
@@ -98,7 +99,7 @@ namespace Oreo.FileMan.Views
         {
             Task.Factory.StartNew(() =>
             {
-                int flag = FilePackageTool.Pack(@"D:\Temp\测试压缩\Root", @"D:\Temp\测试压缩\Root.pkg", UIProgress);
+                int flag = FilePackageTool.Pack(@"D:\temp\测试打包\新建文件夹", @"D:\temp\测试打包\新建文件夹.pkg", UIProgress);
                 if (flag > 0)
                     MessageBox.Show("打包成功");
             });
@@ -108,7 +109,7 @@ namespace Oreo.FileMan.Views
         {
             Task.Factory.StartNew(() =>
             {
-                int flag = FilePackageTool.Unpack(@"D:\Temp\测试压缩\Root.pkg", @"D:\Temp\测试压缩\Root", UIProgress);
+                int flag = FilePackageTool.Unpack(@"D:\temp\测试打包\新建文件夹.pkg", @"D:\temp\测试打包\新建文件夹", UIProgress);
                 if (flag > 0)
                     MessageBox.Show("拆包成功");
             });
@@ -119,6 +120,7 @@ namespace Oreo.FileMan.Views
             {
                 progressBar1.Maximum = 100;
                 progressBar1.Value = (int)(current * 100 / total);
+                label1.Text = current + "/" + total;
             }));
         }
 
@@ -185,5 +187,13 @@ namespace Oreo.FileMan.Views
             catch (Exception e) { }
             return null;
         }
+
+        private void button5_Click(object sender, EventArgs e)
+        {
+            Task.Factory.StartNew(() =>
+            {
+                HttpTool.Download("http://sw.bos.baidu.com/sw-search-sp/software/5b71d074792c3/googleearth_7.1.8.3036.exe", @"D:\temp\测试加密\googleearth_7.1.8.3036.exe", UIProgress);
+            });
+        }
     }
 }

+ 10 - 13
Fork.Net/Y.Utils/IOUtils/FileUtils/FileEncryptTool.cs

@@ -30,6 +30,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// <param name="srcFile">源文件</param>
         /// <param name="dstFile">目标文件</param>
         /// <param name="password">加密密码</param>
+        /// <param name="progress">回调进度</param>
         /// <param name="overwrite">是否覆盖已有目标文件</param>
         /// <returns>
         /// >0:操作成功(操作共计秒数)
@@ -37,7 +38,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -12:加密后的目标文件已存在
         /// -404:未知错误,操作失败
         /// </returns>
-        public static int Encrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
+        public static int Encrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress = null, bool overwrite = true)
         {
             DateTime beginTime = DateTime.Now;
             if (!File.Exists(srcFile)) return -11; //要加密的文件不存在
@@ -47,7 +48,6 @@ namespace Y.Utils.IOUtils.FileUtils
             string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
 
             string md5 = FileTool.GetMD5(srcFile);
-            long current = 0;
             using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
             {
                 using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
@@ -99,8 +99,7 @@ namespace Y.Utils.IOUtils.FileUtils
                                 byte[] enbyte = AesTool.Encrypt(buffer, fmtPwd);
                                 fsWrite.Write(enbyte, 0, enbyte.Length);
                             }
-                            current += readCount;
-                            progress?.Invoke(current, fsRead.Length);
+                            progress?.Invoke(fsRead.Position, fsRead.Length);
                         }
                         return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
                     }
@@ -117,6 +116,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// <param name="srcFile">源文件</param>
         /// <param name="dstFile">目标文件</param>
         /// <param name="password">解密密码</param>
+        /// <param name="progress">回调进度</param>
         /// <param name="overwrite">是否覆盖已有目标文件</param>
         /// <returns>
         /// >0:操作成功(操作共计秒数)
@@ -127,7 +127,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -90:解锁密码错误
         /// -404:未知错误,操作失败
         /// </returns>
-        public static int Decrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
+        public static int Decrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress = null, bool overwrite = true)
         {
             DateTime beginTime = DateTime.Now;
             if (!File.Exists(srcFile)) return -11;//要解密的文件不存在
@@ -137,7 +137,6 @@ namespace Y.Utils.IOUtils.FileUtils
             string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
             List<string> headdata = new List<string>();
 
-            long current = 0;
             using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
             {
                 using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
@@ -146,27 +145,26 @@ namespace Y.Utils.IOUtils.FileUtils
                     {
                         //读取文件类型标识和版本号
                         byte[] filetype = Encoding.Default.GetBytes(FileType);
-                        current += fsRead.Read(filetype, 0, filetype.Length);
+                        fsRead.Read(filetype, 0, filetype.Length);
                         string filetypestr = Encoding.Default.GetString(filetype);
                         byte[] fileversion = Encoding.Default.GetBytes(FileVersion);
-                        current += fsRead.Read(fileversion, 0, fileversion.Length);
+                        fsRead.Read(fileversion, 0, fileversion.Length);
                         string fileversionstr = Encoding.Default.GetString(fileversion);
                         if (filetypestr != FileType && fileversionstr != FileVersion) return -20;//文件类型不匹配
 
                         //读取头部信息个数
                         byte[] count = new byte[4];
-                        current += fsRead.Read(count, 0, count.Length);
+                        fsRead.Read(count, 0, count.Length);
                         int countint = BitConverter.ToInt32(count, 0);
                         //读取各部分长度和数据
                         byte[] headlength = new byte[4 * countint];
                         if (fsRead.Read(headlength, 0, headlength.Length) == headlength.Length)
                         {
-                            current += headlength.Length;
                             for (int i = 0; i < countint; i++)
                             {
                                 int datalong = BitConverter.ToInt32(headlength, i * 4);
                                 byte[] tempdata = new byte[datalong];
-                                current += fsRead.Read(tempdata, 0, datalong);
+                                fsRead.Read(tempdata, 0, datalong);
                                 headdata.Add(Encoding.Default.GetString(tempdata));
                             }
                         }
@@ -189,8 +187,7 @@ namespace Y.Utils.IOUtils.FileUtils
                                 byte[] debyte = AesTool.Decrypt(buffer, fmtPwd);
                                 fsWrite.Write(debyte, 0, debyte.Length);
                             }
-                            current += readCount;
-                            progress?.Invoke(current, fsRead.Length);
+                            progress?.Invoke(fsRead.Position, fsRead.Length);
                         }
                     }
                     catch (Exception e) { }

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

@@ -42,7 +42,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -13;//要打包的路径中没有文件
         /// -404;//未知错误,操作失败
         /// </returns>
-        public static int Pack(string srcPath, string dstFile, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
+        public static int Pack(string srcPath, string dstFile, ProgressDelegate.ProgressHandler progress = null, bool overwrite = true)
         {
             DateTime beginTime = DateTime.Now;
             if (!Directory.Exists(srcPath)) return -11;//要打包的路径不存在
@@ -122,7 +122,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -20;// 文件类型不匹配
         /// -404;//未知错误,操作失败
         /// </returns>
-        public static int Unpack(string srcFile, string dstPath, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
+        public static int Unpack(string srcFile, string dstPath, ProgressDelegate.ProgressHandler progress = null, bool overwrite = true)
         {
             DateTime beginTime = DateTime.Now;
             if (!File.Exists(srcFile)) return -11; //要解包的文件不存在

+ 7 - 7
Fork.Net/Y.Utils/NetUtils/HttpUtils/HttpTool.cs

@@ -9,6 +9,7 @@ using System.Net;
 using System.Text;
 using System.Web;
 using Y.Utils.DataUtils.StringUtils;
+using Y.Utils.DelegateUtils;
 
 namespace Y.Utils.NetUtils.HttpUtils
 {
@@ -203,7 +204,7 @@ namespace Y.Utils.NetUtils.HttpUtils
         /// <param name="url">下载文件地址</param>
         /// <param name="file">文件存放地址,包含文件名</param>
         /// <returns></returns>
-        public static bool Download(string url, string file)
+        public static bool Download(string url, string file, ProgressDelegate.ProgressHandler progress = null)
         {
             string tempPath = Path.GetDirectoryName(file) + @"\temp";
             Directory.CreateDirectory(tempPath);  //创建临时文件目录
@@ -223,13 +224,12 @@ namespace Y.Utils.NetUtils.HttpUtils
                 Stream responseStream = response.GetResponseStream();
                 //创建本地文件写入流
                 //Stream stream = new FileStream(tempFile, FileMode.Create);
-                byte[] bArr = new byte[1024];
-                int size = responseStream.Read(bArr, 0, (int)bArr.Length);
-                while (size > 0)
+                byte[] buffer = new byte[100 * 1024];
+                int readCount = 0;
+                while ((readCount = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                 {
-                    //stream.Write(bArr, 0, size);
-                    fs.Write(bArr, 0, size);
-                    size = responseStream.Read(bArr, 0, (int)bArr.Length);
+                    fs.Write(buffer, 0, readCount);
+                    //progress?.Invoke(responseStream.Position, responseStream.Length);
                 }
                 //stream.Close();
                 fs.Close();