Implementation method of C using WinRar command to compress and decompress

  • 2021-10-16 02:31:09
  • OfStack

In this paper, the implementation method of C # using WinRar command to compress and decompress is described. Share it for your reference, as follows:


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
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  // Zip file 
  protected void Button1_Click(object sender, EventArgs e)
  {
    ProcessStartInfo startinfo = new ProcessStartInfo(); ;
    Process process = new Process();
    string rarName = "1.rar"; // Compressed file name 
    string path = @"C:\images"; // Packaged folder to be compressed 
    string rarPath = @"C:\zip"; // Save folder after compression 
    string rarexe = @"c:\Program Files\WinRAR\WinRAR.exe"; //WinRAR Installation position 
    try
    {
      // Compress command, which is equivalent to the folder to be compressed (path) Right-click on ->WinRAR-> Add to zip file -> Enter the name of the compressed file (rarName)
      string cmd = string.Format("a {0} {1} -r", rarName, path);
      startinfo.FileName = rarexe;
      startinfo.Arguments = cmd;             // Setting command parameters 
      startinfo.WindowStyle = ProcessWindowStyle.Hidden; // Hide  WinRAR  Window 
      startinfo.WorkingDirectory = rarPath;
      process.StartInfo = startinfo;
      process.Start();
      process.WaitForExit(); // Waiting for the process indefinitely  winrar.exe  Quit 
      if (process.HasExited)
      {
        MSCL.JsHelper.Alert(" Compression succeeded! ", Page);
      }
    }
    catch (Exception ex)
    {
      MSCL.JsHelper.Alert(ex.Message, Page);
    }
    finally
    {
      process.Dispose();
      process.Close();
    }
  }
  // Extract a file 
  protected void Button2_Click(object sender, EventArgs e)
  {
    ProcessStartInfo startinfo = new ProcessStartInfo(); ;
    Process process = new Process();
    string rarName = "1.rar"; // To be decompressed  .rar  File name (including suffix) 
    string path = @"C:\images1"; // File Extraction Path (Absolute) 
    string rarPath = @"C:\zip"; // To be decompressed  .rar  Directory where files are stored (absolute path) 
    string rarexe = @"c:\Program Files\WinRAR\WinRAR.exe"; //WinRAR Installation position 
    try
    {
      // Extract command, which is equivalent to compressing files (rarName) Right-click on ->WinRAR-> Unzip to current folder 
      string cmd = string.Format("x {0} {1} -y", rarName, path);
      startinfo.FileName = rarexe;
      startinfo.Arguments = cmd;             // Setting command parameters 
      startinfo.WindowStyle = ProcessWindowStyle.Hidden; // Hide  WinRAR  Window 
      startinfo.WorkingDirectory = rarPath;
      process.StartInfo = startinfo;
      process.Start();
      process.WaitForExit(); // Waiting for the process indefinitely  winrar.exe  Quit 
      if (process.HasExited)
      {
        MSCL.JsHelper.Alert(" Decompression succeeded! ", Page);
      }
    }
    catch (Exception ex)
    {
      MSCL.JsHelper.Alert(ex.Message, Page);
    }
    finally
    {
      process.Dispose();
      process.Close();
    }
  }
}

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: