RAR文件下载解压C# ASP.NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;

public partial class Down : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DownloadFile("http://www.cnblogs.com/tt.rar", @"d:\tt.rar");
        UnRAR(@"d:\", "tt.rar", @"d:\", true);
    }
    public void DownloadFile(string StrUrl, string StrFileName)
    {
        FileStream fs;
        long lStartPos = 0; //返回上次下载字节
        long lCurrentPos = 0;//返回当前下载字节
        long lDownloadFile;//返回当前下载文件长度
        if (System.IO.File.Exists(StrFileName))
        {
            File.Delete(StrFileName);
            //fs = File.OpenWrite(StrFileName);
            //lStartPos = fs.Length;
            //fs.Seek(lStartPos, SeekOrigin.Current);
            //移动文件流中的当前指针
        }
        //else
        //{
        //    fs = new FileStream(StrFileName, FileMode.Create);
        //    lStartPos = 0;
        //}
        fs = new FileStream(StrFileName, FileMode.Create);
        lStartPos = 0;
        //打开网络连接
        try
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(StrUrl);
            long length = request.GetResponse().ContentLength;
            lDownloadFile = length;
            if (lStartPos > 0)
                request.AddRange((int)lStartPos); //设置Range值
            //向服务器请求,获得服务器回应数据流
            Stream ns = request.GetResponse().GetResponseStream();
            byte[] nbytes = new byte[1024];
            int nReadSize = 0;
            nReadSize = ns.Read(nbytes, 0, 1024);
            while (nReadSize > 0)
            {
                fs.Write(nbytes, 0, nReadSize);
                nReadSize = ns.Read(nbytes, 0, 1024);
                lCurrentPos = fs.Length;
            }
            fs.Close();
            ns.Close();
        }
        catch (Exception ex)
        {
            fs.Close();
        }
    }

    /// <summary>
    /// 解压缩到指定文件夹
    /// </summary>
    /// <param name="RARFilePath">压缩文件存在的目录 </param>
    /// <param name="RARFileName">压缩文件名称 </param>
    /// <param name="UnRARFilePath">解压到文件夹</param>
    /// <returns></returns>
    protected bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath, bool deleteRarFile)
    {
        //解压缩
        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 -o+ " + " " + 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();
         
            while (!the_Process.HasExited)
            {
            }
            the_Process.WaitForExit();
            if (deleteRarFile)
            {
                File.Delete(@"d:\tt.rar");
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }

    }

}

 

 

 

posted @ 2013-04-19 11:11  烟F火  阅读(315)  评论(0编辑  收藏  举报