使用winrar命令行完成文件/文件夹压缩,解压(C#实现)
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;
using System.IO.Compression;
namespace Backup
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string getname(string word)
{
// MessageBox.Show("请选择文件");
OpenFileDialog openFileDialog = new OpenFileDialog();
// openFileDialog.InitialDirectory="c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.Title = word;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return openFileDialog.FileName;
}
else
return "error";
}//获取文件路径
private string getpath()
{
//MessageBox.Show("请选择存放的路径!");
// 设置根在桌面
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
// 设置当前选择的路径
folderBrowserDialog1.SelectedPath = "C:";
// 允许在对话框中包括一个新建目录的按钮
folderBrowserDialog1.ShowNewFolderButton = true;
// 设置对话框的说明信息
folderBrowserDialog1.Description = "请选择输出目录";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
// 在此添加代码,选择的路径为 folderBrowserDialog1.SelectedPath
return folderBrowserDialog1.SelectedPath.ToString();
}
else
return "error";
}//获取存放路径
private void button1_Click(object sender, EventArgs e)
{
{
string strtxtPath = getname("");
//string strzipPath = getpath();
//string strtxtPath = "C:\\freezip\\free.txt";
//string strzipPath = "C:\\freezip\\free.zip";
System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
// Process1.StartInfo.FileName = @"F:\Program Files\HaoZip\HaoZipC.exe";
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;
//// 1
////压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)
////到c:\freezip\free.rar
//strzipPath = "C:\\freezip\\free";//默认压缩方式为 .rar
// Process1.StartInfo.Arguments = " a " + strzipPath + " " + strtxtPath + " -r ";
string strzipPath = getpath();
Directory.CreateDirectory(strzipPath);
strzipPath = "\"" + strzipPath + @"\a.rar" + "\"";
MessageBox.Show(strzipPath);
Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;
MessageBox.Show(Process1.StartInfo.Arguments.ToString());
// 2
//压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)
//到c:\freezip\free.rar
// strzipPath = "C:\\freezip\\free";//设置压缩方式为 .zip
// Process1.StartInfo.Arguments = " a -afzip " + strzipPath + " " + strtxtPath;
//// 3
////压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)
//到c:\freezip\free.zip 直接设定为free.zip
// Process1.StartInfo.Arguments = " a -r "+strzipPath+" " + strtxtPath ;
//// 4
////搬迁压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)
////到c:\freezip\free.rar 压缩后 原文件将不存在
//Process1.StartInfo.Arguments = " m " + strzipPath + " " + strtxtPath;
//// 5
////压缩c:\freezip\下的free.txt(即文件free.txt)
////到c:\freezip\free.zip 直接设定为free.zip 只有文件 而没有文件夹
//Process1.StartInfo.Arguments = " a -ep " + strzipPath + " " + strtxtPath;
//// 6
////解压缩c:\freezip\free.rar
////到 c:\freezip\
//strtxtPath = "c:\\freezip\\";
//Process1.StartInfo.Arguments = " x " + strzipPath + " " + strtxtPath;
//// 7
////加密压缩c:\freezip\free.txt(即文件夹及其下文件freezip\free.txt)
////到c:\freezip\free.zip 密码为123456 注意参数间不要空格
//Process1.StartInfo.Arguments = " a -p123456 " + strzipPath + " " + strtxtPath;
//// 8
////解压缩加密的c:\freezip\free.rar
////到 c:\freezip\ 密码为123456 注意参数间不要空格
//strtxtPath = "c:\\freezip\\";
//Process1.StartInfo.Arguments = " x -p123456 " + strzipPath + " " + strtxtPath;
//// 9
////-o+ 覆盖 已经存在的文件
//// -o- 不覆盖 已经存在的文件
//strtxtPath = "c:\\freezip\\";
//Process1.StartInfo.Arguments = " x -o+ " + strzipPath + " " + strtxtPath;
////10
//// 只从指定的zip中
//// 解压出free1.txt
//// 到指定路径下
//// 压缩包中的其他文件 不予解压
//strtxtPath = "c:\\freezip\\";
//Process1.StartInfo.Arguments = " x " + strzipPath + " " +" free1.txt" + " " + strtxtPath;
//// 11
//// 通过 -y 对所有询问回应为"是" 以便 即便发生错误 也不弹出WINRAR的窗口
//// -cl 转换文件名为小写字母
//strtxtPath = "c:\\freezip\\";
//Process1.StartInfo.Arguments = " t -y -cl " + strzipPath + " " + " free1.txt";
try
{
MessageBox.Show("dd");
Process1.Start();
}
catch
{
MessageBox.Show(e.ToString());
}
finally
{
if (Process1.HasExited)
{
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
MessageBox.Show(iExitCode.ToString() + " 正常完成");
}
else
{
MessageBox.Show(iExitCode.ToString() + " 有错完成");
}
}
}
Process1.Close();
}
}
}
}
--------------------编程问答-------------------- 分享吗,如果是,感谢一下 --------------------编程问答-------------------- 看来确实是分享的,
补充:.NET技术 , C#