標籤:
今天分享下昨天做的一個東西 asp.net 的檔案 zip 批量下載,首先你需要去 到http://dotnetzip.codeplex.com這個網站下載zip 的包,在裡面找到 Ionic.Zip.dll 引用到你的項目中去
/// <summary> /// 批量zip下載 /// </summary> /// <param name="Listimg">這裡Listimg 是一個數群組類型</param> public void CreateZip(string Listimg) { string[] imgs = Listimg.Split(‘,‘); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.BufferOutput = false; //網站檔案產生一個readme.txt的讀我檔案(可以不寫) String readmeText = String.Format("README.TXT" +Environment.NewLine+"網址址:http://www.aicoffees.com" ); HttpContext.Current.Response.ContentType = "application/zip";//以zip 形式輸出 HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=\"Photo.zip");//壓縮下載的名字 //批量壓縮操作 using (ZipFile zip = new ZipFile()) { for (int i = 0; i < imgs.Length; i++) {///在壓縮包內添加上面的讀我檔案,文字編碼是系統預設編碼形式 zip.AddEntry("Readme.txt", readmeText, Encoding.Default); zip.Password = "www.aicoffees.com";//給壓縮包設定密碼 zip.Encryption = EncryptionAlgorithm.WinZipAes256;//加密方式 zip.AddFile(HttpContext.Current.Server.MapPath(imgs[i].ToString()), "");//這裡"" 我給的是空就是壓縮時不設定檔案夾,如果需要取什麼名字只需要在“”裡面加上就可以了,這裡是一個重載方法,如果 zip.AddFile(HttpContext.Current.Server.MapPath(imgs[i].ToString()), "");這樣寫的話,zip 就會預設把你的image從根目錄一直壓縮到你的檔案所在目錄。 } zip.Save(HttpContext.Current.Response.OutputStream); } HttpContext.Current.Response.Close(); }
以上是自己的一點小總結,come on
asp.net 檔案壓縮zip下載