.
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.IO;
using Microsoft.Win32;
using System.Diagnostics;
namespace File_Compression
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string _compress_path = "";
string _decompress_path = "";
private void button1_Click(object sender, EventArgs e)
{
_sigle_file = false;
if (_compress_path != "")
{
if (ExistsWinRar())
{
DirectoryInfo dir = new DirectoryInfo(_compress_path);
foreach (DirectoryInfo dChild in
dir.GetDirectories("*"))
//找出資料夾內所有資料夾
{
if (ch_box_compress_day.Checked)
{
if ((DateTime.Now -dChild.LastWriteTime).Days >
Int32.Parse(txt_compress_day.Text))
{
CompressRar(dChild.FullName, _compress_path +
"\\" + dChild);
}
}
else
{
CompressRar(dChild.FullName,_compress_path +
"\\" + dChild);
}
}
}
else
{
MessageBox.Show("沒有安裝 winrar");
}
}
else
{
MessageBox.Show("未選擇要壓縮的資料夾");
}
}
public bool ExistsWinRar()
{
string result = string.Empty;
bool is_exist = false;
string key =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppPaths\WinRAR.exe";
RegistryKey registryKey =Registry.LocalMachine.OpenSubKey(key);
if (registryKey != null)
{
result = registryKey.GetValue("").ToString();
is_exist = true;
}
registryKey.Close();
return is_exist;
}
/// <summary>
/// 將格式為rar的壓縮檔案解壓到指定的目錄
/// </summary>
/// <paramname="rarFileName">要解壓rar檔案的路徑</param>
/// <param name="saveDir">解壓後要儲存到的目錄</param>
public void DeCompressRar(string rarFileName, string saveDir)
{
string regKey =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppPaths\WinRAR.exe";
RegistryKey registryKey
=Registry.LocalMachine.OpenSubKey(regKey);
string winrarPath = registryKey.GetValue("").ToString();
registryKey.Close();
string winrarDir =System.IO.Path.GetDirectoryName(winrarPath);
String commandOptions = string.Format("x{0} {1} -y",
rarFileName, saveDir);
ProcessStartInfo processStartInfo =new ProcessStartInfo();
processStartInfo.FileName =System.IO.Path.Combine(winrarDir,
"rar.exe");
processStartInfo.Arguments =commandOptions;
processStartInfo.WindowStyle =ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo =processStartInfo;
process.Start();
process.WaitForExit();
process.Close();
}
/// <summary>
/// 將目錄和檔案壓縮為rar格式並儲存到指定的目錄
/// </summary>
/// <paramname="soruceDir">要壓縮的資料夾目錄</param>
/// <paramname="rarFileName">壓縮後的rar儲存路徑</param>
public void CompressRar(string soruceDir, string rarFileName)
{
string regKey =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppPaths\WinRAR.exe";
RegistryKey registryKey
=Registry.LocalMachine.OpenSubKey(regKey);
stringwinrarPath = registryKey.GetValue("").ToString();
registryKey.Close();
string winrarDir =System.IO.Path.GetDirectoryName(winrarPath);
String commandOptions = "";
if (ch_box_compress_complete_delete_file.Checked)//
{
if (_sigle_file)
{
String[] Sp_str =soruceDir.Split(new char[] { '.' });
String[] Sp_str2 = Sp_str[0].Split(new char[] { '\\'
});
commandOptions = string.Format("a{0} {1} -ep1 -dw",
rarFileName + "\\"+ Sp_str2[Sp_str2.Length - 1],
soruceDir);
}
else
{
commandOptions = string.Format("a{0} {1} -ep1 -dw",
rarFileName, soruceDir);
}
}
else
{
if (_sigle_file)
{
String[] Sp_str =soruceDir.Split(new char[] { '.' });
String[] Sp_str2 = Sp_str[0].Split(new char[] { '\\'
});
commandOptions = string.Format("a{0} {1} -ep1",
rarFileName + "\\"+ Sp_str2[Sp_str2.Length-1],
soruceDir);
}
else
{
commandOptions = string.Format("a{0} {1} -ep1",
rarFileName, soruceDir);
}
}
ProcessStartInfo processStartInfo =new ProcessStartInfo();
processStartInfo.FileName =System.IO.Path.Combine(winrarDir,
"rar.exe");
processStartInfo.Arguments =commandOptions;
processStartInfo.WindowStyle =ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
process.Close();
}
private void btn_select_folder_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
_compress_path = path.SelectedPath;
txt_file_path.Text =_compress_path;
}
private void btn_select_compressfile_Click(object
sender, EventArgs e)
{
OpenFileDialog path = new OpenFileDialog();
path.Filter = "Text documents (.rar)|*.rar";
// Filter files by extension
path.ShowDialog();
_decompress_path = path.FileName;
txt_decompress_path.Text =_decompress_path;
}
private void btn_decompress_file_Click(object sender, EventArgs
e)
{
String[] sp_str =_decompress_path.Split(new char[]
{ '\\' });
//找到檔案跟目錄
String file_at_menu = "";
for(int i = 0 ; i < sp_str.Length-1 ; i++)
{
if (i == sp_str.Length - 2)
{
file_at_menu += sp_str[i];
}
else
{
file_at_menu += sp_str[i] +"\\";
}
}
DeCompressRar(_decompress_path,file_at_menu);
}
private void button2_Click(object sender, EventArgs e)
{
_sigle_file = false;
CompressRar(_compress_path,_compress_path);
}
bool _sigle_file = false;
private void button3_Click(object sender, EventArgs e)
{
String[] sp_str =_compress_path.Split(new char[] { '\\' });
_sigle_file = true;
//找到檔案跟目錄
String file_at_menu = "";
for (int i = 0; i < sp_str.Length - 1; i++)
{
if (i == sp_str.Length - 2)
{
file_at_menu += sp_str[i];
}
else
{
file_at_menu += sp_str[i] +"\\";
}
}
CompressRar(_compress_path,file_at_menu);
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog path = new OpenFileDialog();
path.Filter = "Text documents (.*)|*.*";
// Filter files by extension
path.ShowDialog();
_compress_path = path.FileName;
txt_file_path.Text =_compress_path;
}
}
}
實際執行畫面
資料夾中含多個資料夾個別單獨壓縮
壓縮完成後如下圖 每個資料夾都被已檔名獨立壓縮成一個檔案
解壓縮
選擇壓縮檔
執行解壓縮
單一資料夾壓縮
單一檔案壓縮