C # uses the WinRAR command to compress and decompress

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using system.diagnostics;using System.io;public partial class zip:system.web.ui.page{prot ected void Page_Load (object sender, EventArgs e) {}//compressed file protected void Button1_Click (object sender, Eventa        RGS e) {ProcessStartInfo startinfo = new ProcessStartInfo ();        Process process = new process (); String rarname = "1.rar"; Compressed file name string path = @ "C:\images";  Package directory to be compressed string Rarpath = @ "C:\zip";  Compressed after storing directory string rarexe = @ "C:\Program Files\winrar\winrar.exe";            WinRAR Install location try {//compress command, equivalent to right-click on the directory to be Compressed (path)->winrar-> join to the compressed file, enter the compressed file name (rarname) string cmd = string.            Format ("a {0} {1}-R", Rarname, Path); StartInfo.            FileName = Rarexe; StartInfo.             Arguments = cmd;             Sets the number of command parameters StartInfo.  WindowStyle = Processwindowstyle.hidden; Hides the WinRAR form StartInfo.            WorkingDirectory = Rarpath; Process.            StartInfo = StartInfo; Process.            Start (); Process. WaitForExit (); Waits indefinitely for the process to Winrar.exe exit if (process. hasexited) {MscL. Jshelper.alert ("Compression succeeded!            ", page); }} catch (Exception ex) {MSCL. Jshelper.alert (ex.        Message, Page); } finally {process.            Dispose (); Process.        Close (); }}//extract file protected void button2_click (object sender, EventArgs e) {ProcessStartInfo Startinf        o = new ProcessStartInfo ();        Process process = new process (); String rarname = "1.rar"; The. rar file name (containing the suffix) that will be decompressed string path = @ "C:\images1";  File decompression Path (absolute) string rarpath = @ "C:\zip"; The storage directory (absolute path) of the. rar file that will be decompressed string rarexe = @ "C:\Program Files\winrar\winrar. exe "; WinRAR Install location try {//unzip command, equivalent to right-click on the file to be compressed (rarname)->winrar-> extract to the current directory string cmd = String.            Format ("x {0} {1}-y", rarname, Path); StartInfo.            FileName = Rarexe; StartInfo.                          Arguments = cmd; Sets the number of command parameters StartInfo.  WindowStyle = Processwindowstyle.hidden; Hides the WinRAR form StartInfo.            WorkingDirectory = Rarpath; Process.            StartInfo = StartInfo; Process.            Start (); Process. WaitForExit (); Waits indefinitely for the process to Winrar.exe exit if (process. hasexited) {MscL. Jshelper.alert ("Unzip successfully!")            ", page); }} catch (Exception ex) {MSCL. Jshelper.alert (ex.        Message, Page); } finally {process.            Dispose (); Process.        Close (); }       }}

C # uses the WinRAR command to compress and decompress

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.