123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /* ==============================================================================
- * 功能描述:Untility
- * 创 建 者:Garrett
- * 创建日期:2019/1/29 11:19:14
- * ==============================================================================*/
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using Aliyun.OSS;
- using Aliyun.OSS.Common;
- using Aliyun.OSS.Util;
- using ICSharpCode.SharpZipLib.Zip;
- using Newtonsoft.Json.Linq;
- using SAGA.DotNetUtils.FileOperate;
- using SAGA.DotNetUtils.Http;
- using Update.Core.Entities;
- using PackageUploader.Compress;
- using PackageUploader.Http;
- using SAGA.DotNetUtils.Others;
- using Update;
- using HttpUtils = Update.HttpUtils;
- namespace PackageUploader
- {
- /// <summary>
- /// Untility
- /// </summary>
- public class Untility
- {
- /// <summary>
- /// 压缩文件夹
- /// </summary>
- /// <param name="destZipName"></param>
- /// <param name="dirs"></param>
- public static void CompressDir(string destZipName, string[] dirs, Action<string> compressChangeAction)
- {
- //using (ZipFile zip = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(destZipName))
- //{
- // zip.BeginUpdate();
- // long totalLen = 0;
- // foreach (string dir in dirs)
- // {
- // totalLen += GetDirectoryLength(dir);
- // }
- // CompressArgs compressArgs = new CompressArgs(0, 0, totalLen, compressChangeAction);
- // foreach (var dir in dirs)
- // {
- // FileInfo fileInfo = new FileInfo(dir);
- // string basePath = fileInfo.Directory?.FullName ?? "";
- // //ZipEntry e = new ZipEntry(Path.GetFileName(file));
- // ZipAddFile(zip, dir, basePath, compressArgs);
- // //break;
- // //zip.Add(file, Path.GetFileName(file));
- // }
- // compressChangeAction?.Invoke("Compressing ...");
- // zip.CommitUpdate();
- //}
- long totalLen = 0;
- foreach (string dir in dirs)
- {
- totalLen += GetDirectoryLength(dir);
- }
- CompressArgs compressArgs = new CompressArgs(0, 0, totalLen, compressChangeAction);
- using (ZipOutputStream s = new ZipOutputStream(File.Create(destZipName)))
- {
- s.SetLevel(6);
- foreach (var dir in dirs)
- {
- FileInfo fileInfo = new FileInfo(dir);
- string basePath = fileInfo.Directory?.FullName ?? "";
- CompressT.Compress(basePath, dir, s, compressArgs);
- }
- s.Finish();
- s.Close();
- }
- }
- /// <summary>
- /// 使用递归压缩文件夹和文件
- /// </summary>
- /// <param name="zip"></param>
- /// <param name="path"></param>
- /// <param name="basePath"></param>
- private static void ZipAddFile(ZipFile zip, string path, string basePath, CompressArgs args)
- {
- string fileName = path.Replace(basePath, "");
- DirectoryInfo dirInfo = new DirectoryInfo(path);
- if (dirInfo.Exists)
- {
- zip.AddDirectory(fileName);
- foreach (FileSystemInfo info in dirInfo.GetFileSystemInfos())
- {
- ZipAddFile(zip, info.FullName, basePath, args);
- }
- }
- else
- {
- args.IncrementTransferred = (new FileInfo(path)).Length;
- zip.Add(path, fileName);
- }
- }
- /// <summary>
- /// 获取指定路径的大小
- /// </summary>
- /// <param name="dirPath">路径</param>
- /// <returns></returns>
- public static long GetDirectoryLength(string dirPath)
- {
- long len = 0;
- //判断该路径是否存在(是否为文件夹)
- if (!Directory.Exists(dirPath))
- {
- //查询文件的大小
- len = FileSize(dirPath);
- }
- else
- {
- //定义一个DirectoryInfo对象
- DirectoryInfo di = new DirectoryInfo(dirPath);
- //通过GetFiles方法,获取di目录中的所有文件的大小
- foreach (FileInfo fi in di.GetFiles())
- {
- len += fi.Length;
- }
- //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
- DirectoryInfo[] dis = di.GetDirectories();
- if (dis.Length > 0)
- {
- for (int i = 0; i < dis.Length; i++)
- {
- len += GetDirectoryLength(dis[i].FullName);
- }
- }
- }
- return len;
- }
- //所给路径中所对应的文件大小
- public static long FileSize(string filePath)
- {
- //定义一个FileInfo对象,是指与filePath所指向的文件相关联,以获取其大小
- FileInfo fileInfo = new FileInfo(filePath);
- return fileInfo.Length;
- }
- /// <summary>
- /// 获取可执行文件的版本值
- /// </summary>
- /// <param name="exePath"></param>
- /// <returns></returns>
- public static Version GetFileVersion(string exePath)
- {
- Version version = null;
- try
- {
- version = Assembly.ReflectionOnlyLoadFrom(exePath).GetName().Version;
- }
- catch (Exception e)
- {
- version = new Version("0,0,0,0");
- }
- return version;
- //return new Version(FileVersionInfo.GetVersionInfo(exePath).FileVersion);
- }
- private static Action<string> m_UploadAction;
- /// <summary>
- /// 上传压缩包
- /// </summary>
- /// <param name="url"></param>
- /// <param name="compressPath"></param>
- public static bool UploadCompress(string compressPath, Action<string> action)
- {
- bool result = true;
- string fileName = Path.GetFileName(compressPath);
- m_UploadAction = action;
- var url = HttpUtils.GetUploadUrl(fileName);
- try
- {
- using (var fs = new MemoryStream(FileStreamOperate.ReadFile(compressPath)))
- {
- PutFlieContent fileContent = new PutFlieContent(url, fs);
- #region 进度条管理
- fileContent.TransferProgress += (sender, args) =>
- {
- try
- {
- var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
- Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
- args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData);
- UploadProgressCallback(sender, args);
- }
- catch (Exception ex)
- {
- throw;
- }
- };
- #endregion
- var resultStr = fileContent.PutContent();
- JObject rj = JObject.Parse(resultStr);
- if (rj["Result"].ToString() == "success")
- {
- return true;
- }
- throw new Exception(rj["ResultMsg"].ToString());
- }
- }
- catch (Exception ex)
- {
- MessageShowBase.Infomation("Revit文件上传载失败!\r\n" + ex.Message + "\r\n" + ex.StackTrace);
- result = false;
- }
- return result;
- }
- /// <summary>
- /// 下载进度条
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private static void UploadProgressCallback(object sender, StreamProgressArgs args)
- {
- var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
- m_UploadAction?.Invoke(string.Format("UploadCallback - TotalBytes:{0}M, TransferredBytes:{1}M,UploadPrecent:{2}%",
- args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData));
- }
- /// <summary>
- /// 保存md5值
- /// </summary>
- /// <param name="url"></param>
- /// <param name="md5"></param>
- public static string SaveVision(string key, string value)
- {
- string url = HttpUtils.GetUploadUrl(key);
- RestClient client = new RestClient(url, HttpVerb.POST, value);
- string request = client.PostRequest();
- return request;
- }
- /// <summary>
- /// 删除旧压缩包
- /// </summary>
- public static void DeleteCompress()
- {
- string compressName = $"{ReadVision()}_{Const.Key}.zip";
- string url =HttpUtils.DeleteUrl();
- JArray jArray = new JArray();
- jArray.Add(compressName);
- JObject jObject = new JObject
- {
- { "keys", jArray }
- };
- RestClient client = new RestClient(url, HttpVerb.POST, jObject.ToString());
- string request = client.PostRequest();
- }
- /// <summary>
- /// 读取版本信息
- /// </summary>
- /// <param name="url"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- public static Version ReadVision()
- {
- string url = HttpUtils.GetDownloadUrl(Update.Const.Key);
- Packages packages = null;
- try
- {
- RestClient client = new RestClient(url, HttpVerb.GET);
- string request = client.PostRequest();
- packages = new Packages(request);
- }
- catch (Exception e)
- {
- packages = new Packages("0,0,0,0");
- }
- return packages?.FullPackages.Max()?.To;
- }
- /// <summary>
- /// CanUpload
- /// </summary>
- /// <param name="exePath"></param>
- /// <returns></returns>
- public static bool CheckVision(string exePath, out string version)
- {
- Version serviceVersion = ReadVision();
- Version localVersion = GetFileVersion(exePath);
- version = $"ServiceVersion:{serviceVersion};LocalVersion:{localVersion}";
- return (serviceVersion == null || localVersion > serviceVersion);
- }
- }
- }
|