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
- webclient 开启gzip下载
- C# WebClient gzip
- 开启gzip
- windows下apache开启gzip压缩 加快网页下载速度
- windows下apache开启gzip压缩 加快网页下载速度
- 使用WebClient实现下载
- WebClient下载文件代码!
- WebClient下载文件
- HTTP下载webClient
- C#下载文件:WebClient
- 使用WebClient下载
- WebClient下载图片示例
- WebClient异步下载文件
- WebClient类数据下载
- webclient下载文件
- c# webclient下载网页
- WebClient下载网站源码
- apache开启gzip
- C# wcf 注册window服务 报System.IO.FileNotFoundException: 未能加载文件或程序集“file:///C:\Windows\system32\***
- 【CEOI2004】锯木厂选址(斜率优化入门题)
- STL源码剖析---红黑树原理详解上
- Struts2 OGNL表达式的使用
- addSubview和insertSubview 区别
- webclient 开启gzip下载
- nyoj222 整数中的1 数位DP
- 电力系统安全区划分
- QProgressBar水平、垂直样式表两种设置方式
- C++11特性
- CentOS搭建dubbo-admin管理平台
- [LeetCode]169 Majority Element
- 关于使用curl下载网页源码中文乱码问题!
- Android studio适配6.0的一些小问题