一:使用DotNetZip(文件名含有中文,出现乱码,暂无解决)
.dll文件下载http://www.codeplex.com/DotNetZip/Release/ProjectReleases.aspx?ReleaseId=12712
Code
public void CompressWholeFolder( string FromDirectory, string ToDirectory, string ZipName )
{
if( Directory.Exists( FromDirectory ) )
{
using( ZipFile zip = new ZipFile( ToDirectory + "\\" + ZipName ))
{
DirectoryInfo directInfo = new DirectoryInfo( FromDirectory );
FileSystemInfo[] FilesInfo = directInfo.GetFileSystemInfos();
//zip.Password = "123";//压缩密码
zip.Comment = "备份文件!";//压缩文件的信息说明
foreach( FileSystemInfo file in FilesInfo )
{
if( file is FileInfo )
{
zip.AddFile( ( file.FullName.ToString()) );//添加文件
}
if( file is DirectoryInfo )
{
zip.AddDirectory( file.FullName );//添加文件夹
}
}
zip.Save();
}
}
}
二:调用winrar压缩
Code
using Microsoft.Win32
using System.Diagnostics;
/// <summary>
/// 调用winrar压缩
/// </summary>
/// <param name="patch">被压缩文件路径</param>
/// <param name="rarPatch">压缩文件存放路径</param>
/// <param name="rarName">设置要压缩的rar名称</param>
public static bool DoRAR( string patch, string rarPatch, string rarName )
{
String WinRAR;
RegistryKey Reg;
Object Obj;
String CmdCommand;
ProcessStartInfo ProStart;
Process Pro;
try
{
Reg = Registry.ClassesRoot.OpenSubKey( "Applications\\WinRAR.exe\\Shell\\Open\\Command" );
Obj = Reg.GetValue( "" );
WinRAR = Obj.ToString();
Reg.Close();
WinRAR = WinRAR.Substring( 1, WinRAR.Length - 7 );
//命令参数
// -ao 之压缩具有存档属性的文件
// -df 压缩后删除源文件
// -ed 压缩时不添加空目录
// -hp 加密数据及头文件
// -p 压缩密码
// -ep 压缩文件去掉文件夹中路径,使所有文件压缩在一个文件夹下
// -ep1 设置将所有 tmp 文件夹内的全部文件和文件夹加入到压缩文件 test,但是在压缩的路径名不包含「tmp\」WinRAR a -r -ep1 test tmp\*
// -ep2 当压缩时存储完整的文件路径 (除了驱动器号与前缀的反斜线)
// 开关 -SFX[名称] - 创建自解压文件
// 开关 -V<n>[k|b|f|m|M|g|G] - 创建分卷 创建 1200000 字节大小的固实,分卷自解压文件 WinRAR a -v1200 -s -sfx bitmaps
CmdCommand = "a -r -hpmy291217 -df -ep1 " + "\"" + rarPatch + rarName + "\"" + " " + "\"" + patch + "\"" + " " + "\"" + patch + "\"";
ProStart = new ProcessStartInfo();
ProStart.FileName = WinRAR;
ProStart.Arguments = CmdCommand;
ProStart.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录
ProStart.WorkingDirectory = rarPatch;
Pro = new Process();
Pro.StartInfo = ProStart;
Pro.Start();
Pro.WaitForExit();
if( Pro.HasExited )
{
Pro.Close();
return true;
}
else
{
Pro.Dispose();
return false;
}
}
catch( Exception ex )
{
throw ex;
}
}
/// <summary>
/// 调用winrar解压缩
/// </summary>
/// <param name="unRarPatch">解压缩文件夹路径</param>
/// <param name="rarPatch">被解压缩文件的路径</param>
/// <param name="rarName">rar压缩文件命名</param>
/// <returns></returns>
public static bool UnDoRAR( string unRarPatch, string rarPatch, string rarName )
{
String WinRAR;
RegistryKey Reg;
Object Obj;
String CmdCommand;
ProcessStartInfo ProStart;
Process Pro;
try
{
Reg = Registry.ClassesRoot.OpenSubKey( "Applications\\WinRAR.exe\\Shell\\Open\\Command" );
Obj = Reg.GetValue( "" );
WinRAR = Obj.ToString();
Reg.Close();
WinRAR = WinRAR.Substring( 1, WinRAR.Length - 7 );
// e 命令解压缩所有文件到同一个文件夹下
// x 命令以完整路径解压缩
// -af 指定压缩类型 zip or rar 用法 -afzip
// -o+ 覆盖现存的文件
// -o- 不覆盖现存的文件
// -ad 附加压缩文件名到目标路径中
//CmdCommand = "x " + "\"" + rarPatch + "\\" + rarName + "\"" + " " + "\"" + unRarPatch + "\"" + " -y";
CmdCommand = "e -hpmy291217 " + "\"" + rarPatch + rarName + "\"" + " " + "\"" + unRarPatch + "\"" + " -y";
ProStart = new ProcessStartInfo();
ProStart.FileName = WinRAR;
ProStart.Arguments = CmdCommand;
ProStart.WindowStyle = ProcessWindowStyle.Hidden;
ProStart.WorkingDirectory = "\"" + rarPatch + "\"";//获取压缩包路径
Pro = new Process();
Pro.StartInfo = ProStart;
Pro.Start();
Pro.WaitForExit();
if( Pro.HasExited )
{
Pro.Close();
return true;
}
else
{
Pro.Dispose();
return false;
}
}
catch( Exception ex )
{
throw ex;
}
}