string fileRar = DateTime.Now.ToString("yyyyMMddhhmmss");
string file = Server.MapPath("~/rar/") +fileRar+ "3.rar";
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/shell/open/command"); //win 2003 xp 查找rar的注册表
if (the_Reg == null)
{
   the_Reg = Registry.ClassesRoot.OpenSubKey("WinRAR\\Shell\\Open\\Command");  //server 2008/win7
}
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    " + fileRar + "  " +pathFile; //执行命令 压缩后的文件  被压缩的文件
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = Server.MapPath("~/rar/");//获取或设置要启动的进程的初始目录。初始化目录
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start(); //执行成功
the_Process.Close();
the_Process.Dispose();/*执行到这里 已经生成把我的txt压缩成rar*/ 并且可以使用 关键是输出流让用户下载  下载可以正常下载 只是下载后的
不能解压 而我打开根目录下面的生成 rar是可以解压并正常使用的。 下载后的报(此档案格式末知或已损坏)Response.ContentType = "WinRar/rar";
Response.AppendHeader("Content-Disposition", "attachment;filename=aaaa.rar");//用法的方式输出
Response.BinaryWrite(File.ReadAllBytes(file)); //弹出下载框/*就是这个流输出的问题 可又找不到是哪里的问题 输出txt是正常的 excel也是正常的*/ 

解决方案 »

  1.   

    是不是Response.ContentType = "WinRar/rar"这里不对啊
    用Response.ContentType = "application/octet-stream";试试?
      

  2.   

    我的方法给你看下  是可以的            byte[] buffer = new byte[2048];            
                int readCount = FtpStream.Read(buffer, 0, buffer.Length);           
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));  
                while (readCount > 0)
                {
                    Response.OutputStream.Write(buffer, 0, readCount);
                    readCount = FtpStream.Read(buffer, 0, buffer.Length);
                }
      

  3.   

    Response.ContentType = "application/octet-stream" 这种我已经用过 还是不行
      

  4.   

    3楼 麻烦你说一下FtpStream这个是使用的哪个?
      

  5.   

    我知道是输出问题 可就是不知道是输出的哪里有问题 因为生成后的rar是好的 
      

  6.   

    十六进制对比rar文件,看哪里不同
    猜测下载的文件要大那么一点,要么没先Response.Clear();
    要么输出了额外的东西
      

  7.   

    我的需求就是输出一个rar文件 输出下载后的文件比 压缩后的文件 要大很多 不晓得是怎么个情况 我已经在之前用Response.Clear();了 还是不行 而且结尾也有Response.End();
      

  8.   

    用记事本打开那个大文件,估计又是一个错误页
    不知道你程序流程,是压缩了就下载吗
    还没压完,文件是独占式访问
    File.ReadAllBytes报错
    因为已设置了ResponseHeader
    所以错误页已rar文件形式被下载
      

  9.   

    是的 压缩完了之后释放资源就开始下载 呵呵 但是单独弄一个rar下载却是好的 呵呵
      

  10.   

    the_Process.Start(); 只是开始异步执行命令
    要等压缩完成,需要正确使用
    the_Process.WaitForExit(n); 
    好像