asp.net implementation of WinRAR compression and decompression in Windows platform

Source: Internet
Author: User
Tags rar zip

Let's look at WinRAR's own support invocation command:

Compression command: a {0} {1}-R ' {0}: Compressed filename | {1}: File physical path to compress

Ex: "A Your sister. rar f:\MM-r" (meaning to compress the folder in MM under F to "your sister. rar" file)

Decompression command: x {0} {1}-y ' {0}: File name to extract | {1}: File physical path to extract

Ex: "x sister. rar f:\ sister-y" (to be compressed file physical path: "F:\ sister, the younger sister. Rar")

Parameter description

Parameters

Meaning a

Add file to zip package X

Unzip the compression from the compressed package with the full path

C # calling a generic class

The code is as follows Copy Code

Using System;
Using System.Collections.Generic;
Using System.Text;

--------------using
Using System.Diagnostics;
Using Microsoft.Win32;
Using System.IO;

<summary>
Name:stone
Datetime:2011/12/31 16:39:26
Description:winrar compression
</summary>
public class Winrarcsharp
{
WinRAR Install registry key
Private Const string Winrar_key = @ "Winrar.zipshellopencommand";

<summary>
Using WinRAR to compress
</summary>
<param name= "path" > folder to be Compressed (absolute path) </param>
<param name= "Rarpath" > Storage directory (absolute path) of compressed. RAR </param>
<param name= "Rarname" > Compressed file name (including suffix) </param>
<returns>true or False. Compression successfully returns TRUE, otherwise, false. </returns>
public bool RAR (string path, String Rarpath, String rarname)
{
BOOL flag = FALSE;
String Rarexe; Full path to WinRAR.exe
RegistryKey RegKey; Registry keys
Object Regvalue; Key value
string cmd; WinRAR Command Parameters
ProcessStartInfo StartInfo;
Process process;
Try
{
RegKey = Registry.ClassesRoot.OpenSubKey (Winrar_key);
Regvalue = RegKey.  GetValue (""); The key value is "D:program FilesWinRARWinRAR.exe" "%1"
Rarexe = Regvalue. ToString ();
RegKey. Close ();
Rarexe = Rarexe. Substring (1, Rarexe.  LENGTH-7); D:program FilesWinRARWinRAR.exe

Directory.CreateDirectory (path);
Compression command, which is equivalent to right-clicking on the folder (path) to be compressed->winrar-> add to compressed file-> input compressed filename (rarname)
cmd = string. Format (' a {0} {1}-R ',
Rarname,
path);
StartInfo = new ProcessStartInfo ();
StartInfo. FileName = Rarexe;
StartInfo.                          Arguments = cmd; Set command parameters
StartInfo.  WindowStyle = Processwindowstyle.hidden; Hide WinRAR window

StartInfo. WorkingDirectory = Rarpath;
Process = new process ();
Process. StartInfo = StartInfo;
Process. Start ();
Process. WaitForExit (); Wait indefinitely for process Winrar.exe exit
if (process. hasexited)
{
Flag = true;
}
Process. Close ();
}
catch (Exception e)
{
Throw e;
}
return flag;
}
<summary>
Using WinRAR to decompress
</summary>
<param name= "path" > File decompression Path (absolute) </param>
<param name= "Rarpath" > Storage directory (absolute path) of the. rar file to be uncompressed </param>
<param name= "Rarname" > the. rar file name (including suffix) that will be uncompressed </param>
<returns>true or False. The decompression successfully returns TRUE, otherwise, false. </returns>
public bool Unrar (string path, String Rarpath, String rarname)
{
BOOL flag = FALSE;
String Rarexe;
RegistryKey RegKey;
Object Regvalue;
string cmd;
ProcessStartInfo StartInfo;
Process process;
Try
{
RegKey = Registry.ClassesRoot.OpenSubKey (Winrar_key);
Regvalue = RegKey. GetValue ("");
Rarexe = Regvalue. ToString ();
RegKey. Close ();
Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7);

Directory.CreateDirectory (path);
Extract the command, which is equivalent to the right key on the compressed file (rarname)->winrar-> extract to the current folder
cmd = string. Format (' x {0} {1}-y ',
Rarname,
path);
StartInfo = new ProcessStartInfo ();
StartInfo. FileName = Rarexe;
StartInfo. Arguments = cmd;
StartInfo. WindowStyle = Processwindowstyle.hidden;

StartInfo. WorkingDirectory = Rarpath;
Process = new process ();
Process. StartInfo = StartInfo;
Process. Start ();
Process. WaitForExit ();
if (process. hasexited)
{
Flag = true;
}
Process. Close ();
}
catch (Exception e)
{
Throw e;
}
return flag;
}
}

Call method

The code is as follows Copy Code

Winrarcsharp win = new Winrarcsharp ();

Win. RAR ("F:\aaa\", "f:\", "A.rar");//Compress (compress "f:\aaa\" directory file to "F:\a.rar")

Win. Unrar ("F:\ Yo M.zip", "F:\MM", "GG"); Decompression ("F:\ yo M.zip" to the "F:\MM\GG" directory)

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.