您的位置:首页 > 编程语言 > ASP

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

2015-02-12 15:37 211 查看
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 "";

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: