当前位置:网站首页>TXT_ File encryption and compression
TXT_ File encryption and compression
2022-06-13 07:03:00 【AndroidOrCSharp】
WinRar hit tar package
string WinRarDir = GetWinRarDir();
WinrarZip(ClearingFileDir, "p_report", dateTime, batchNo.ToString(), Dir, FileName, WinRarDir, "100000000000306");
/// <summary>
/// collect files
/// </summary>
/// <param name="OutPutDir"> The output path </param>
/// <param name="ZipName"></param>
/// <param name="Date"></param>
/// <param name="BatchNo"></param>
/// <param name="FileDir"></param>
/// <param name="FileName"></param>
/// <param name="WinRarDir"> Compressor directory </param>
/// <param name="seatNo"> Organization code </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;
// Source file encryption
log.Info(" The source file format is modified to UTF-8");
SendMsgEdit(" The source file format is modified to UTF-8");
TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
log.Info(" Source file encryption ");
SendMsgEdit(" Source file encryption ");
EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + ZipName + FileName);
log.Info(" New encrypted file added to TAR In bag ");
SendMsgEdit(" New encrypted file added to TAR In bag ");
// New encrypted file / The original file is added to TAR In bag
//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 Add package to send package
log.Info(" TAR Add package to send package ");
SendMsgEdit(" TAR Add package to send package ");
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(" Exception occurred in fast money forward file encryption , Abnormal information :{0}",ex.Message));
SendMsgEdit(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
}
}
// obtain winrar The installation path
private string GetWinRarDir()
{
RegistryKey rkey = Registry.CurrentUser;
RegistryKey subKey = rkey.OpenSubKey(@"Software\WinRAR SFX", true);
log.Info(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
SendMsgEdit(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
return subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString();
}
//txt Change the file to 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();
}
// Encrypt files
private void EncryptFile(string InputFilename, string OutputFilename)
{
string strKey = IniFile.GetCfgValue("SubSystem,QP", "DESKey");
SendMsgEdit(" Read the fast money key :" + strKey);
EncryptDESFile.EncryptFile(InputFilename, OutputFilename, strKey, Encoding.UTF8, FileContentModify);
}
/// <summary>
/// Add... Before the contents of the document 8 individual 8, Fill in blanks after the contents of the document
/// </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>
/// The input content needs to be modified with the specified output file code DES File encryption
/// </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
{
// Determine the key length
sKey = KeyLengthValid(sKey);
// Determine the encoding method of the input file
Encoding fileEncode = GetType(InputFilename);
// Read in file encoding
FileInput = new FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
sReader = new StreamReader(FileInput, fileEncode);
// Read in the data
string strRead = sReader.ReadToEnd();
if (FileContent != null)
{
strRead = FileContent(strRead);
}
//DES Encrypting device
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);
// Write encrypted data with encrypted stream
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();
}
}
Start console call 7zip Command packaging
// obtain winrar The installation path
private string GetRarDir()
{
RegistryKey rkey = Registry.CurrentUser;
RegistryKey subKey = rkey.OpenSubKey(@"Software\7-Zip", true);
log.Info(" Compressor installation path :" + subKey.GetValue(subKey.GetValueNames()[0].ToString()).ToString());
SendMsgEdit(" Compressor installation path :" + 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;
// Source file encryption
log.Info(" The source file format is modified to UTF-8");
SendMsgEdit(" The source file format is modified to UTF-8");
TransformFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
log.Info(" Source file encryption ");
SendMsgEdit(" Source file encryption ");
EncryptFile(FileDir + "\\" + FileName, FileDir + "\\" + FileName);
log.Info(" New encrypted file added to TAR In bag ");
SendMsgEdit(" New encrypted file added to TAR In bag ");
// start-up CMD txt The file is compressed into tar package
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");
// Delete after compression txt file
fileInfo = new FileInfo(OutPutDir + FileName);
fileInfo.Delete();
// Open a new one CMD tar Pack compressed into 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();
// Delete after compression tar file
fileInfo = new FileInfo(OutPutDir + ZipName + "_" + seatNo + "_" + Date + "_" + BatchNo + ".tar");
fileInfo.Delete();
}
catch (Exception ex)
{
log.Info(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
SendMsgEdit(string.Format(" Exception occurred in fast money forward file encryption , Abnormal information :{0}", ex.Message));
}
}
winrar command :
psi.Arguments = string.Format("a -av- -ep1 -afzip {0}.rar {0}", floderPath);// Compress zip Keep the original file
psi.Arguments = string.Format("a -av- -ep1 -afrar {0}.rar {0}", floderPath);// Compress rar Keep the original file
psi.Arguments = string.Format("m -df- -av- -ep1 -afzip {0}.rar {0}", floderPath); // Compress zip Delete the original file
psi.Arguments = string.Format("m -df- -av- -ep1 -afrar {0}.rar {0}", floderPath);// Compress rar Delete the original file
边栏推荐
- 玄武云科技通过上市聆讯:业绩波动明显,陈永辉等三人为控股股东
- 基于FPGA的ds18b20温度传感器使用
- RT thread simulator lvgl control: button button style
- Is it safe for Hangzhou Securities to open an account?
- Try to use renderdoc to view the shader code of UE
- The biggest highlight of wwdc2022: metalfx
- Continuous management design
- 15、 IO stream (I)
- Nfv basic overview
- AIO Introduction (VIII)
猜你喜欢
How to quickly support the team leader to attract new fission users in the business marketing mode of group rebate?
[RS-422 and RS-485] RS-422 and RS-485 serial interface standard
Tidb statistics
【微弱瞬态信号检测】混沌背景下微弱瞬态信号的SVM检测方法的matlab仿真
How to use Wangyou DFM software for cold plate analysis
想进行快速钢网设计,还能保证钢网质量? 来看这里
Common method of props changing value V-model sync
Try to use renderdoc to view the shader code of UE
RT-Thread 模拟器 simulator LVGL控件:slider 控件
Test development programmers, are you still confused? You can't define yourself as a yard farmer
随机推荐
1154. day of the year
如何使用望友DFM軟件進行冷板分析
Upper computer development (software test of firmware download software)
Tidb dashboard modify port
Through the function seaborn cubehelix_ Palette build order palette
2022-06-12:在N*N的正方形棋盤中,有N*N個棋子,那麼每個格子正好可以擁有一個棋子。 但是現在有些棋子聚集到一個格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二維數組代錶,一
Unable to find method 'org gradle. api. artifacts. result. ComponentSelectionReason. getDesc
json. Stringify() and json The difference between parse () and json Usage of stringify()
New Taishan crowdfunding business diversion fission growth model in 2022
What is the essence of social e-commerce disruption? How can businesses get more traffic?
不间断管理设计
Jinglianwen technology provides voice data acquisition and labeling services
How to seize the bonus of social e-commerce through brand play to achieve growth and profit?
Ticdc synchronization task
线程池中的 工作线程如何被回收
Jinglianwen technology provides a one-stop smart home data acquisition and labeling solution
面试必刷算法TOP101之单调栈 TOP31
在产业互联网的概念刚刚出现时,人们仅仅只是将其看成是一个获取B端流量的方法
[system analysis and design] college student association management system
【转】FPGA面试题