asp.net using RAR to realize file compression and decompression

Source: Internet
Author: User
Tags rar

If a RAR program is installed on the server, then asp.net can call RAR to implement file compression and decompression.

However, note that because the Web program can not directly invoke the client's program (unless the use of activex,activex almost discarded), so if you want to achieve the user to the local file with a Web page uncompressed only to upload files to the server and then call the server on the RAR compression, In the same way to extract the local RAR file can upload files to the server decompression and then get back.

This article is about how to extract files on the server side of the directory!

Foreground code:



<% ... @ Page Language="C #" autoeventwireup=" true' CodeFile='Default.aspx.cs' Inherits="_default " %>
<! DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd " >
< HTML xmlns = "http://www.w3.org/1999/xhtml"   >
< Head runat = "Server" >
< title > server-side decompression qingqing Moon http://blog.111cn.net/21aspnet/ </ title >
</ Head >
< Body >
< form ID = "Form1" runat = "Server" >
< Div >
< Asp:button ID = "Button1" runat = "Server" OnClick = "Button1_Click" Text = "Compression"   />
< Asp:button ID = "Button2" runat = "Server" OnClick = "Button2_Click" Text = "Decompress"   /></ Div >
</ form >
</ Body >
</ HTML >Background code:


using System;
using System.Data;
using System.Configuration;
using system.web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;
Public   Partial   class _default:system.web.ui.page
... {
protected voidPage_Load (Objectsender, EventArgs e)
...{
// qingqing Moon http://blog.111cn.net/21aspnet/
    }
protected voidButton1_Click (Objectsender, EventArgs e)
...{
//Compress
String The_rar;
RegistryKey The_reg;
Object The_obj;
String The_info;
ProcessStartInfo The_startinfo;
Process the_process;
Try
...{
The_reg=Registry.ClassesRoot.OpenSubKey ("Applicationswinrar.exeshellopencommand");
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" + "1.rar" + "  " + "C:.txt";
The_startinfo= NewProcessStartInfo ();
The_startinfo.filename=The_rar;
The_startinfo.arguments=The_info;
The_startinfo.windowstyle=Processwindowstyle.hidden;
The_startinfo.workingdirectory= "C:";//Gets or sets the initial directory of the process to start.
the_process= NewProcess ();
The_process.startinfo=The_startinfo;
The_process.start ();
Response.Write ("Compact Success");
}
Catch(Exception ex)
...{
Response.Write (ex. ToString ());
}
}
protected voidbutton2_click (Objectsender, EventArgs e)
...{
//Decompression
String The_rar;
RegistryKey The_reg;
Object The_obj;
String The_info;
ProcessStartInfo The_startinfo;
Process the_process;
Try
...{
The_reg=Registry.ClassesRoot.OpenSubKey ("Applicationswinrar.exeshellopencommand");
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" + "1.rar" + " " + "C:";
The_startinfo= NewProcessStartInfo ();
The_startinfo.filename=The_rar;
The_startinfo.arguments=The_info;
The_startinfo.windowstyle=Processwindowstyle.hidden;
The_process= NewProcess ();
The_process.startinfo=The_startinfo;
The_process.start ();
Response.Write ("Decompression succeeded");
}
Catch(Exception ex)
...{
Response.Write (ex. ToString ());
}
}
}Server-side directory:

Workaround for Client decompression:
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.