ASP.NET生成压缩文件(rar打包)2

来源:互联网 发布:手机淘宝单怎么刷 编辑:程序博客网 时间:2024/05/17 04:56
using System.IO;using System.IO.Compression; public static void fileCompress(string f_name)    {         string filename =HttpContext.Current.Server.MapPath(f_name);        FileStream infile = File.OpenRead(filename);        byte[] buffer = new byte[infile.Length];        infile.Read(buffer, 0, buffer.Length);        infile.Close();         FileStream outfile = File.Create(Path.ChangeExtension(filename, "zip"));        GZipStream gz = new GZipStream(outfile, CompressionMode.Compress);        gz.Write(buffer, 0, buffer.Length);        gz.Close();    }压缩包//解压代码  private string UnZipFile(string strFile)    {        try        {            int i = strFile.LastIndexOf("\\");            string strFileName = strFile.Substring(i + 1);            string strFileName1 = strFileName.Substring(0, strFileName.LastIndexOf("."));            //string strPath = strFile.Substring(0,strFile.Length- strFile.LastIndexOf(".")+1);//            string strPath = strFile.Substring(0, strFile.Length - i + 1);            string strPath = strFile.Substring(0,  i);            strPath = strPath.Replace("-", "");            string strPathFile = strPath + "\\" + strFileName1;            //string ServerDir = Server.MapPath("");//rar路径               System.Diagnostics.Process Process1 = new System.Diagnostics.Process();            Process1.StartInfo.FileName = ServerDir + "\\Rar.exe";            Directory.CreateDirectory(strPathFile);   //创建解压文件夹               //if (!File.Exists(strFile))            //{            //    Response.Write("<script language='javascript'>alert('" + strFile + "不存在');</script>");            //}            Process1.StartInfo.Arguments = " e " + strFile + "   " + strPathFile + " -y";            Process1.Start();//解压开始               while (!Process1.HasExited)                       //等待解压的完成               {            }            string strBak = strPath + "\\" + strFileName1 + "\\tjnb" + strNd  + ".dat";            string strReport = strPath + "\\" + strFileName1 + "\\上报.dat";            if (File.Exists(strBak))            {                return strBak;            }            else if (File.Exists(strReport))            {                return strReport;            }            else            {                return "";            }            //File.Delete(strPath + "\\" + strFileName + ".rar");//删除rar文件           }        catch (Exception ex)        {            throw ex;            return "";        }    }

0 0
原创粉丝点击