RarOperate.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. namespace ZipFileHelper
  7. {
  8. public class RarOperate
  9. {
  10. private CommonFunctions commFuncs = CommonFunctions.getInstance();
  11. #region 单例模式
  12. private static RarOperate uniqueInstance;
  13. private static object _lock = new object();
  14. public RarOperate() { }
  15. public static RarOperate getInstance()
  16. {
  17. if (null == uniqueInstance) //确认要实例化后再进行加锁,降低加锁的性能消耗。
  18. {
  19. lock (_lock)
  20. {
  21. if (null == uniqueInstance)
  22. {
  23. uniqueInstance = new RarOperate();
  24. }
  25. }
  26. }
  27. return uniqueInstance;
  28. }
  29. #endregion
  30. #region 压缩
  31. /// <summary>
  32. /// 使用Rar.exe压缩对象
  33. /// </summary>
  34. /// <param name="rarRunPathName">Rar.exe路径+对象名</param>
  35. /// <param name="objectPathName">被压缩对象路径+对象名</param>
  36. /// <param name="objectRarPathName">对象压缩后路径+对象名</param>
  37. /// <returns></returns>
  38. public CompressResults CompressObject(string rarRunPathName, string objectPathName, string objectRarPathName, string password,ref string errorinfo)
  39. {
  40. try
  41. {
  42. //被压缩对象是否存在
  43. int beforeObjectNameIndex = objectPathName.LastIndexOf('\\');
  44. string objectPath = objectPathName.Substring(0, beforeObjectNameIndex);
  45. //System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
  46. if (Directory.Exists(objectPathName)/*directoryInfo.Exists*/ == false && System.IO.File.Exists(objectPathName) == false)
  47. {
  48. return CompressResults.SourceObjectNotExist;
  49. }
  50. //将对应字符串转换为命令字符串
  51. string rarCommand = "\"" + rarRunPathName + "\"";
  52. string objectPathNameCommand = "\"" + objectPathName + "\"";
  53. int beforeObjectRarNameIndex = objectRarPathName.LastIndexOf('\\');
  54. int objectRarNameIndex = beforeObjectRarNameIndex + 1;
  55. string objectRarName = objectRarPathName.Substring(objectRarNameIndex);
  56. string rarNameCommand = "\"" + objectRarName + "\"";
  57. string objectRarPath = objectRarPathName.Substring(0, beforeObjectRarNameIndex);
  58. //目标目录、文件是否存在
  59. if (System.IO.Directory.Exists(objectRarPath) == false)
  60. {
  61. System.IO.Directory.CreateDirectory(objectRarPath);
  62. }
  63. else if (System.IO.File.Exists(objectRarPathName) == true)
  64. {
  65. System.IO.File.Delete(objectRarPathName);
  66. }
  67. //Rar压缩命令
  68. string commandInfo = "a " + rarNameCommand + " " + objectPathNameCommand + " -y -p" + password + " -ep1 -r -s- -rr ";
  69. //另起一线程执行
  70. commFuncs.ExecuteProcess(rarCommand, commandInfo, objectRarPath, ProcessWindowStyle.Hidden);
  71. CompressRarTest(rarCommand, objectRarPathName, password,ref errorinfo);
  72. CorrectConfusedRar(objectRarPathName,ref errorinfo);
  73. }
  74. catch (Exception ex)
  75. {
  76. //MessageBox.Show(ex.Message);
  77. errorinfo = ex.Message.ToString();
  78. return CompressResults.UnKnown;
  79. }
  80. return CompressResults.Success;
  81. }
  82. #endregion
  83. #region 解压
  84. /// <summary>
  85. /// 解压:将文件解压到某个文件夹中。 注意:要对路径加上双引号,避免带空格的路径,在rar命令中失效
  86. /// </summary>
  87. /// <param name="rarRunPath">rar.exe的名称及路径</param>
  88. /// <param name="fromRarPath">被解压的rar文件路径</param>
  89. /// <param name="toRarPath">解压后的文件存放路径</param>
  90. /// <returns></returns>
  91. public UnCompressResults unCompressRAR(String rarRunPath, String objectRarPathName, String objectPath, string password,ref string errorinfo)
  92. {
  93. try
  94. {
  95. bool isFileExist = File.Exists(objectRarPathName);
  96. if (false == isFileExist)
  97. {
  98. //MessageBox.Show("解压文件不存在!" + objectRarPathName);
  99. errorinfo = "解压文件不存在!" + objectRarPathName;
  100. return UnCompressResults.SourceObjectNotExist;
  101. }
  102. File.SetAttributes(objectRarPathName, FileAttributes.Normal); //去掉只读属性
  103. if (Directory.Exists(objectPath) == false)
  104. {
  105. Directory.CreateDirectory(objectPath);
  106. }
  107. String rarCommand = "\"" + rarRunPath + "\"";
  108. String objectPathCommand = "\"" + objectPath + "\\\"";
  109. String commandInfo = "x \"" + objectRarPathName + "\" " + objectPath + " -y -p" + password;
  110. if (string.IsNullOrEmpty(password))
  111. {
  112. commandInfo = "x \"" + objectRarPathName + "\" " + objectPath + " -y";//无密码
  113. }
  114. commFuncs.ExecuteProcess(rarCommand, commandInfo, objectPath, ProcessWindowStyle.Hidden);
  115. //MessageBox.Show("解压缩成功!" + objectRarPathName);
  116. return UnCompressResults.Success;
  117. }
  118. catch(Exception ex)
  119. {
  120. //MessageBox.Show("解压缩失败!" + objectRarPathName);
  121. errorinfo = ex.Message.ToString();
  122. return UnCompressResults.UnKnown;
  123. }
  124. }
  125. #endregion
  126. #region 进程
  127. #endregion
  128. #region 测试压缩文件
  129. /// <summary>
  130. /// 测试压缩后的文件是否正常。
  131. /// </summary>
  132. /// <param name="rarRunPath"></param>
  133. /// <param name="rarFilePathName"></param>
  134. public bool CompressRarTest(String rarRunPath, String rarFilePathName, string password,ref string errorinfo)
  135. {
  136. bool isOk = false;
  137. String commandInfo = "t -p" + password + " \"" + rarFilePathName + "\"";
  138. ProcessStartInfo startInfo = new ProcessStartInfo();
  139. startInfo.FileName = rarRunPath;
  140. startInfo.Arguments = commandInfo;
  141. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  142. startInfo.UseShellExecute = false;
  143. startInfo.RedirectStandardOutput = true;
  144. startInfo.CreateNoWindow = true;
  145. Process process = new Process();
  146. process.StartInfo = startInfo;
  147. process.Start();
  148. StreamReader streamReader = process.StandardOutput;
  149. process.WaitForExit();
  150. if (streamReader.ReadToEnd().ToLower().IndexOf("error") >= 0)
  151. {
  152. //MessageBox.Show("压缩文件已损坏!");
  153. errorinfo = "压缩文件已损坏!";
  154. isOk = false;
  155. }
  156. else
  157. {
  158. //MessageBox.Show("压缩文件良好!");
  159. isOk = true;
  160. }
  161. process.Close();
  162. process.Dispose();
  163. return isOk;
  164. }
  165. /// <summary>
  166. /// 混淆Rar
  167. /// </summary>
  168. /// <param name="objectRarPathName">rar路径+名</param>
  169. /// <returns></returns>
  170. public bool ConfusedRar(string objectRarPathName,ref string errorinfo)
  171. {
  172. try
  173. {
  174. //混淆
  175. System.IO.FileStream fs = new FileStream(objectRarPathName, FileMode.Open);
  176. fs.WriteByte(0x53);
  177. fs.Close();
  178. return true;
  179. }
  180. catch (Exception ex)
  181. {
  182. errorinfo = ex.Message.ToString();
  183. //MessageBox.Show("混淆Rar失败!" + ex.Message);
  184. return false;
  185. }
  186. }
  187. /// <summary>
  188. /// 纠正混淆的Rar
  189. /// </summary>
  190. /// <param name="objectRarPathName">rar路径+名</param>
  191. /// <returns></returns>
  192. public bool CorrectConfusedRar(string objectRarPathName,ref string errorinfo)
  193. {
  194. bool isCorrect = false;
  195. try
  196. {
  197. //先判断一下待解压文件是否经过混淆
  198. FileStream fsRar = new FileStream(objectRarPathName, FileMode.Open, FileAccess.Read);
  199. int b = fsRar.ReadByte();
  200. fsRar.Close();
  201. if (b != 0x52) //R:0x52 原始开始值
  202. {
  203. string strTempFile = System.IO.Path.GetTempFileName();
  204. File.Copy(objectRarPathName, strTempFile, true);
  205. File.SetAttributes(strTempFile, FileAttributes.Normal); //去掉只读属性
  206. FileStream fs = new FileStream(strTempFile, FileMode.Open);
  207. fs.WriteByte(0x52);
  208. fs.Close();
  209. System.IO.File.Delete(objectRarPathName);
  210. File.Copy(strTempFile, objectRarPathName, true);
  211. }
  212. isCorrect = true;
  213. return isCorrect;
  214. }
  215. catch(Exception ex)
  216. {
  217. //MessageBox.Show("判断待解压文件是否经过混淆时出错!" + objectRarPathName);
  218. errorinfo = ex.Message.ToString();
  219. return isCorrect;
  220. }
  221. }
  222. #endregion
  223. #region
  224. #endregion
  225. }
  226. }