C # RAR compression and decompression call

Source: Internet
Author: User

Public void rarsave (string rarpatch, string rarfiles, string patch, string rarname)
{
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
{
The_reg = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
The_rar = the_rar.substring (1, the_rar.length-7 );
If (! Directory. exists (patch ))
Directory. createdirectory (patch );
// Command Parameters

// The_info = "A c: \ test.rar a.txt-R"; // File compression

the_info = string. format ("A-DF {0 }\{ 1} {2}-R", patch, rarname, rarfiles ); // "A" + rarname + "" + patch;
the_startinfo = new processstartinfo ();
the_startinfo.filename = the_rar;
the_startinfo.arguments = the_info;
the_startinfo.windowstyle = processwindowstyle. hidden;
// directory where the packaged files are stored

The_startinfo.workingdirectory = rarpatch;
The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
}
Catch (exception ex)
{
Throw ex;
}
}

Public String unrar (string rarpath, string rarname, string unrarpath)
{
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
{
The_reg = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
The_rar = the_rar.substring (1, the_rar.length-7 );
If (! Directory. exists (unrarpath + "\" + rarname. substring (0, rarname. lastindexof (".")-1 )))
Directory. createdirectory (unrarpath + "\" + rarname. substring (0, rarname. lastindexof (".")-1 ));
The_info = string. format ("X {0} {1 }\\{ 2}-y", rarname, unrarpath, rarname. substring (0, rarname. lastindexof (". ")-1 ));
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_startinfo.workingdirectory = rarpath; // obtain the path of the compressed package

The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
}
Catch (exception ex)
{
Throw ex;
}
Return ""; // server. mappath (unrarpatch );
}
Public String unrar (string rarpath, string rarname, string unrarpath, string unrarpathname)
{
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
{
The_reg = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
The_rar = the_rar.substring (1, the_rar.length-7 );
// If (! Directory. exists (unrarpath + "\" + unrarpathname ))
// Directory. createdirectory (unrarpath + "\" + unrarpathname );
// The_info = string. Format ("X {0} {1} \\{ 2}-y", rarname, unrarpath, unrarpathname );

If (! Directory. exists (unrarpath ))
Directory. createdirectory (unrarpath );
The_info = string. Format ("X {0} {1}-y", rarname, unrarpath );
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_startinfo.workingdirectory = rarpath; // obtain the path of the compressed package

The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
}
Catch (exception ex)
{
Throw ex;
}
Return ""; // server. mappath (unrarpatch );
}

Rarsave ("C: \ emrbs", "1.txt 2.txt"," C: \ test "," test.rar ");

Unrar ("d :\\", "f.rar", "d :\\ ftest ");

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.