123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace ZipFileHelper
- {
- public class RarOperate
- {
- private CommonFunctions commFuncs = CommonFunctions.getInstance();
- #region 单例模式
- private static RarOperate uniqueInstance;
- private static object _lock = new object();
- public RarOperate() { }
- public static RarOperate getInstance()
- {
- if (null == uniqueInstance) //确认要实例化后再进行加锁,降低加锁的性能消耗。
- {
- lock (_lock)
- {
- if (null == uniqueInstance)
- {
- uniqueInstance = new RarOperate();
- }
- }
- }
- return uniqueInstance;
- }
- #endregion
- #region 压缩
- /// <summary>
- /// 使用Rar.exe压缩对象
- /// </summary>
- /// <param name="rarRunPathName">Rar.exe路径+对象名</param>
- /// <param name="objectPathName">被压缩对象路径+对象名</param>
- /// <param name="objectRarPathName">对象压缩后路径+对象名</param>
- /// <returns></returns>
- public CompressResults CompressObject(string rarRunPathName, string objectPathName, string objectRarPathName, string password,ref string errorinfo)
- {
- try
- {
- //被压缩对象是否存在
- int beforeObjectNameIndex = objectPathName.LastIndexOf('\\');
- string objectPath = objectPathName.Substring(0, beforeObjectNameIndex);
- //System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
- if (Directory.Exists(objectPathName)/*directoryInfo.Exists*/ == false && System.IO.File.Exists(objectPathName) == false)
- {
- return CompressResults.SourceObjectNotExist;
- }
- //将对应字符串转换为命令字符串
- string rarCommand = "\"" + rarRunPathName + "\"";
- string objectPathNameCommand = "\"" + objectPathName + "\"";
- int beforeObjectRarNameIndex = objectRarPathName.LastIndexOf('\\');
- int objectRarNameIndex = beforeObjectRarNameIndex + 1;
- string objectRarName = objectRarPathName.Substring(objectRarNameIndex);
- string rarNameCommand = "\"" + objectRarName + "\"";
- string objectRarPath = objectRarPathName.Substring(0, beforeObjectRarNameIndex);
- //目标目录、文件是否存在
- if (System.IO.Directory.Exists(objectRarPath) == false)
- {
- System.IO.Directory.CreateDirectory(objectRarPath);
- }
- else if (System.IO.File.Exists(objectRarPathName) == true)
- {
- System.IO.File.Delete(objectRarPathName);
- }
- //Rar压缩命令
- string commandInfo = "a " + rarNameCommand + " " + objectPathNameCommand + " -y -p" + password + " -ep1 -r -s- -rr ";
- //另起一线程执行
- commFuncs.ExecuteProcess(rarCommand, commandInfo, objectRarPath, ProcessWindowStyle.Hidden);
- CompressRarTest(rarCommand, objectRarPathName, password,ref errorinfo);
- CorrectConfusedRar(objectRarPathName,ref errorinfo);
- }
- catch (Exception ex)
- {
- //MessageBox.Show(ex.Message);
- errorinfo = ex.Message.ToString();
- return CompressResults.UnKnown;
- }
- return CompressResults.Success;
- }
- #endregion
- #region 解压
- /// <summary>
- /// 解压:将文件解压到某个文件夹中。 注意:要对路径加上双引号,避免带空格的路径,在rar命令中失效
- /// </summary>
- /// <param name="rarRunPath">rar.exe的名称及路径</param>
- /// <param name="fromRarPath">被解压的rar文件路径</param>
- /// <param name="toRarPath">解压后的文件存放路径</param>
- /// <returns></returns>
- public UnCompressResults unCompressRAR(String rarRunPath, String objectRarPathName, String objectPath, string password,ref string errorinfo)
- {
- try
- {
- bool isFileExist = File.Exists(objectRarPathName);
- if (false == isFileExist)
- {
- //MessageBox.Show("解压文件不存在!" + objectRarPathName);
- errorinfo = "解压文件不存在!" + objectRarPathName;
- return UnCompressResults.SourceObjectNotExist;
- }
- File.SetAttributes(objectRarPathName, FileAttributes.Normal); //去掉只读属性
- if (Directory.Exists(objectPath) == false)
- {
- Directory.CreateDirectory(objectPath);
- }
- String rarCommand = "\"" + rarRunPath + "\"";
- String objectPathCommand = "\"" + objectPath + "\\\"";
- String commandInfo = "x \"" + objectRarPathName + "\" " + objectPath + " -y -p" + password;
- if (string.IsNullOrEmpty(password))
- {
- commandInfo = "x \"" + objectRarPathName + "\" " + objectPath + " -y";//无密码
- }
-
- commFuncs.ExecuteProcess(rarCommand, commandInfo, objectPath, ProcessWindowStyle.Hidden);
- //MessageBox.Show("解压缩成功!" + objectRarPathName);
- return UnCompressResults.Success;
- }
- catch(Exception ex)
- {
- //MessageBox.Show("解压缩失败!" + objectRarPathName);
- errorinfo = ex.Message.ToString();
- return UnCompressResults.UnKnown;
- }
- }
- #endregion
- #region 进程
- #endregion
- #region 测试压缩文件
- /// <summary>
- /// 测试压缩后的文件是否正常。
- /// </summary>
- /// <param name="rarRunPath"></param>
- /// <param name="rarFilePathName"></param>
- public bool CompressRarTest(String rarRunPath, String rarFilePathName, string password,ref string errorinfo)
- {
- bool isOk = false;
- String commandInfo = "t -p" + password + " \"" + rarFilePathName + "\"";
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = rarRunPath;
- startInfo.Arguments = commandInfo;
- startInfo.WindowStyle = ProcessWindowStyle.Hidden;
- startInfo.UseShellExecute = false;
- startInfo.RedirectStandardOutput = true;
- startInfo.CreateNoWindow = true;
- Process process = new Process();
- process.StartInfo = startInfo;
- process.Start();
- StreamReader streamReader = process.StandardOutput;
- process.WaitForExit();
- if (streamReader.ReadToEnd().ToLower().IndexOf("error") >= 0)
- {
- //MessageBox.Show("压缩文件已损坏!");
- errorinfo = "压缩文件已损坏!";
- isOk = false;
- }
- else
- {
- //MessageBox.Show("压缩文件良好!");
- isOk = true;
- }
- process.Close();
- process.Dispose();
- return isOk;
- }
- /// <summary>
- /// 混淆Rar
- /// </summary>
- /// <param name="objectRarPathName">rar路径+名</param>
- /// <returns></returns>
- public bool ConfusedRar(string objectRarPathName,ref string errorinfo)
- {
- try
- {
- //混淆
- System.IO.FileStream fs = new FileStream(objectRarPathName, FileMode.Open);
- fs.WriteByte(0x53);
- fs.Close();
- return true;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- //MessageBox.Show("混淆Rar失败!" + ex.Message);
- return false;
- }
- }
- /// <summary>
- /// 纠正混淆的Rar
- /// </summary>
- /// <param name="objectRarPathName">rar路径+名</param>
- /// <returns></returns>
- public bool CorrectConfusedRar(string objectRarPathName,ref string errorinfo)
- {
- bool isCorrect = false;
- try
- {
- //先判断一下待解压文件是否经过混淆
- FileStream fsRar = new FileStream(objectRarPathName, FileMode.Open, FileAccess.Read);
- int b = fsRar.ReadByte();
- fsRar.Close();
- if (b != 0x52) //R:0x52 原始开始值
- {
- string strTempFile = System.IO.Path.GetTempFileName();
- File.Copy(objectRarPathName, strTempFile, true);
- File.SetAttributes(strTempFile, FileAttributes.Normal); //去掉只读属性
- FileStream fs = new FileStream(strTempFile, FileMode.Open);
- fs.WriteByte(0x52);
- fs.Close();
- System.IO.File.Delete(objectRarPathName);
- File.Copy(strTempFile, objectRarPathName, true);
- }
- isCorrect = true;
- return isCorrect;
- }
- catch(Exception ex)
- {
- //MessageBox.Show("判断待解压文件是否经过混淆时出错!" + objectRarPathName);
- errorinfo = ex.Message.ToString();
- return isCorrect;
- }
- }
- #endregion
- #region
- #endregion
- }
- }
|