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 压缩 /// /// 使用Rar.exe压缩对象 /// /// Rar.exe路径+对象名 /// 被压缩对象路径+对象名 /// 对象压缩后路径+对象名 /// 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 解压 /// /// 解压:将文件解压到某个文件夹中。 注意:要对路径加上双引号,避免带空格的路径,在rar命令中失效 /// /// rar.exe的名称及路径 /// 被解压的rar文件路径 /// 解压后的文件存放路径 /// 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 测试压缩文件 /// /// 测试压缩后的文件是否正常。 /// /// /// 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; } /// /// 混淆Rar /// /// rar路径+名 /// 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; } } /// /// 纠正混淆的Rar /// /// rar路径+名 /// 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 } }