C # Batch File compression

Source: Internet
Author: User

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

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.