[Post] ASP. NET [C #] online compression and decompression

Source: Internet
Author: User

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 ");

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.