Asp.net uses the zip method to batch export txt files and asp. netzip to export txt files

Source: Internet
Author: User
Tags crc32

Asp.net uses the zip method to batch export txt files and asp. netzip to export txt files

First:

Reference ICSharpCode. SharpZipLib. dll, Baidu download

Then reference the namespace:

Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. Checksums;

My own code:

// Fetch data dt
 
                      string path = Server.MapPath("~\\tempzt\\");
If (dt. rows. count> 0) {System. text. stringBuilder sb = new System. text. stringBuilder (); string wjmc = "xxxxxxxxxxxx ". txt "; for (int n = 0; n <dt. rows. count; n ++) {for (int m = 0; m <dt. columns. count; m ++) {sb. append (dt. rows [n] [m]. toString (); sb. append ("\ t");} sb. append ("\ r \ n") ;}txt_export (sb, wjmc, path) ;}// zip download string [] files = Directory. getFiles (path); if (files. length> 0) {stri Ng name = "xxxxxxxxxxxx.zip"; ZipFileMain (files, path + name, 9); DownLoadZip (path + name, name);} else {Response. write ("<script> alert ('no target file! Write ') </script> ");}
Public void txt_export (StringBuilder sb, string wjmc, string path) {// zip download string txtPath = path + wjmc; if (! Directory. exists (path) {Directory. createDirectory (path);} if (File. exists (txtPath) {File. delete (txtPath);} StreamWriter sw = new StreamWriter (txtPath, false, System. text. encoding. default); sw. write (sb. toString (); sw. close (); // zip download} // zip download // <summary> // compressed file // </summary> // <param name = "fileName"> to be compressed all files (full path) </param> /// <param name = "name"> compressed file path </param> /// <param name = "Level"> Compression Level </param> public void ZipFileMain (string [] filenames, string name, int Level) {if (File. exists (name) {File. delete (name);} ZipOutputStream s = new ZipOutputStream (File. create (name); Crc32 crc = new Crc32 (); // compression level s. setLevel (Level); // 0-store only to 9-means best compression try {foreach (string file in filenames) {if (! Path. getExtension (file ). equals (". txt ") {continue;} // open the compressed File FileStream fs = File. openRead (file); // file address byte [] buffer = new byte [fs. length]; fs. read (buffer, 0, buffer. length); // create a compressed object ZipEntry entry = new ZipEntry (Path. getFileName (file); // original file name // time entry. dateTime = DateTime. now; // space size entry. size = fs. length; fs. close (); crc. reset (); crc. update (buffer); entry. crc = crc. value; s. putNextEntry (entry); s. write (buffer, 0, buffer. length); File. delete (file) ;}} catch {throw;} finally {s. finish (); s. close () ;}} public void DownLoadZip (string fileName, string name) {Response. contentType = "application/x-zip-compressed"; Response. addHeader ("Content-Disposition", "attachment; filename =" + name + ""); Response. transmitFile (fileName );}

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.