1 //traverse all files under the specified folder of the server2 stringPath ="uploads/image/";3 stringServerpath =Server.MapPath (path);4 5 //Create a temporary folder6 stringTempname = DateTime.Now.ToString ("YYYYMMDDHHMMSS");7 stringTempFolder =Path.Combine (Serverpath, tempname);8 directory.createdirectory (tempfolder);9 TenDirectoryInfo folder =NewDirectoryInfo (serverpath); One foreach(FileInfo fileinchfolder. GetFiles ()) A { - stringfilename =file. Name; -File.Copy (Serverpath +"/"+ filename, TempFolder +"/"+filename); the } - //ZKHelper.JSHelper.Alert ("Picture copy succeeded!"); - //generate RAR files, and file output - Rarsave (TempFolder, tempname); +Downloadrar (TempFolder +"\\"+ Tempname +". rar");
1 /// <summary>2 ///Generate RAR file3 /// </summary>4 /// <param name= "path" >directory where the copied files are stored</param>5 /// <param name= "Rarpatch" >rar file storage directory</param>6 /// <param name= "Rarname" >rar file name</param>7 Private voidRarsave (stringRarpatch,stringrarname)8 {9 stringThe_rar;Ten RegistryKey The_reg; One Object the_obj; A stringThe_info; - ProcessStartInfo The_startinfo; - Process the_process; the Try - { -The_reg = Registry.ClassesRoot.OpenSubKey (@"WinRAR"); -The_obj = The_reg.getvalue (""); +The_rar =the_obj.tostring (); - the_reg.close (); +The_rar = The_rar. Substring (1, The_rar. Length-7); AThe_info ="a"+ Rarname +"- R"; atThe_startinfo =NewProcessStartInfo (); -The_startinfo.filename ="WinRar";//The_rar; -The_startinfo.arguments =The_info; -The_startinfo.windowstyle =Processwindowstyle.hidden; - //package file storage directory -The_startinfo.workingdirectory =Rarpatch; inThe_process =NewProcess (); -The_process.startinfo =The_startinfo; to The_process.start (); + the_process.waitforexit (); - the_process.close (); the } * Catch(Exception) $ {Panax Notoginseng Throw; - } the}
Download the generated RAR file
1 /// <summary>2 ///download the generated RAR file3 /// </summary>4 Private voidDownloadrar (stringfile)5 {6FileInfo FileInfo =NewFileInfo (file);7 response.clear ();8 response.clearcontent ();9 response.clearheaders ();TenResponse.AddHeader ("content-disposition","attachment;filename="+fileinfo.name); OneResponse.AddHeader ("Content-length", fileInfo.Length.ToString ()); AResponse.AddHeader ("content-transfer-encoding","binary"); -Response.ContentType ="Application/octet-stream"; -response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312"); the Response.WriteFile (fileinfo.fullname); - Response.Flush (); - stringTempPath = file. Substring (0, file. LastIndexOf ("\\")); - //Delete all files in temp directory + deletefiles (TempPath); - //Delete Empty directory + Directory.delete (TempPath); A Response.End (); at}
1 /// <summary>2 ///Delete all files in temp directory3 /// </summary>4 /// <param name= "TempPath" >Temp directory path</param>5 Private voidDeletefiles (stringTempPath)6 {7DirectoryInfo directory =NewDirectoryInfo (TempPath);8 Try9 {Ten foreach(FileInfo fileinchdirectory. GetFiles ()) One { A if(file. Attributes.tostring (). IndexOf ("ReadOnly") != -1) - { -File. Attributes =Fileattributes.normal; the } - File.delete (File. FullName); - } - } + Catch(Exception) - { + Throw; A } at}