Esempio n. 1
0
        /// <summary>
        /// 文件压缩7z  Mac 修改 2015年8月5日16:38:27
        /// </summary>
        /// <param name="szFileList">要压缩的原文件</param>
        /// <param name="szSaveFile">压缩包文件</param>
        /// <param name="szPassword">压缩包密码</param>
        /// <param name="prgCompress"></param>
        /// <param name="IsDelFile">压缩完成后是否删除原文件,默认不删除</param>
        /// <returns></returns>
        public static int CompressFiles(string szFileList, string szSaveFile, string szPassword, COMPRESSPRG prgCompress, bool isDelFile = false)
        {
            while (compressingThreadCount >= WinRARMaxThreadCount) //已经有MAX_THREAD_COUNT个线程在压缩,就等待
            {
                Thread.Sleep(200);                                 //如果太多并发压缩,等待200ms
            }
            ProcessStartInfo ps = null;
            Process          p  = null;

            try
            {
                compressingThreadCount++;
                //ConnectLan(@"\\192.168.20.164\utDir","NewAccount","123");
                //KillWinRAR();
                if (File.Exists(szSaveFile))
                {
                    //LogHelper.Error("压缩包已存在:" + szSaveFile);
                    File.Delete(szSaveFile);
                }
                string   compressType = "7z";
                string   path         = AppDomain.CurrentDomain.BaseDirectory;
                string   paramWinRar  = "";
                string   WinRarPath   = "";
                string[] files        = szFileList.Split(';');
                foreach (string fileName in files)      //遍历文件
                {
                    if (string.IsNullOrEmpty(fileName)) //文件名不能为空
                    {
                        continue;
                    }
                    if (File.Exists(fileName) || Directory.Exists(fileName))//文件和目录存在
                    {
                        if (compressType.Equals("7z"))
                        {
                            WinRarPath  = path + @"lib\7z.exe";
                            paramWinRar = string.Format(SEVEN_COMPRESS_FILE, szSaveFile, fileName, GetSevenCompressPwdArg(szPassword));
                        }

                        ps                  = new ProcessStartInfo(WinRarPath, paramWinRar);
                        ps.WindowStyle      = ProcessWindowStyle.Hidden;
                        ps.WorkingDirectory = WinRARTemp;
                        p           = new Process();
                        p.StartInfo = ps;
                        p.Start();
                        if (p != null)
                        {
                            p.WaitForExit();
                            //p.Close();
                        }

                        if (isDelFile)//删除原文件
                        {
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);
                            }
                            else if (Directory.Exists(fileName))
                            {
                                Directory.Delete(fileName, true);
                            }
                        }
                    }
                }
                return(1);
            }
            catch (Exception se)
            {
                //LogHelper.Error("压缩失败,WinRAR.exe路径:" + WinRARFileName + ",文件列表:" + szFileList + ",压缩包:" + szSaveFile, se);
                return(0);
            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (compressingThreadCount > 0)
                {
                    compressingThreadCount--;
                }
            }
        }
Esempio n. 2
0
        //[DllImport(@"FileComp.dll")]
        //[DllImport(@"F:\Test\FileComp\FileComp1\FileComp\Debug\FileComp.dll")]
        //[DllImport(@"F:\a\FileComp.dll")]
        //public static extern int DecompressFile(string szCompFile, string szSaveDir, string szPassword, COMPRESSPRG prgCompress);
        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="szCompFile">解压缩文件</param>
        /// <param name="szSaveDir">解压之后保存文件目录</param>
        /// <param name="szPassword">解压缩密码</param>
        /// <param name="prgCompress"></param>
        /// <returns></returns>
        //public static int DecompressFile(string szCompFile, string szSaveDir, string szPassword, COMPRESSPRG prgCompress)
        //{
        //    while (decompressingThreadCount >= WinRARMaxThreadCount)//已经有MAX_THREAD_COUNT个线程在压缩,就等待
        //    {
        //        Thread.Sleep(1000);//如果太多并发压缩,等待200ms
        //    }
        //    ProcessStartInfo ps = null;
        //    Process p = null;
        //    try
        //    {
        //        decompressingThreadCount++;
        //        //KillWinRAR();
        //        if (!File.Exists(szCompFile))
        //        {
        //            return 0;
        //        }
        //        if (!Directory.Exists(szSaveDir))
        //        {
        //            Directory.CreateDirectory(szSaveDir);
        //        }
        //        string paramWinRar = string.Format(DECOMPRESS_FILE, szCompFile, szSaveDir, GetCompressPwdArg(szPassword));

        //        ps = new ProcessStartInfo(WinRARFileName, paramWinRar);
        //        ps.WindowStyle = ProcessWindowStyle.Hidden;
        //        ps.WorkingDirectory = WinRARTemp;
        //        p = new Process();
        //        p.StartInfo = ps;
        //        p.Start();

        //        return 1;
        //    }
        //    catch (InvalidOperationException ex)
        //    {
        //        LogHelper.Error("解压缩失败,WinRAR.exe路径:" + WinRARFileName + ",解压路径:" + szSaveDir + ",压缩包:" + szCompFile, ex);
        //        return 0;
        //    }
        //    catch (Exception se)
        //    {
        //        LogHelper.Error("解压缩失败,WinRAR.exe路径:" + WinRARFileName + ",解压路径:" + szSaveDir + ",压缩包:" + szCompFile, se);
        //        return 0;
        //    }
        //    finally
        //    {
        //        if (p != null)
        //        {
        //            p.WaitForExit();
        //            p.Close();
        //        }
        //        if (decompressingThreadCount > 0)
        //        {
        //            decompressingThreadCount--;
        //        }
        //    }
        //}

        /// <summary>
        /// 解压缩文件7z  Mac 修改 2015年8月5日16:38:27
        /// </summary>
        /// <param name="szCompFile">解压缩文件</param>
        /// <param name="szSaveDir">解压之后保存文件目录</param>
        /// <param name="szPassword">解压缩密码</param>
        /// <param name="prgCompress"></param>
        /// <returns></returns>
        public static int DecompressFile(string szCompFile, string szSaveDir, string szPassword, COMPRESSPRG prgCompress)
        {
            while (decompressingThreadCount >= WinRARMaxThreadCount) //已经有MAX_THREAD_COUNT个线程在压缩,就等待
            {
                Thread.Sleep(1000);                                  //如果太多并发压缩,等待200ms
            }
            ProcessStartInfo ps = null;
            Process          p  = null;

            try
            {
                decompressingThreadCount++;
                //KillWinRAR();
                if (!File.Exists(szCompFile))
                {
                    return(0);
                }
                if (!Directory.Exists(szSaveDir))
                {
                    Directory.CreateDirectory(szSaveDir);
                }
                string compressType = "7z";
                //string path = HttpRuntime.AppDomainAppPath.ToString();
                string path        = AppDomain.CurrentDomain.BaseDirectory.ToString();
                string paramWinRar = "";
                string WinRarPath  = "";
                if (compressType.Equals("7z"))
                {
                    WinRarPath  = path + @"lib\7z.exe";
                    paramWinRar = string.Format(SEVEN_DECOMPRESS_FILE, szCompFile, szSaveDir, GetSevenCompressPwdArg(szPassword));
                }

                ps                  = new ProcessStartInfo(WinRarPath, paramWinRar);
                ps.WindowStyle      = ProcessWindowStyle.Hidden;
                ps.WorkingDirectory = WinRARTemp;
                p           = new Process();
                p.StartInfo = ps;
                p.Start();

                return(1);
            }
            catch (InvalidOperationException ex)
            {
                //LogHelper.Error("解压缩失败,WinRAR.exe路径:" + WinRARFileName + ",解压路径:" + szSaveDir + ",压缩包:" + szCompFile, ex);
                return(0);
            }
            catch (Exception se)
            {
                //LogHelper.Error("解压缩失败,WinRAR.exe路径:" + WinRARFileName + ",解压路径:" + szSaveDir + ",压缩包:" + szCompFile, se);
                return(0);
            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (decompressingThreadCount > 0)
                {
                    decompressingThreadCount--;
                }
            }
        }