Compress files using plugins and provide downloads

Source: Internet
Author: User

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

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.