Esempio n. 1
0
        /// <summary>
        /// 解压文件
        /// <param> -aoa表示直接覆盖现有文件,且没有提示。类似的还有:</param>
        /// <param> -aos跳过现有文件不会覆盖</param>
        /// <param> -aou如果相同文件名的文件已存在,将自动重命名被释放的文件</param>
        /// <param> -aot如果相同文件名的文件已存在,将自动重命名现有的文件</param>
        /// </summary>
        /// <param name="zipFilePath">源文件</param>
        /// <param name="destPath">目标文件夹</param>
        /// <param name="Duplicateactor">覆盖时操作选项</param>
        /// <returns></returns>
        public bool DeCompress(string zipFilePath, string destPath, DuplicateOperate Duplicateactor = DuplicateOperate.Overwrite)
        {
            var operation = string.Empty;

            switch (Duplicateactor)
            {
            case DuplicateOperate.Overwrite:
                operation = " -aoa";
                break;

            case DuplicateOperate.Skip:
                operation = " -aos";
                break;

            case DuplicateOperate.Rename:
                operation = " -aou";
                break;

            case DuplicateOperate.RenameOld:
                operation = " -aot";
                break;

            default:
                break;
            }

            Log.Debug($"DeCompress Start zipFilePath[{zipFilePath}]");
            Process process   = new Process();
            bool    succeeded = false;

            System.DateTime startTime = System.DateTime.Now;
            process.StartInfo.FileName               = SEVENZIPEXEFILEPATH;
            process.StartInfo.UseShellExecute        = false; //是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput  = true;  //接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
            process.StartInfo.RedirectStandardError  = true;  //重定向标准错误输出
            process.StartInfo.CreateNoWindow         = true;  //不显示程序窗口
            process.StartInfo.Arguments              = string.Format(@"x ""{0}"" -o""{1}"" {2}", zipFilePath, destPath, operation);
            // "x -o""D:\用户目录\下载\0723.zip"" -aoa"
            process.ErrorDataReceived  += ErrorDataReceived;
            process.OutputDataReceived += OutputDataReceived;
            process.Start();//启动程序
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
            Log.Debug($"SevenZipBot Start ");
            process.WaitForExit(ProcessTimeOut);//等待程序执行完退出进程
            succeeded = (startTime.AddMilliseconds(ProcessTimeOut) > System.DateTime.Now);

            process.Close();
            Log.Debug($"SevenZipBot End Arguments:" + zipFilePath + ", succeeded:" + succeeded);

            return(succeeded);
        }
Esempio n. 2
0
        /// <summary>
        /// unzip files
        /// <param> -aoa means to directly overwrite the existing file without prompting. There are similar: </ param>
        /// <param> -aos will not overwrite existing files if skipped </ param>
        /// <param> -aou If a file with the same file name already exists, it will automatically rename the released file </ param>
        /// <param> -aot If a file with the same file name already exists, it will automatically rename the existing file </ param>
        /// </ summary>
        /// <param name = "zipFilePath"> source file </ param>
        /// <param name = "destPath"> target folder </ param>
        /// <param name = "Duplicateactor"> Operation options when overwriting </ param>
        /// <returns> </ returns>
        public bool DeCompress(string zipFilePath, string destPath, DuplicateOperate Duplicateactor = DuplicateOperate.Overwrite)
        {
            var operation = string.Empty;

            switch (Duplicateactor)
            {
            case DuplicateOperate.Overwrite:
                operation = " -aoa";
                break;

            case DuplicateOperate.Skip:
                operation = " -aos";
                break;

            case DuplicateOperate.Rename:
                operation = " -aou";
                break;

            case DuplicateOperate.RenameOld:
                operation = " -aot";
                break;

            default:
                break;
            }

            Log.Debug($"DeCompress Start zipFilePath[{zipFilePath}]");
            Process process   = new Process();
            bool    succeeded = false;

            System.DateTime startTime = System.DateTime.Now;
            process.StartInfo.FileName               = SEVENZIPEXEFILEPATH;
            process.StartInfo.UseShellExecute        = false; // Whether to use the operating system shell to start
            process.StartInfo.RedirectStandardInput  = true;  // Accept input information from the calling program
            process.StartInfo.RedirectStandardOutput = true;  // The output information is obtained by the calling program
            process.StartInfo.RedirectStandardError  = true;  // Redirect standard error output
            process.StartInfo.CreateNoWindow         = true;  // Do not display the program window
            process.StartInfo.Arguments              = string.Format(@"x ""{0}"" -o""{1}"" {2}", zipFilePath, destPath, operation);
            // "x -o" "D:\User Directory\Download\0723.zip" "-aoa"
            process.ErrorDataReceived  += ErrorDataReceived;
            process.OutputDataReceived += OutputDataReceived;
            process.Start(); // Start the program
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
            Log.Debug($"SevenZipBot Start");
            process.WaitForExit(ProcessTimeOut); // Wait for the program to finish and exit the process
            succeeded = (startTime.AddMilliseconds(ProcessTimeOut) > System.DateTime.Now);

            process.Close();
            Log.Debug($"SevenZipBot End Arguments:" + zipFilePath + ", succeeded:" + succeeded);

            return(succeeded);
        }