C# WinRAR 创建自解压格式压缩包问题

jar_1647 2009-08-17 04:27:57
这是我在C# Winfrom里 写的一个关于制作“自解压格式”压缩包的方法:
private bool WinRAR()
{
string strtxtPath = "C:\\freezip\\free.txt";
string strzipPath = "C:\\freezip\\free.zip";
System.Diagnostics.Process Process1 =
new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;

strzipPath = "C:\\freezip\\free";//默认压缩方式为rar
Process1.StartInfo.Arguments = " a -s -sfx Setup=frees.exe "+ strzipPath + " " + strtxtPath ;
Process1.Start();
if (Process1.HasExited)
{
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
return false;
}
else
{
return false;
}
}
}

.... a -s -sfx 命令行已经可以实现“自解压格式”压缩包
但是我想在解压这个压缩包后会自动运行“frees.exe”程序
“Setup=<程序>”这个是 命令行
但是我写的“" a -s -sfx Setup=frees.exe "+ strzipPath + " " + strtxtPath ;”
是错的...请高手帮忙解决下。
...全文
576 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ximi82878 2009-08-22
  • 打赏
  • 举报
回复
貌似这样的原理是EXE文件是自己程序可识别的解压缩程序吧?
guohouchang 2009-08-22
  • 打赏
  • 举报
回复
关注
LutzMark 2009-08-21
  • 打赏
  • 举报
回复
[code=C#]public class RarShell : IDisposable
{
private MediaManager m = new MediaManager();
private Process _ExeProcess = new Process();
private Process _RarProcess = new Process();
private Thread _ExeThread;
private Thread _RarThread;
private ProcessStartInfo _startInfo = new ProcessStartInfo();
private int _inOriginalMp3Size;
private bool _isRunning = false;
private bool _isDone = false;
private string _inswfFile;
private string _inOriginalmp3File;
private string _inUsermp3File;
private string _outFile;
private string startupfile;
private static readonly string dataDir = "myData";
private string workpath = Path.Combine(Application.StartupPath, dataDir);
private readonly string _optionsExe = " a -zinfo.txt -sfx";
private readonly string _optionsRar = " a ";
private int _ExeOKLines = 0;
private int _RarOKLines = 0;

public bool IsDone
{
get { return _isDone; }
set { _isDone = value; }
}

public string OptionsRar
{
get { return _optionsRar; }
}

private string _infofile = "info.txt";


public string InSwfFile
{
get { return _inswfFile; }
set { _inswfFile = value; }
}
public string Options
{
get { return _optionsExe; }

}
public string RarPath
{
get { return _startInfo.WorkingDirectory; }
set { _startInfo.WorkingDirectory = value; }
}
public string Startupfile
{
get { return startupfile; }
set { startupfile = value; }
}


public string Infofile
{
get { return _infofile; }
set { _infofile = value; }
}



public RarShell()
{
if (_inswfFile != null)
{
InitialRarConfig();
}
DefStartInfo();
_startInfo.WorkingDirectory = workpath;
}
public RarShell(string InSwfFile, string InMp3File, string OutFile, string RarPath, string Options)
{
if (InSwfFile != string.Empty)
{
InitialRarConfig(InSwfFile);
}
DefStartInfo();
if (string.IsNullOrEmpty(RarPath))
_startInfo.WorkingDirectory = workpath;
_inswfFile = InSwfFile;
_inUsermp3File = InMp3File;
_outFile = OutFile;
_optionsExe = Options;
}
private void DefStartInfo()
{
_startInfo.FileName = "myData\\rar.exe";
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
_startInfo.RedirectStandardError = true;
_startInfo.CreateNoWindow = true;
}

private void InitProcessStartInfo()
{
string arguments = "";

_ExeProcess = new Process();
if (_optionsExe != "")
arguments = _optionsExe + " ";
if (OutFile != "") arguments += OutFile + " ";
if (InSwfFile != "")
arguments += InSwfFile + " ";
if (InOriginalMp3File != "")
{
arguments += " " + InOriginalMp3File + " ";
//得到待加入原始mp3文件的大小KB
//_inOriginalMp3Size = Convert.ToInt32(new FileInfo(Path.Combine(workpath, InOriginalMp3File)).Length / 1024);
}
if (InUserMp3File != "")
arguments += " " + InUserMp3File + " ";


_startInfo.Arguments = arguments;
_ExeProcess.StartInfo = _startInfo;
_ExeProcess.OutputDataReceived += new DataReceivedEventHandler(ExeOutputHandler);

}
public bool Start()
{
if (_isRunning || _inUsermp3File == string.Empty || _inswfFile == string.Empty || _outFile == string.Empty)
return false;
_isRunning = true;

InitProcessStartInfo();
try
{
InitialRarConfig();

_ExeThread = new Thread(new ThreadStart(BuildExe));
_ExeThread.IsBackground = true;
_ExeThread.Priority = ThreadPriority.Highest;
_ExeThread.Name = "BuildExe";

//生成exe
_ExeThread.Start();




_RarThread = new Thread(new ThreadStart(BuildRar));
_RarThread.IsBackground = true;
_RarThread.Priority = ThreadPriority.Highest;

_RarThread.Name = "BuildRar";


return true;

}
catch
{

return false;
}

}

private void ExeOutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
{


if (!String.IsNullOrEmpty(outLine.Data))
{
if (outLine.Data.ToUpper().Contains("OK"))
{
_ExeOKLines++;
}
if (outLine.Data.Contains("完成") && _ExeOKLines >= 2)
{
_RarThread.Start();
}

}
}
private void RarOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
if (outLine.Data.ToUpper().Contains("OK"))
{
_RarOKLines++;
}
if (outLine.Data.Contains("完成") && _RarOKLines >= 1)
{
_isDone = true;
_RarProcess.WaitForExit();

_RarProcess.Close();

}

}
}
private void BuildRar()
{
_RarProcess = new Process();
_startInfo.Arguments = string.Format(" {0} \"{1}\" \"{1}.exe\"", OptionsRar, OutFile);
_RarProcess.StartInfo = _startInfo;
_RarProcess.OutputDataReceived += new DataReceivedEventHandler(RarOutputHandler);
try
{
_RarProcess.Start();
//2009-06-23改为异步方式获取rar进程输出流
_RarProcess.BeginOutputReadLine();

}
catch (Exception ex)
{
if (_RarProcess != null)
{
_RarProcess.Close();
_RarProcess = null;
}

Log.WriteErLog(ex.Message);
}
finally
{

_isRunning = false;
}
}

private void BuildExe()
{


try
{

_ExeProcess.Start();
//2009-06-23改为异步方式获取rar进程输出流
_ExeProcess.BeginOutputReadLine();

}
catch (Exception ex)
{
if (_ExeProcess != null)
{
_ExeProcess.Close();
_ExeProcess = null;
}
Log.WriteErLog(ex.Message);
}
finally
{

_isRunning = false;
}
}


public void Cancel()
{
try
{
if (_ExeProcess != null)
{
_ExeProcess.Kill();
}
if (_RarProcess != null)
{
_RarProcess.Kill();
}
}
catch
{
}
}
/// <summary>
/// 初始化Rar.exe所需自解压配置文件
/// </summary>
public void InitialRarConfig()
{

InitialRarConfig(Startupfile);
}

/// <summary>
/// 初始化自解压配置文件
/// </summary>
/// <param name="startupfile">rar生成exe内自动启动文件,默认为程序自动生成的swf文件</param>
public void InitialRarConfig(string _startupfile)
{

//using (FileStream fs = new FileStream(m.UserDataPath + "\\" + _infofile, FileMode.Create))
using (FileStream fs = new FileStream(Path.Combine(m.UserDataPath, _infofile), FileMode.Create))
{

using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
sw.BaseStream.Seek(0, SeekOrigin.End);

sw.WriteLine("Setup={0}", _startupfile);
sw.WriteLine("TempMode");
sw.WriteLine("Silent=1");
sw.WriteLine("Overwrite=1");
sw.Flush();
sw.Close();
}

}

}
public void Dispose()
{

if (_RarThread.ThreadState != System.Threading.ThreadState.Running)
{ _RarThread.Abort(); _RarThread = null; }
_ExeThread = null;
if (_ExeProcess != null) _ExeProcess.Dispose();
if (_RarProcess != null)
{
_RarProcess.Close();
_RarProcess.Dispose();
}
if (_startInfo != null) _startInfo = null;
if (m != null) m.Dispose();
}
}
[/code]
LutzMark 2009-08-21
  • 打赏
  • 举报
回复
我正好前些日子写过调用Winrar自解压自运行文件的,
开始也用楼主这种方法,不可行。最后只能把自运行文件那些参数放入动态生成的txt中。
即把
Setup=frees.exe
TempMode
Silent=1
Overwrite=1

生成了一个info.txt文件
winrar.exe参数这样写"a -zinfo.txt -sfx"


laigb 2009-08-20
  • 打赏
  • 举报
回复
再用Process 调个看看吧
CopperBell 2009-08-19
  • 打赏
  • 举报
回复
帮顶
yeaicc 2009-08-18
  • 打赏
  • 举报
回复


Update

1,978

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧