webclient 开启gzip下载

 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值