我从数据库中取得文件路径,循环将其打包压缩成zip或rar(多个文件),当压缩包不存在的时候新建压缩包,如果此压缩包已经存在则将要压缩的文件压缩进此压缩包,请问各位大虾,如何实现?注意是要将多个文件一个一个的压缩进一个压缩包中。

解决方案 »

  1.   

    C# 文件压缩与解压(ZIP格式) 
      

  2.   

    调用rar的dos命令就好
    详细用法在winRAR安装目录下面的Rar.txt中
      

  3.   

    你下一个ICSharpCode.SharpZipLib.dll这个是目前我用过最好用的.zip压缩与解压类
      

  4.   

    给你一个例子
    /// <summary>目录strSrc到指定目录strDest
            /// 
            /// </summary>
            /// <param name="strSrc">导出的目录</param>
            /// <param name="strDest">指定的目录</param>
            public string Rar(string strSrc, string strDest)
            {
                Process p = new Process();
                string cmd = String.Format("a -en  -ep1 {0} {1} ", "\"" + strDest + "\"", "\"" + strSrc + "\"");
                string cmd1 = String.Format("a -en  -ap{0} -ep {1}", "新的文件", "\"" + strDest + "\"");
                try
                {
                    for (int k = 0; k < 2; k++)
                    {
                        p.StartInfo.FileName = Application.StartupPath + "\\Rar.exe";
                        if (k == 0)
                        {
                            p.StartInfo.Arguments = cmd;
                        }
                        else
                        {                        for (int i = 0; i < 3; i++)
                            {
                                if (TestG[i] != null)
                                {
                                    if (TestG[i].Paper != null)
                                    {
                                        cmd1 = cmd1 + " \"" + 文件 + "\"";
                                        for (int j = 0; j < Count; j++)
                                        {
                                            if (TestG[i].Paper.part(i) != null)
                                            { cmd1 = cmd1 + " \"" + 文件 + "\""; }
                                            }
                                        }
                                    }
                                }
                            }                        p.StartInfo.Arguments = cmd1;
                        }
                        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        p.Start();
                        p.WaitForExit();
                        p.Close();                }
                    if (Directory.Exists(Application.StartupPath + "\\Temp"))
                    {
                        Directory.Delete(Application.StartupPath + "\\Temp", true);
                    }
                    return "压缩完成";
                }
                catch (Exception)
                {
                    return "压缩失败";
                }需要windows里的RAR.exe文件 拷贝到工程中
      

  5.   

       //压缩文件
        private void Export(string meetingTitle, ref ArrayList fileArray)
        {
            string pathRoot = Server.MapPath("/");
            string winRAR = pathRoot + "meeting/WinRAR/WinRAR.exe";
            string vfolder = "/upload/meeting/rar/";
            string rarfolder = Server.MapPath("~" + vfolder);        //添加文件
            StringBuilder file = new StringBuilder();
            for (int i = 0; i < fileArray.Count; i++)
            {
                file.Append("\"" + Tool.CStr(fileArray[i]) + "\" ");
            }        if (!Directory.Exists(rarfolder))
                Directory.CreateDirectory(rarfolder);        string makedFile = "\"" + rarfolder + meetingTitle + ".rar\"";        if (File.Exists(makedFile) == true)
            {
                try
                {
                    File.Delete(makedFile);
                }
                catch
                {
                    Tool.OpenWindow("文件无法删除,请先手动删除!");
                }
            }        string arguments = @"a " + makedFile + " " + file;        if (file.Length > 0)
            {
                try
                {
                    ProcessStartInfo info = new ProcessStartInfo();
                    info.FileName = winRAR;         //winRAR目录
                    info.Arguments = arguments;     //参数
                    info.WorkingDirectory = pathRoot;                Process p = new Process();
                    p.StartInfo = info;
                    p.Start();                      //启动
                    p.WaitForExit();                //无限期等待进程退出
                    p.Close();                //写入数据库
                    string thisHost = Request.Url.ToString();
                    thisHost = thisHost.Substring(thisHost.IndexOf("//") + 2);
                    thisHost = thisHost.Substring(0, thisHost.IndexOf("/"));
                    thisHost = "http://" + thisHost;
                    BLL.Meeting.ExportRar er = new BLL.Meeting.ExportRar();
                    er.Add(_meetingID, meetingTitle, (thisHost + vfolder + meetingTitle + ".rar"), Tool.Cint(Session["AdminID"]));
                }
                catch (Exception e)
                {
                    Tool.OpenWindow("压缩失败!");
                }
                //删除生成的附件
                for (int i = 0; i < fileArray.Count; i++)
                {
                    try
                    {
                        File.Delete(Tool.CStr(fileArray[i]));
                    }
                    catch
                    { }
                }
            }
            else
            {
                Tool.OpenWindow("没有需要压缩的文件!");
                return;
            }
        }