Today, I changed the function of a website. The website provides some materials. Each page corresponds to a set of materials. If a Member downloads the materials one by one, the website interaction is a little poor. The modified content is to provide a button and click the button to package and download the image and website information.
Ideas:
First, generate website information according to the format, traverse the directory to find all the materials, package these files, and use response to output.
The implementation of file packaging is to use the external open source library dotnetzip
Code implementation:
Create a blank Asp.net project, create a page, and reference ionic. Zip. dll under the dotnetzip Library
Reference the ionic. Zip namespace on the page
using Ionic.Zip;
Code for batch compression:
Add in page_load
If (! Page. ispostback) {response. clear (); response. bufferoutput = false; string [] files = directory. getfiles (server. mappath ("img/"); // The website file generates a readme.txt file string readmetext = string. format ("readme. TXT "+ environment. newline + "http://shandongit.com"); response. contenttype = "application/zip"; response. addheader ("content-disposition", "inline; filename = \" "+ String. format ("archive-00000).zip", datetime. now. tostring ("yyyy-mmm-dd-hhmmss") + "\"); // batch compression operation using (zipfile zip = new zipfile ()) {// The readme.txt file will not be password-protected. zip. addentry ("readme.txt", readmetext, encoding. default); zip. password = "shandongit.com"; zip. encryption = encryptionalgorithm. winzipaes256; // filestoinclude is a string [] or list <string> zip. addfiles (files, "Files"); zip. save (response. outputstream);} response. close ();}
C # Batch File compression