Source: Unknown ..
1. Reference
Using system. IO;
Using Microsoft. Win32;
Using system. diagnostics;
/*************************************** ***************
Program purpose: implement the function of compressing and extracting files from [folders ].
Program remarks:
* Server WinRAR support
* The path description must be an absolute path.
**************************************** **************/
2. Compression of main code
/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "dfilepath"> the folder or file to be compressed </param>
/// <Param name = "drarname"> Generate the compressed file name </param>
/// <Param name = "drarpath"> Generate the path for saving the compressed file </param>
/// <Returns> </returns>
Protected bool RAR (string dfilepath, string drarname, string drarpath)
{
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 );
The_info = "A" + "" + drarname + "" + dfilepath; // command + compressed file name + compressed file or path
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_startinfo.workingdirectory = drarpath; // directory where the rarfile is stored.
The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
Return true;
}
Catch (exception ex)
{
Return false;
}
}
3. decompress the main code
/// <Summary>
/// Decompress the package to the specified folder
/// </Summary>
/// <Param name = "rarfilepath"> directory of the compressed file </param>
/// <Param name = "rarfilename"> compressed file name </param>
/// <Param name = "unrarfilepath"> decompress the package to a folder </param>
/// <Returns> </returns>
Protected bool unrar (string rarfilepath, string rarfilename, string unrarfilepath)
{
// Extract
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 );
The_info = @ "X" + "" + rarfilepath + rarfilename + "" + unrarfilepath;
The_startinfo = new processstartinfo ();
The_startinfo.filename = the_rar;
The_startinfo.arguments = the_info;
The_startinfo.windowstyle = processwindowstyle. hidden;
The_process = new process ();
The_process.startinfo = the_startinfo;
The_process.start ();
Return true;
}
Catch (exception ex)
{
Return false;
}
}
3. Example
String ofilepath = @ "C: \ 1 \";
String orarfilename = "1.rar ";
String otofilepath = @ "C: \ 2 \";
// If (RAR (ofilepath, orarfilename, otofilepath ))
If (unrar (ofilepath, orarfilename, otofilepath ))
Response. Write ("OK ");
Else
Response. Write ("no ");