ASP.NET利用RAR实现文件压缩解压缩

添加人:admin四级(1706分)   添加时间:2007-08-11    阅读次数:893  收藏此教程

如果服务器上安装了RAR程序,那么asp.net可以调用RAR实现文件压缩与解压缩。

不过要注意的是,由于Web程序不能直接调用客户端的程序(除非用ActiveX,ActiveX几乎被废弃),所以如果要想实现让用户把本地文件用网页解压缩只有把文件上传到服务器上再调用服务器上的RAR压缩,同理要解压缩本地的RAR文件可以把文件上传到服务器解压再拿回来。

本文讲怎么在服务器端的目录解压缩文件!

效果图:
 
前台代码:
 1<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3<html xmlns="http://www.w3.org/1999/xhtml" >
 4<head runat="server">
 5    <title>服务器端解压缩 清清月儿</title>
 6</head>
 7<body>
 8    <form id="form1" runat="server">
 9    <div>
10        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="压缩" />
11        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="解压缩" /></div>
12    </form>
13</body>
14</html>
 
后台代码:
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10using System.IO;
11using System.Runtime.InteropServices;
12using Microsoft.Win32;
13using System.Diagnostics;
14public partial class _Default : System.Web.UI.Page
15{
16    protected void Page_Load(object sender, EventArgs e)
17    {
18    }

19    protected void Button1_Click(object sender, EventArgs e)
20    {
21        //压缩
22        String the_rar;
23        RegistryKey the_Reg;
24        Object the_Obj;
25        String the_Info;
26        ProcessStartInfo the_StartInfo;
27        Process the_Process;
28        try
29        {
30            the_Reg = Registry.ClassesRoot.OpenSubKey("ApplicationsWinRAR.exeShellOpenCommand");
31            the_Obj = the_Reg.GetValue("");
32            the_rar = the_Obj.ToString();
33            the_Reg.Close();
34            the_rar = the_rar.Substring(1, the_rar.Length - 7);
35            the_Info = " a " + " 1.rar " + "  " + "C:.txt";
36            the_StartInfo = new ProcessStartInfo();
37            the_StartInfo.FileName = the_rar;
38            the_StartInfo.Arguments = the_Info;
39            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
40            the_StartInfo.WorkingDirectory = "C:";//获取或设置要启动的进程的初始目录。
41            the_Process = new Process();
42            the_Process.StartInfo = the_StartInfo;
43            the_Process.Start();
44            Response.Write("压缩成功");
45        }

46        catch (Exception ex)
47        {
48            Response.Write(ex.ToString());
49        }

50    }

51    protected void Button2_Click(object sender, EventArgs e)
52    {
53        //解压缩
54        String the_rar;
55        RegistryKey the_Reg;
56        Object the_Obj;
57        String the_Info;
58        ProcessStartInfo the_StartInfo;
59        Process the_Process;
60        try
61        {
62            the_Reg = Registry.ClassesRoot.OpenSubKey("ApplicationsWinRar.exeShellOpenCommand");
63            the_Obj = the_Reg.GetValue("");
64            the_rar = the_Obj.ToString();
65            the_Reg.Close();
66            the_rar = the_rar.Substring(1, the_rar.Length - 7);
67            the_Info = " X " + " 1.rar " + " " + "C:";
68            the_StartInfo = new ProcessStartInfo();
69            the_StartInfo.FileName = the_rar;
70            the_StartInfo.Arguments = the_Info;
71            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
72            the_Process = new Process();
73            the_Process.StartInfo = the_StartInfo;
74            the_Process.Start();
75            Response.Write("解压缩成功");
76        }

77        catch (Exception ex)
78        {
79            Response.Write(ex.ToString());
80        }

81    }

82}

83
 
 
服务器端目录:
 
客户端解压缩的变通方法:
1页 第1上一页1下一页
相关的教程: RAR 压缩 解压
收藏此教程

当前平均分: -1.0(1 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: