现在需要把客户选择的多个文件夹压缩为一个压缩包。只需要写一个方法,如何把多个文件夹打包为一个压缩包,我是把文件都复制到一个文件夹里面,然后压缩这个文件夹,但是不符合要求。求大虾门指点一下其它的方法,有个例子看看也行。

解决方案 »

  1.   

    利用tar32.dll
     [DllImport("tar32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
            static extern int Tar(IntPtr _hwnd, string _szCmdLine, StringBuilder _szOutput, int _dwSize);
       public static bool Create(
                string asDestinationFilePath,
                bool abCompress,
                string[] asSourceFilePaths)
            {
                string sCmd = @"-c ";
                if (abCompress)
                {
                    sCmd += @"-z ";
                }
                sCmd += @"--use-directory=0 ";
                sCmd += asDestinationFilePath + @" ";
                for (int i = 0; i < asSourceFilePaths.Length; i++)
                {
                    sCmd += asSourceFilePaths[i] + @" ";
                }
                int iRet = Tar(IntPtr.Zero, sCmd, null, 0);
                if (iRet != 0)
                {
                    return false;
                }            return true;
            }    }详细使用你自己搜下
      

  2.   

    http://www.cnblogs.com/time-is-life/archive/2012/08/07/2627253.html 生成zip文件夹
      

  3.   

    /// <summary>
            /// 压缩文件夹
            /// </summary>
            /// <param name="FilePath">需要压缩文件夹路径</param>
            /// <param name="FileName">压缩后文件名</param>
            /// <param name="SavePath">压缩后存放路径</param>
            /// <param name="PassWord">压缩密码,null为无密码</param>
            /// <returns>异常消息,成功返回null</returns>
            public static string SetZipFile(string []FilePath, string FileName, string SavePath, string PassWord)
            {
                try
                {
                    ZipFile zipfile = new ZipFile(SavePath + "\\" + FileName + ".zip", Encoding.Default);
                    if (PassWord != string.Empty)
                        zipfile.Password = PassWord;
                    foreach (var path in FilePath)
                    {
                        zipfile.AddDirectory(path);
                        zipfile.Save();
                    }
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
                return null;
            }
    需要Ionic.Zip
      

  4.   

    你是调用WinRAR来压缩吗?如果是,WinRAR支持用命令行参数调用,压缩多个文件/文件夹,具体看WinRAR帮助。
    然后,用System.Diagnostics.Process.Start(WinRAR路径 + 空格 + 参数);
    调用WinRAR压缩即可。