Using C # to realize automatic decompression of RAR files

Source: Internet
Author: User
Tags file upload rar

If we are using a managed server it is usually necessary to implement file upload download (instead of FTP function for remote file management) has improved the efficiency of file processing the following program code enables automatic decompression of files

The

///<summary>
///uncompress the specified RAR file.
///</summary>
///<param name= "rarfiletodecompress" >rar file (absolute path). </param>
///<param name= "Directorytosave" > Unzip the saved directory. </param>
///<param name= "Deleterarfile" to delete the RAR file after decompression. </param>
public void Decompressrar (string rarfiletodecompress, String directorytosave, bool deleterarfile)
{
String winrarexe = Server.MapPath (@). /enterprisecustom/winrar.exe ")//need to place the Winara.exe executable file under the specified path to locate the file
if (new FileInfo (Winrarexe) under the installation directory.   Exists)
{
Directorytosave = Checkdirectoryname (directorytosave);
  Try
{
Process P = new process ();
  The program name that needs to be started
p.StartInfo.FileName = Winrarexe;
  Parameter
String arguments = @ "X-inul-y-o+";
Arguments + = "" + rarfiletodecompress + "" + directorytosave;
P.startinfo.arguments = Arguments;   
P.start ();/Start
while (!p.hasexited)
{
}
p.WaitForExit ();
}
catch (Exception ee)
{
throw new Exception ("An error occurred during decompression of the uploaded compressed file!") <BR> Please contact the administrator to check if you have write permission to the corresponding directory! ");   
}
if (deleterarfile)
{
File.delete (rarfiletodecompress);

}
Else
{
throw new Exception ("The required Winrar.exe file is missing on the system server, please contact administrator!");
}
}

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.