Пример #1
0
        protected static void CheckAndDownload(String file, String targetPath = null)
        {
            if (!Path.IsPathRooted(file))
            {
                if (!Runtime.IsWeb)
                    file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
                else
                    file = Path.Combine(HttpRuntime.BinDirectory, file);
            }

            if (File.Exists(file) && new FileInfo(file).Length > 0) return;

            // 目标目录
            String dir = !String.IsNullOrEmpty(targetPath) ? targetPath : Path.GetDirectoryName(file);

            // 从网上下载文件
            var zipfile = Path.GetFileNameWithoutExtension(file);

            try
            {
                #region 检测64位平台
                Module module = typeof(Object).Module;

                PortableExecutableKinds kind;
                ImageFileMachine machine;
                module.GetPEKind(out kind, out machine);

                if (machine != ImageFileMachine.I386) zipfile += "64";
                #endregion

                zipfile += ".zip";
                String url = String.Format(ServiceAddress, zipfile);

                // 目标Zip文件
                zipfile = Path.Combine(dir, zipfile);
                // Zip文件不存在,准备下载
                if (!File.Exists(zipfile) || new FileInfo(zipfile).Length <= 0)
                {
                    DAL.WriteLog("准备从{0}下载文件到{1}!", url, zipfile);
                    var client = new WebClientX(true, true);
                    // 同步下载,3秒超时
                    client.Timeout = 3000;
                    //var data = client.DownloadData(url);
                    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                    client.DownloadFile(url, zipfile);
                    client.Dispose();
                }

                DAL.WriteLog("下载完成,准备解压到{0}!", dir);
                //var ms = new MemoryStream(data);
                //if (file.EndsWith("64")) file = file.Substring(0, file.Length - 2);
                //IOHelper.DecompressFile(ms, dir, file, false);
                ZipFile.Extract(zipfile, dir, true);
                DAL.WriteLog("解压完成!");
            }
            catch (ZipException ex)
            {
                if (File.Exists(zipfile)) File.Delete(zipfile);
                DAL.WriteLog("解压失败,删除压缩文件!{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                DAL.WriteLog(ex.ToString());
            }
        }
Пример #2
0
        protected static void CheckAndDownload(String file, String targetPath = null)
        {
            if (!Path.IsPathRooted(file))
            {
                if (!Runtime.IsWeb)
                    file = file.GetFullPath();
                else
                    file = Path.Combine(HttpRuntime.BinDirectory, file);
            }

            if (File.Exists(file) && new FileInfo(file).Length > 0) return;

            // 目标目录
            String dir = !String.IsNullOrEmpty(targetPath) ? targetPath : Path.GetDirectoryName(file);

            // 从网上下载文件
            var zipfile = Path.GetFileNameWithoutExtension(file);

            try
            {
                #region 检测64位平台
                var module = typeof(Object).Module;

                PortableExecutableKinds kind;
                ImageFileMachine machine;
                module.GetPEKind(out kind, out machine);

                if (machine != ImageFileMachine.I386) zipfile += "64";
                #endregion

                zipfile += ".zip";
                var url = String.Format(ServiceAddress, zipfile);

                var sw = new Stopwatch();
                sw.Start();

                // 检查缓存文件,一个月内有效
                var x = Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), @"X");
                var xfile = Path.Combine(x, zipfile);
                var xf = new FileInfo(xfile);
                if (CacheZip && File.Exists(xfile) && xf.Length > 0 && xf.LastWriteTime.AddMonths(1) > DateTime.Now)
                    zipfile = xfile;
                else
                {
                    // 目标Zip文件
                    zipfile = Path.Combine(dir, zipfile);
                    // Zip文件不存在,准备下载
                    if (!File.Exists(zipfile) || new FileInfo(zipfile).Length <= 0)
                    {
                        DAL.WriteLog("准备从{0}下载文件到{1}!", url, zipfile);
                        var client = new WebClientX(true, true);
                        // 同步下载,3秒超时
                        client.Timeout = 10000;
                        //var data = client.DownloadData(url);
                        if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                        client.DownloadFile(url, zipfile);
                        client.Dispose();
                    }

                    if (CacheZip) File.Copy(zipfile, xfile, true);
                }
                sw.Stop();
                var size = new FileInfo(zipfile).Length;
                DAL.WriteLog("下载{0}完成({3:n0}字节),耗时{2},准备解压到{1}!", zipfile, dir, sw.Elapsed, size);
                //var ms = new MemoryStream(data);
                //if (file.EndsWith("64")) file = file.Substring(0, file.Length - 2);
                //IOHelper.DecompressFile(ms, dir, file, false);
                ZipFile.Extract(zipfile, dir, true);
                DAL.WriteLog("解压完成!");
            }
            catch (ZipException ex)
            {
                if (File.Exists(zipfile)) File.Delete(zipfile);
                DAL.WriteLog("解压失败,删除压缩文件!{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                DAL.WriteLog(ex.ToString());
            }
        }