当前位置 : 主页 > 编程语言 > 其它开发 >

C# 文件打包下载

来源:互联网 收集:自由互联 发布时间:2022-06-14
今天偶然在实践中使用到了多文件需要打包下载的功能,这么说吧,可能内心还有一些美滋滋,又要到了每日的动脑动手环节了,直接石更(ying)核开干...... ##首先 不得不提到一个需
  今天偶然在实践中使用到了多文件需要打包下载的功能,这么说吧,可能内心还有一些美滋滋,又要到了每日的动脑动手环节了,直接石更(ying)核开干......   ## 首先 不得不提到一个需要使用到的类库,那就是`SharpZipLib`,可能猛然一看还是有点陌生的,但是在项目中打开NuGet包管理器窗口,一点即搜,分分钟下载到位;
简单介绍一下`SharpZipLib`的功能:其实主要用来解压缩**Zip、GZip、BZip2、Tar** 等格式,是以托管程序集的方式实现,可以非常方便的应用于项目之中。   好了好了 不要聊了不要聊了  先上Code 先上Code..   你懂我的意思,需要先引用一下类库:
1 using ICSharpCode.SharpZipLib.Zip;
  接下来将虚拟路径映射到本地/服务器上的物理路径,如果文件已存在就删除,不存在就创建
 1 #region 删除目录中现有文件及子目录
 2 string filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Files/Temp/{0}/Test".Ft(DateTime.Now.ToString("yyyyMMdd")));
 3 Directory.CreateDirectory(filePath);
 4 DirectoryInfo dir = new DirectoryInfo(filePath);
 5 FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();//返回目录中所有文件和子目录
 6 foreach (FileSystemInfo i in fileinfo)
 7 {
 8     if (i is DirectoryInfo)//判断是否文件夹
 9     {
10         DirectoryInfo subdir = new DirectoryInfo(i.FullName);
11         subdir.Delete(true);//删除子目录和文件
12     }
13     else
14     {
15         System.IO.File.Delete(i.FullName); //删除指定文件
16     }
17 }
18 #endregion

 

接下来流程就很简单了,就是首先获取我们需要下载的文件   --->  然后请求页面  --->  生成自己需要的文件路径及文件名称 ---> DownLoad到本地,如果有若干个文件可以考虑使用ForEach来进行遍历。
1 List<string> file = new List<string>();
2 var url = "https://baidu.com/Download/Files?access={0})".Ft(文件.pdf);
3 REST.GET(url);
4  var urlfile = filePath + "\\" + $"{定义成自己需要的文件名称}";
5 file.Add(urlfile);
6 HttpDownloadFile(url, urlfile);
7 
8 FilesZip(file,9,System.Web.Hosting.HostingEnvironment.MapPath("~/Files/Temp/{0}/{1}".Ft(DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip")));

这是一个提供的下载文件的方法 ↓↓↓
 1 public void HttpDownloadFile(string url, string path){
 2 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 3 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 4 Stream responseStream = response.GetResponseStream();
 5 Stream stream = new FileStream(path, FileMode.Create);
 6 byte[] bArr = new byte[1024];
 7 int size = responseStream.Read(bArr, 0, (int)bArr.Length);
 8 while (size > 0)
 9 {
10     stream.Write(bArr, 0, size);
11     size = responseStream.Read(bArr, 0, (int)bArr.Length);
12 }
13 stream.Close();
14 responseStream.Close();
15 }

 

这是主要负责打包压缩,包括可以设置压缩级别、Zip包加密、Zip包注释
 1 public void FilesZip(List<string> fileNames, int? compresssionLevel, string saveFullPath, string password = "", string comment = ""){
 2     using (ZipOutputStream zos = new ZipOutputStream(System.IO.File.Open(saveFullPath, FileMode.OpenOrCreate)))
 3     {
 4 if (compresssionLevel.HasValue)
 5 {
 6     zos.SetLevel(compresssionLevel.Value);//设置压缩级别
 7 }
 8 
 9 if (!string.IsNullOrEmpty(password))
10 {
11     zos.Password = password;//设置zip包加密密码
12 }
13 
14 if (!string.IsNullOrEmpty(comment))
15 {
16     zos.SetComment(comment);//设置zip包的注释
17 }
18 
19 foreach (string file in fileNames)
20 {
21     if (System.IO.File.Exists(file))
22     {
23 FileInfo item = new FileInfo(file);
24 FileStream fs = System.IO.File.OpenRead(item.FullName);
25 byte[] buffer = new byte[fs.Length];
26 fs.Read(buffer, 0, buffer.Length);
27 
28 ZipEntry entry = new ZipEntry(item.Name);
29 zos.PutNextEntry(entry);
30 zos.Write(buffer, 0, buffer.Length);
31 fs.Close();
32     }
33 }
34 zos.Close();
35     }
36 }

 

## 结语 在普通的星期五,结束这普通的2021。
网友评论