C # compress or unzip (RAR and zip files)

Source: Internet
Author: User
Tags rar

        /// <summary>        ///unzip the RAR and zip files (there is a need for Winrar.exe (as long as you can extract or compress files on your computer Winrar.exe))/// </summary>        /// <param name= "Unpath" >Save directory after extracting files</param>        /// <param name= "Rarpathname" >The file to be extracted is stored in absolute path (including file name)</param>        /// <param name= "Iscover" >whether the extracted files will overwrite the existing files (if not overwritten, the extracted files and the same name files that already exist will not exist together, only the original existing files are retained)</param>        /// <param name= "PassWord" >unzip the password (empty if no password is required)</param>        /// <returns>true (decompression succeeded); False (decompression failed)</returns>         Public Static BOOLUnrarorzip (stringUnpath,stringRarpathname,BOOLIscover,stringPassWord) {            if(!directory.exists (Unpath))            Directory.CreateDirectory (Unpath); Process Process1=NewProcess (); Process1.StartInfo.FileName="Winrar.exe"; Process1.StartInfo.CreateNoWindow=true; stringcmd =""; if(!string. IsNullOrEmpty (PassWord) &&iscover)//unzip the encrypted file and overwrite the existing file (-p password)cmd =string. Format ("x-p{0}-o+ {1} {2}-y", PassWord, Rarpathname, Unpath); Else if(!string. IsNullOrEmpty (PassWord) &&!iscover)//extract encrypted files without overwriting existing files (-p password)cmd =string. Format ("x-p{0}-o-{1} {2}-y", PassWord, Rarpathname, Unpath); Else if(iscover)//Overwrite command (x-o+ means overwrite existing file)cmd =string. Format ("x-o+ {0} {1}-y", Rarpathname,unpath); Else                //do not overwrite the command (x-o-means that no existing file is overwritten)cmd =string. Format ("x-o-{0} {1}-y", Rarpathname, Unpath); //CommandProcess1.StartInfo.Arguments =cmd;            Process1.start (); Process1.waitforexit ();//waits indefinitely for process Winrar.exe to exit//process1.exitcode==0 refers to normal execution, process1.exitcode==1 refers to abnormal execution            if(Process1.exitcode = =0) {process1.close (); return true; }            Else{process1.close (); return false; }        }        /// <summary>        ///Zip files into RAR or zip files (there is a need for Winrar.exe (as long as you can extract or compress files on your computer Winrar.exe))/// </summary>        /// <param name= "Filespath" >The absolute path to the folder or file that will be compressed</param>        /// <param name= "Rarpathname" >Compressed compressed file to save absolute path (including file name)</param>        /// <param name= "Iscover" >whether the compressed file will overwrite the existing compressed file (if not overwritten, the compressed file and the existing compressed file of the same name will not exist together, only the original existing compressed file)</param>        /// <param name= "PassWord" >compress the password (empty if no password is required)</param>        /// <returns>true (compression succeeded); False (compression failed)</returns>         Public Static BOOLCondenserarorzip (stringFilespath,stringRarpathname,BOOLIscover,stringPassWord) {            stringRarpath =Path.getdirectoryname (rarpathname); if(!directory.exists (Rarpath))            Directory.CreateDirectory (Rarpath); Process Process1=NewProcess (); Process1.StartInfo.FileName="Winrar.exe"; Process1.StartInfo.CreateNoWindow=true; stringcmd =""; if(!string. IsNullOrEmpty (PassWord) &&iscover)//Compress encrypted files and overwrite existing compressed files (-p password-o+ overwrite)cmd =string. Format ("a-ep1-p{0}-o+ {1} {2}-R", PassWord, Rarpathname, Filespath); Else if(!string. IsNullOrEmpty (PassWord) &&!iscover)//compress encrypted files without overwriting existing compressed files (-p password-o-not overwritten)cmd =string. Format ("a-ep1-p{0}-o-{1} {2}-R", PassWord, Rarpathname, Filespath); Else if(string. IsNullOrEmpty (PassWord) &&iscover)//Compress and overwrite existing compressed files (-o+ overwrite)cmd =string. Format ("a-ep1-o+ {0} {1}-R", Rarpathname, Filespath); Else                //compressed and does not overwrite existing compressed files (-o-not overwritten)cmd =string. Format ("a-ep1-o-{0} {1}-R", Rarpathname, Filespath); //CommandProcess1.StartInfo.Arguments =cmd;            Process1.start (); Process1.waitforexit ();//waits indefinitely for process Winrar.exe to exit//process1.exitcode==0 refers to normal execution, process1.exitcode==1 refers to abnormal execution            if(Process1.exitcode = =0) {process1.close (); return true; }            Else{process1.close (); return false; }        }

C # compress or unzip (RAR and zip files)

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.