TXT_文件加密与压缩

WinRar打tar包

  string WinRarDir = GetWinRarDir();
  WinrarZip(ClearingFileDir, "p_report", dateTime, batchNo.ToString(), Dir, FileName, WinRarDir, "100000000000306");

/// <summary>
        /// 文件打包
        /// </summary>
        /// <param name="OutPutDir">输出路径</param>
        /// <param name="ZipName"></param>
        /// <param name="Date"></param>
        /// <param name="BatchNo"></param>
        /// <param name="FileDir"></param>
        /// <param name="FileName"></param>
        /// <param name="WinRarDir">压缩程序目录</param>
        /// <param name="seatNo">机构代码</param>
        private void WinrarZip(string OutPutDir, string ZipName, string Date, string BatchNo, string FileDir, string FileName, string WinRarDir, string seatNo)
        {
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo(WinRarDir + "\\winrar.exe");
                psi.WindowStyle = ProcessWindowStyle.Hidden;

                //源文件加密
                log.Info("       源文件格式修改为UTF-8");
                SendMsgEdit("       源文件格式修改为UTF-8");
                TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("       源文件加密");
                SendMsgEdit("       源文件加密");
                EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + ZipName + FileName);
                log.Info("       新加密文件添加到TAR包中");
                SendMsgEdit("       新加密文件添加到TAR包中");

                //新加密文件/原文件添加到TAR包中
                //psi.Arguments = string.Format("a -df -av- -ep1 -ibck -afzip {0}{1}_{2}_{3}.tar {4}", OutPutDir, ZipName, Date, BatchNo, FileDir + "\\" + FileName);
                psi.Arguments = string.Format("a -df -av- -ep1 -ibck -afzip {0}{1}_{2}_{3}_{4}.tar {5}", OutPutDir, ZipName, seatNo, Date, BatchNo, FileDir + "\\" + ZipName + FileName);
                Process process = Process.Start(psi);
                process.WaitForExit();


                //TAR包添加到发送包中
                log.Info("       TAR包添加到发送包中");
                SendMsgEdit("       TAR包添加到发送包中");
                psi.Arguments = string.Format("a -av- -ep1 -ibck {0}{1}_{2}_{3}_{4}.tar.gz {5}", OutPutDir, ZipName, seatNo, Date, BatchNo, FileDir + "\\" + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                process = Process.Start(psi);
                process.WaitForExit();
            }
            catch (Exception ex) 
            {
                log.Info(string.Format("快钱正向文件加密发生异常,异常信息:{0}",ex.Message));
                SendMsgEdit(string.Format("快钱正向文件加密发生异常,异常信息:{0}", ex.Message));
            }

        }

        //获取winrar安装路径
        private string GetWinRarDir()
        {
            RegistryKey rkey = Registry.CurrentUser;
            RegistryKey subKey = rkey.OpenSubKey(@"Software\WinRAR SFX", true);
            log.Info("压缩程序安装路径:" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            SendMsgEdit("压缩程序安装路径:" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            return subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString();
        }

        //txt文件修改为UTF-8
        private void TransformFile(string InputFilename, string OutputFilename)
        {
            FileStream fStream = new FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader sReader = new StreamReader(fStream, Encoding.Default);

            string strRead = sReader.ReadToEnd();
            sReader.Close();
            fStream.Close();

            fStream = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write);
            StreamWriter sWriter = new StreamWriter(fStream, Encoding.UTF8);
            sWriter.Write(strRead);

            sWriter.Close();
            fStream.Close();
        }

        //对文件加密
        private void EncryptFile(string InputFilename, string OutputFilename)
        {
            string strKey = IniFile.GetCfgValue("SubSystem,QP", "DESKey");
            SendMsgEdit("       读取快钱密钥:" + strKey);
            EncryptDESFile.EncryptFile(InputFilename, OutputFilename, strKey, Encoding.UTF8, FileContentModify);
        }

        /// <summary>
        /// 文件内容前加8个8,文件内容后补空格
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        private string FileContentModify(string strInput)
        {
            string strRead = "88888888" + strInput;
            byte[] byteInput = Encoding.UTF8.GetBytes(strRead);

            return strRead.PadRight(strRead.Length + (8 - byteInput.Length % 8), ' ');
        }



        /// <summary>
        /// 输入内容需要修改与指定输出文件编码的DES文件加密
        /// </summary>
        /// <param name="InputFilename"></param>
        /// <param name="OutputFilename"></param>
        /// <param name="sKey"></param>
        /// <param name="EncryptEncode"></param>
        /// <param name="FileContent"></param>
        public static void EncryptFile(string InputFilename, string OutputFilename, string sKey, Encoding EncryptEncode, FileContentHander FileContent)
        {
            FileStream FileInput = null;
            StreamReader sReader = null;
            FileStream FileOutput = null;
            CryptoStream cryptoStream = null;

            try
            {
                //判断密钥长度
                sKey = KeyLengthValid(sKey);
                //判断输入文件的编码方式
                Encoding fileEncode = GetType(InputFilename);

                //以文件编码的方式读取
                FileInput = new FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sReader = new StreamReader(FileInput, fileEncode);

                //读入数据
                string strRead = sReader.ReadToEnd();
                if (FileContent != null)
                {
                    strRead = FileContent(strRead);
                }

                //DES加密器
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Mode = CipherMode.ECB;
                DES.Padding = PaddingMode.None;
                DES.Key = EncryptEncode.GetBytes(sKey);
                DES.IV = EncryptEncode.GetBytes(sKey);

                ICryptoTransform desEncrypt = DES.CreateEncryptor();
                FileOutput = new FileStream(OutputFilename, FileMode.Create, FileAccess.Write);
                cryptoStream = new CryptoStream(FileOutput, desEncrypt, CryptoStreamMode.Write);

                //用加密流写入加密数据
                byte[] byteOutput = EncryptEncode.GetBytes(strRead);
                cryptoStream.Write(byteOutput, 0, byteOutput.Length);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                cryptoStream.Close();
                FileOutput.Close();
                sReader.Close();
                FileInput.Close();
            }
        }

启动控制台调用 7zip命令打包

        //获取winrar安装路径
        private string GetRarDir()
        {
            RegistryKey rkey = Registry.CurrentUser;
            RegistryKey subKey = rkey.OpenSubKey(@"Software\7-Zip", true);
            log.Info("压缩程序安装路径:" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            SendMsgEdit("压缩程序安装路径:" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
            return subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString();
        }

   private void SevenZip(string OutPutDir, string ZipName, string Date, string BatchNo, string FileDir, string FileName, string RarDir, string seatNo)
        {
            try
            {
                FileInfo fileInfo;
                ProcessStartInfo psi = new ProcessStartInfo(RarDir + "\\7z.exe");
                psi.WindowStyle = ProcessWindowStyle.Hidden;

                //源文件加密
                log.Info("       源文件格式修改为UTF-8");
                SendMsgEdit("       源文件格式修改为UTF-8");
                TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("       源文件加密");
                SendMsgEdit("       源文件加密");
                EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
                log.Info("       新加密文件添加到TAR包中");
                SendMsgEdit("       新加密文件添加到TAR包中");

                //启动CMD  txt文件压缩成tar包
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                string message = "";
                string path = RarDir.Substring(0,1)+":";
                string exePath = @"cd "+ RarDir;
                string command = string.Format(@"7z a " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar" + " " + OutPutDir + FileName);

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.StandardInput.WriteLine(path);
                process.StandardInput.WriteLine(exePath);
                process.StandardInput.WriteLine(command);
                process.StandardInput.WriteLine("exit");
                message = process.StandardOutput.ReadToEnd();

                command = string.Format(@"7z a " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar.gz" + " " + OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                //压缩完后删除txt文件
                fileInfo = new FileInfo(OutPutDir + FileName);
                fileInfo.Delete();
                //新开一个CMD tar包压缩成tar.gz
                Process tarGz = new Process();
                tarGz.StartInfo.FileName = "cmd.exe";
                tarGz.StartInfo.UseShellExecute = false;
                tarGz.StartInfo.RedirectStandardInput = true;
                tarGz.StartInfo.RedirectStandardOutput = true;
                tarGz.StartInfo.RedirectStandardError = true;
                tarGz.StartInfo.CreateNoWindow = true;
                tarGz.Start();
                tarGz.StandardInput.WriteLine(path);
                tarGz.StandardInput.WriteLine(exePath);
                tarGz.StandardInput.WriteLine(command);
                tarGz.StandardInput.WriteLine("exit");
                message = tarGz.StandardOutput.ReadToEnd();
                //压缩完后删除tar文件
                fileInfo = new FileInfo(OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
                fileInfo.Delete();
            }
            catch (Exception ex)
            {
                log.Info(string.Format("快钱正向文件加密发生异常,异常信息:{0}", ex.Message));
                SendMsgEdit(string.Format("快钱正向文件加密发生异常,异常信息:{0}", ex.Message));
            }

        }

winrar命令:

            psi.Arguments = string.Format("a -av- -ep1 -afzip {0}.rar {0}", floderPath);//压缩zip保留原文件

            psi.Arguments = string.Format("a -av- -ep1 -afrar {0}.rar {0}", floderPath);//压缩rar保留原文件

            psi.Arguments = string.Format("m -df- -av- -ep1 -afzip {0}.rar {0}", floderPath);   //压缩zip删除原文件

            psi.Arguments = string.Format("m -df- -av- -ep1 -afrar {0}.rar {0}", floderPath);//压缩rar删除原文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值