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 );}