Direct Internet access to the method:
/// <summary> ///function: Compress files (temporarily compress only the files in the folder level directory, and the folders and their children are ignored)/// </summary> /// <param name= "Dirpath" >Folders folder path that is compressed</param> /// <param name= "Zipfilepath" >the path to the compressed file is generated, and the default is the same level directory as the compressed folder with the name: folder name +.zip</param> /// <param name= "Err" >error Message</param> /// <returns>whether compression succeeded</returns> Public Static BOOLZipFile (stringDirpath,stringZipfilepath, out stringerr) { //If the server has a file with the same name, delete if(File.exists (Zipfilepath)) {Try{file.delete (Zipfilepath); } Catch{}} Err=""; if(Dirpath = =string. Empty) {Err="the folder you want to compress cannot be empty! "; return false; } if(!directory.exists (Dirpath)) {Err="the folder you want to compress does not exist! "; return false; } //use folder name when compressed file name is empty +.zip if(Zipfilepath = =string. Empty) {if(Dirpath.endswith ("\\") ) {Dirpath= Dirpath.substring (0, Dirpath.length-1); } Zipfilepath= Dirpath +". zip"; } Try { string[] filenames =Directory.GetFiles (Dirpath); using(Zipoutputstream s =NewZipoutputstream (File.create (Zipfilepath))) {S.setlevel (9); byte[] buffer =New byte[4096]; foreach(stringFileinchfilenames) {ZipEntry entry=Newzipentry (path.getfilename (file)); Entry. DateTime=DateTime.Now; S.putnextentry (entry); using(FileStream fs =File.openread (File)) { intsourcebytes; Do{sourcebytes= fs. Read (Buffer,0, buffer. Length); S.write (Buffer,0, sourcebytes); } while(Sourcebytes >0); }} s.finish (); S.close (); } } Catch(Exception ex) {err=Ex. Message; return false; } return true; }
Download code directly on the package
stringTemp_path = HttpContext.Current.Request.MapPath (". \\query\\");String Path = Temp_path + DateTime.Now.ToString ("YYYYMMDDHHMMSS"); stringNewPath = path +"\\"; Directory.CreateDirectory (path); for(inti =0; I < DS. Tables.count; i++) {Datatocsv.savecsv (ds. Tables[i], NewPath); The String result=String.Empty; Boolean Flag= Datatocsv.zipfile (NewPath, Temp_path +"Mesdata.zip", outresult); if(flag) {//Delete a build folderDirectory.delete (Path,true); Available for download FileInfo FileInfo=NewFileInfo (Temp_path +"Mesdata.zip"); Response.Clear (); Response.clearcontent (); Response.ClearHeaders (); Response.AddHeader ("content-disposition","Attachment;filename=mesdata.zip"); Response.AddHeader ("Content-length", fileInfo.Length.ToString ()); Response.AddHeader ("content-transfer-encoding","binary"); Response.ContentType="Application/octet-stream"; Response.ContentEncoding= System.Text.Encoding.GetEncoding ("UTF-8"); Response.WriteFile (Fileinfo.fullname); Response.Flush (); HttpContext.Current.ApplicationInstance.CompleteRequest (); Deleting replicas//File.delete (filePath);response.close (); } Else{//prompt information }
Compress files using plugins and provide downloads