当前位置:网站首页>C file package and download

C file package and download

2022-06-13 10:39:00 Maocuntou juvenile*

 
Today, I accidentally used the function of multiple files to be packaged and downloaded in practice , Let me put it this way , There may be some happiness in my heart , It's time for the daily brain and hands-on session , Direct stone (ying) Nuclear drying ......
 
##  First
I have to mention a class library that needs to be used , That's it ` SharpZipLib`, It may be strange at a glance , But open in the project NuGet Package manager window , One click search , Download in minutes ;

A brief introduction ` SharpZipLib` The function of : In fact, it is mainly used to decompress ** Zip、GZip、BZip2、Tar** Equiform , It's implemented as a managed assembly , It can be easily applied to projects .
 
Good good Stop talking. Stop talking   First Code First Code..
 
You know what I mean , You need to reference the class library first :
1 using ICSharpCode.SharpZipLib.Zip;
 
Next, map the virtual path to the local / The physical path on the server , Delete the file if it already exists , Create... If it doesn't exist
 1 #region  Delete existing files and subdirectories in the directory 
 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();// Return all files and subdirectories in the directory 
 6 foreach (FileSystemInfo i in fileinfo)
 7 {
 8     if (i is DirectoryInfo)// Determine whether the folder 
 9     {
10         DirectoryInfo subdir = new DirectoryInfo(i.FullName);
11         subdir.Delete(true);// Delete subdirectories and files 
12     }
13     else
14     {
15         System.IO.File.Delete(i.FullName); // Delete the specified file 
16     }
17 }
18 #endregion

 

Then the process is very simple , First, get the files we need to download   --->   Then request the page  --->   Generate the required file path and file name ---> DownLoad To local , If there are several files, consider using ForEach Let's do the traversal .
1 List<string> file = new List<string>();
2 var url = "https://baidu.com/Download/Files?access={0})".Ft( file .pdf);
3 REST.GET(url);
4  var urlfile = filePath + "\\" + $"{ Define the file name you need }";
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")));

This is a provided method for downloading files ↓↓↓
 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 }

 

This is mainly responsible for packaging and compression , Including setting the compression level 、Zip Packet encryption 、Zip Package comments
 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);// Set compression level 
 7 }
 8 
 9 if (!string.IsNullOrEmpty(password))
10 {
11     zos.Password = password;// Set up zip Package encryption password 
12 }
13 
14 if (!string.IsNullOrEmpty(comment))
15 {
16     zos.SetComment(comment);// Set up zip Package comments 
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 }

 

## Conclusion
On an ordinary Friday , End this ordinary 2021.
原网站

版权声明
本文为[Maocuntou juvenile*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131035402198.html