使用的類庫(kù)為:ICSharpCode.SharpZipLib.dll
一種是打包整個(gè)文件夾,另一種是打包指定的多個(gè)文件,大同小異:
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 // 打包下載某文件夾里的所有文件 8 9 //需要打包的文件夾 10 string path = "UploadImage/Images/"; 11 string serverPath = Server.MapPath(path); 12 13 //創(chuàng)建臨時(shí)文件夾 14 string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 15 string tempFolder = Path.Combine(serverPath, tempName); 16 Directory.CreateDirectory(tempFolder); 17 18 //遍歷文件夾中所有的文件到臨時(shí)文件夾中 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 //壓縮文件 27 compressFiles(tempFolder, tempFolder + "////" + tempName + ".rar"); 28 //下載文件 29 // DownloadRAR(tempFolder + "http:////" + tempName + ".rar", "這里下載文件重命名后的名稱"); 30 31 32 //某些文件打包下載 33 DownLodeSum(); 34 35 } 36 37 /// <summary> 38 /// 某些文件打包下載 39 /// </summary> 40 public void DownLodeSum() 41 { 42 //臨時(shí)文件夾所在目錄 43 string path = "UploadImage/"; 44 string serverPath = Server.MapPath(path); 45 46 //創(chuàng)建臨時(shí)文件夾 47 string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 48 string tempFolder = Path.Combine(serverPath, tempName); 49 Directory.CreateDirectory(tempFolder); 50 51 //復(fù)制需要壓縮的文件到臨時(shí)文件夾中 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(path + tempName + "/icon-cart.png")); 55 56 //壓縮文件 57 compressFiles(tempFolder, tempFolder + "////" + tempName + ".rar"); 58 //下載文件 59 DownloadRAR(tempFolder + "////" + tempName + ".rar", "這里下載文件重命名后的名稱"); 60 61 } 62 63 /// <summary> 64 /// 壓縮文件 65 /// </summary> 66 /// <param name="dir">文件目錄</param> 67 /// <param name="zipfilename">zip文件名</param> 68 public static void compressFiles(string dir, string 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 /// 下載生成的RAR文件111 /// </summary>112 private 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 //刪除臨時(shí)目錄下的所有文件127 DeleteFiles(tempPath);128 //刪除空目錄129 Directory.Delete(tempPath);130 Response.End();131 }132 133 /// <summary>134 /// 刪除臨時(shí)目錄下的所有文件135 /// </summary>136 /// <param name="tempPath">臨時(shí)目錄路徑</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 }
點(diǎn)擊下載源碼
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注