C # compression or decompression (rar and zip files ),

Source: Internet
Author: User

C # compression or decompression (rar and zip files ),

/// <Summary> /// decompress the RARs and zips (the winrar.exe(only the files on the computer can be decompressed or compressed are winrar.exe )) /// </summary> /// <param name = "UnPath"> decompress the package and save the file to the directory </param> /// <param name = "rarPathName"> to be decompressed absolute file storage path (including file name) </param> /// <param name = "IsCover"> whether the extracted file overwrites the existing file (if not overwritten, the extracted file and the existing file with the same name do not exist together, only the existing file is retained) </param> /// <param name = "PassWord"> extract the PassWord (empty if no PassWord is required) </param> /// <returns> true (decompression successful); false (decompression failed) </returns> public static boo L UnRarOrZip (string UnPath, string rarPathName, bool IsCover, string PassWord) {if (! Directory. exists (UnPath) Directory. createDirectory (UnPath); Process Process1 = new Process (); Process1.StartInfo. fileName = "Winrar.exe"; Process1.StartInfo. createNoWindow = true; string cmd = ""; if (! String. isNullOrEmpty (PassWord) & IsCover) // decompress 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) // decompress the encrypted file and do not overwrite the existing file (-p password) cmd = string. format ("x-p {0}-o-{1} {2}-y", PassWord, rarPathName, UnPath); else if (IsCover) // overwrite command (x-o + indicates overwriting existing files) cmd = string. format ("x-o + {0} {1}-y", rarPathName, UnPath ); else // do not overwrite command (x-o-Indicates do not overwrite existing files) cmd = string. format ("x-o-{0} {1}-y", rarPathName, UnPath); // command Process1.StartInfo. arguments = cmd; Process1.Start (); Process1.WaitForExit ();// Waiting for winrar.exe to exit indefinitely // Process1.ExitCode = 0 indicates normal execution. Process1.ExitCode = 1 indicates that if (Process1.ExitCode = 0) {Process1.Close (); return true ;} else {Process1.Close (); return false ;}/// <summary> // The compressed file must exist in the raror zipfile (winrar.exe(you only need to uncompress the file on your computer or winrar.exe )) /// </summary> /// <param name = "filesPath"> absolute path of the folder or file to be compressed </param> /// <param name = "rarPathName "> absolute path (including file name) of compressed files) </param> /// <par Am name = "IsCover"> whether the compressed file overwrites the existing compressed file (if it is not overwritten, the compressed file and the compressed file with the same name do not exist together, only existing compressed files are retained.) </param> // <param name = "PassWord"> compress the PassWord (null if no PassWord is required) </param> // <returns> true (compression successful); false (compression failed) </returns> public static bool CondenseRarOrZip (string filesPath, string rarPathName, bool IsCover, string PassWord) {string rarPath = Path. getDirectoryName (rarPathName); if (! Directory. exists (rarPath) Directory. createDirectory (rarPath); Process Process1 = new Process (); Process1.StartInfo. fileName = "Winrar.exe"; Process1.StartInfo. createNoWindow = true; string cmd = ""; if (! String. isNullOrEmpty (PassWord) & IsCover) // compress the encrypted file and overwrite the existing compressed file (-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 the encrypted file and do not overwrite the existing compressed file (-p password-o-Not Overwrite) 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 // compress and does not overwrite existing compressed files (-o-Not Overwrite) cmd = string. format ("a-ep1-o-{0} {1}-r", rarPathName, filesPath); // command Process1.StartInfo. arguments = cmd; Process1.Start (); Process1.WaitForExit (); // wait for winrar.exe to exit indefinitely // Process1.ExitCode = 0 indicates that the process is normal, process1.ExitCode = 1 indicates that if (Process1.ExitCode = 0) {Process1.Close (); return true ;}else {Process1.Close (); return false ;}} is not executed normally ;}}

 

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.