ASP. NET package download file, asp.net package download

Source: Internet
Author: User

ASP. NET package download file, asp.net package download

The class library used is ICSharpCode. SharpZipLib. dll.

 

One is to package the entire folder, and the other is to Package Multiple specified files, similar to the following:

 

1 using ICSharpCode. sharpZipLib. zip; 2 3 public partial class _ Default: System. web. UI. page 4 {5 protected void Page_Load (object sender, EventArgs e) 6 {7 // package and download all files in a folder 8 9 // 10 string path = "UploadImage/Images/"; 11 string serverPath = Server. mapPath (path); 12 13 // create a Temporary Folder 14 string tempName = DateTime. now. toString ("yyyyMMddHHMMss"); 15 string tempFolder = Path. combine (serverPath, TempName); 16 Directory. createDirectory (tempFolder); 17 18 // traverse all the files in the folder to the Temporary folder 19 DirectoryInfo folder = new DirectoryInfo (serverPath); 20 foreach (FileInfo file in folder. getFiles () 21 {22 string filename = file. name; 23 File. copy (serverPath + "/" + filename, tempFolder + "/" + filename); 24} 25 26 // compressed file 27 compressFiles (tempFolder, tempFolder + "\\\\" + tempName + ". rar "); 28 // download file 29 // D OwnloadRAR (tempFolder + "\\\\" + tempName + ". rar "," Name of the downloaded file after renaming "); 30 31 32 // download 33 DownLodeSum () for some files (); 34 35} 36 37 // <summary> 38 // some files are packed and downloaded 39 /// </summary> 40 public void DownLodeSum () 41 {42 // directory where the Temporary Folder is located 43 string path = "UploadImage/"; 44 string serverPath = Server. mapPath (path); 45 46 // create a Temporary Folder 47 string tempName = DateTime. now. toString ("yyyyMMddHHMMss"); 48 string tempFolder = Pa Th. combine (serverPath, tempName); 49 Directory. createDirectory (tempFolder); 50 51 // copy the File to be compressed to the Temporary Folder 52 File. copy (Server. mapPath ("UploadImage/Images/bg-b.png"), Server. mapPath (path + tempName + "/bg-b.png"); 53 File. copy (Server. mapPath ("UploadImage/Images/bg-j.png"), Server. mapPath (path + tempName + "/bg-j.png"); 54 File. copy (Server. mapPath ("UploadImage/Images/icon-cart.png"), Server. mapPath (pat H + tempName + "/icon-cart.png"); 55 56 // compressed file 57 compressFiles (tempFolder, tempFolder + "\\\\" + tempName + ". rar "); 58 // download file 59 DownloadRAR (tempFolder +" \\\\ "+ tempName + ". rar "," Name of the downloaded file after renaming "); 60 61} 62 63 // <summary> 64 // compressed file 65 // </summary> 66 // <param name = "dir"> file directory </param> 67 // <param name = "zipfilename"> zip file name </param> 68 public static void compressFiles (string dir, strin G zipfilename) 69 {70 if (! Directory. exists (dir) 71 {72 return; 73} 74 try 75 {76 string [] filenames = Directory. getFiles (dir); 77 using (ZipOutputStream s = new ZipOutputStream (File. create (zipfilename) 78 {79 80 s. setLevel (9); // 0-store only to 9-means best compression 81 82 byte [] buffer = new byte [4096]; 83 84 foreach (string file in filenames) 85 {86 ZipEntry entry = new ZipEntry (Path. getFileName (file); 87 Entry. dateTime = DateTime. now; 88 s. putNextEntry (entry); 89 using (FileStream fs = File. openRead (file) 90 {91 int sourceBytes; 92 do 93 {94 sourceBytes = fs. read (buffer, 0, buffer. length); 95 s. write (buffer, 0, sourceBytes); 96} while (sourceBytes> 0); 97} 98} 99 s. finish (); 100 s. close (); 101} 102} 103 catch104 {105 106} 107 108 109 // <summary> 110 // download the generated rarfile 111 // </summary> 112 priva Te void DownloadRAR (string file, string name) 113 {114 FileInfo fileInfo = new FileInfo (file); 115 Response. clear (); 116 Response. clearContent (); 117 Response. clearHeaders (); 118 Response. addHeader ("Content-Disposition", "attachment; filename =" + name + ". rar "); 119 Response. addHeader ("Content-Length", fileInfo. length. toString (); 120 Response. addHeader ("Content-Transfer-Encoding", "binary"); 121 Response. ContentType = "application/octet-stream"; 122 Response. contentEncoding = System. text. encoding. getEncoding ("gb2312"); 123 Response. writeFile (fileInfo. fullName); 124 Response. flush (); 125 string tempPath = file. substring (0, file. lastIndexOf ("\\\\\"); 126 // delete all files in the temporary Directory 127 DeleteFiles (tempPath); 128 // Delete the empty Directory 129 Directory. delete (tempPath); 130 Response. end (); 131} 132 133 // <summary> 134 // delete all files in the temporary directory 135/ /// </Summary> 136 // <param name = "tempPath"> temporary directory path </param> 137 private void DeleteFiles (string tempPath) 138 {139 DirectoryInfo directory = new DirectoryInfo (tempPath); 140 try141 {142 foreach (FileInfo file in directory. getFiles () 143 {144 if (file. attributes. toString (). indexOf ("ReadOnly ")! =-1) 145 {146 file. attributes = FileAttributes. normal; 147} 148 File. delete (file. fullName); 149} 150} 151 catch (Exception) 152 {153 throw; 154} 155} 156}

Click to download source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.