webclient 开启gzip下载

来源:互联网 发布:上古卷轴5战斗优化mod 编辑:程序博客网 时间:2024/06/13 22:34
 public static class Download    {        /// <summary>        /// 下载文件保留字        /// </summary>        public static string PersistExp = ".tefi";              /// <summary>        ///  压缩下载        /// </summary>        /// <param name="savePath">保存路径</param>        /// <param name="loadPath">下载路径</param>        /// <param name="fileName">文件名称</param>        /// <param name="flag">是否备份文件,默认备份名,原文件+.back</param>        /// <returns></returns>        public static void DownloadGip(string savePath, string loadPath, string fileName = null, bool flag = true)        {            // 自定义文件名为空的情况            if (string.IsNullOrEmpty(fileName))            {                // 取得下载文件名                fileName = Path.GetFileName(loadPath);            }            savePath = savePath + fileName;            // 判断保存路径是否存在,备份原文件            JustFileAndDic(savePath, true);            FileStream fs;            MemoryStream ms;            using (WebClient client = new WebClient())            {                try                {                    client.Headers.Add("Accept-Encoding", "gzip,deflate");                    byte[] byteArray = client.DownloadData(loadPath);                    // 处理 gzip                     string sContentEncoding = client.ResponseHeaders["Content-Encoding"];                    if (sContentEncoding == "gzip")                    {                        ms = new MemoryStream(byteArray);                        fs = new FileStream(savePath, FileMode.Create);                        int count = 0;                        // 解压                        GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);                        byte[] buf = new byte[512];                        while ((count = gzip.Read(buf, 0, buf.Length)) > 0)                        {                            fs.Write(buf, 0, count);                        }                        fs.Close();                        ms.Close();                    }                }                // ReSharper disable once EmptyGeneralCatchClause                catch                {                }            }        }        #region 方法        #region 创建文件夹        /// <summary>        ///  文件夹不存在,创建文件夹        /// </summary>        /// <param name="path">文件夹路径</param>        public static void JustFileAndDic(string path)        {            try            {                string folderPath = path.Substring(0, path.LastIndexOf("\\", StringComparison.Ordinal));                if (!Directory.Exists(folderPath))                {                    Directory.CreateDirectory(folderPath);                }            }            // ReSharper disable once EmptyGeneralCatchClause            catch            {            }        }        /// <summary>        ///  文件夹不存在,创建文件夹        /// </summary>        /// <param name="path">文件夹路径</param>        /// <param name="flag">备份文件</param>        public static void JustFileAndDic(string path, bool flag)        {            try            {                string folderPath = path.Substring(0, path.LastIndexOf("\\", StringComparison.Ordinal));                if (!Directory.Exists(folderPath))                {                    Directory.CreateDirectory(folderPath);                }                if (flag)                {                    BackUpFile(path);                }            }            // ReSharper disable once EmptyGeneralCatchClause            catch            {            }        }        #endregion        #region 备份文件        /// <summary>        ///   备份删除文件(.back)        /// </summary>        /// <param name="savePath">保存路径</param>        public static void BackUpFile(string savePath)        {            if (File.Exists(savePath))            {                // 覆盖文件                File.Copy(savePath, $"{savePath}.back", true);                File.Delete(savePath);            }        }        #endregion        #endregion    }

0 0
原创粉丝点击