C#代码控制 zip rar 解压缩

时间:2023-03-09 15:25:43
C#代码控制 zip  rar 解压缩

首先 解压ZIP的方法:

#region 解压ZIP
/// <summary>
/// 解压ZIP
/// </summary>
/// <param name="zipFilePath">解压文件的物理地址</param>
/// <param name="savePath">存放解压后的文件物理地址</param>
/// <returns></returns>
private static bool UnZipFile(string zipFilePath, string savePath, out string Message)
{
Message = "";
bool result = true;
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
Console.WriteLine(theEntry.Name);
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > )
{
Directory.CreateDirectory(savePath + "/" + directoryName);
}
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(savePath + "/" + theEntry.Name))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
}
}
catch(Exception ex)
{
result = false;
Message = ex.Message;
}
return result;
}
#endregion

然后就是解压rar的方法:

#region 解压RAR
/// <summary>
/// 解压RAR
/// </summary>
/// <param name="destPath">解压的文件名</param>
/// <param name="savePath">解压的文件地址</param>
/// <returns></returns>
public bool UnRar(string fileRarName, string savePath, out string Message)
{
Message = "";
bool result = false;
ProcessStartInfo startinfo = new ProcessStartInfo(); ;
Process process = new Process();
string rarName = fileRarName; //将要解压缩的 .rar 文件名(包括后缀)
string path = savePath; //文件解压路径(绝对)
string rarPath = savePath; //将要解压缩的 .rar 文件的存放目录(绝对路径)
string rarexe = "WinRAR.exe"; //WinRAR安装位置 try
{
//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
string cmd = string.Format("x {0} {1} -y",
rarName,
path);
startinfo.FileName = rarexe;
startinfo.Arguments = cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 startinfo.WorkingDirectory = rarPath;
process.StartInfo = startinfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
result = true;
}
}
catch (Exception ex)
{
result = false;
Message = ex.Message;
Framework.Core.Logging.LogManager.GetCurrentClassLogger().Fatal("在线阅读电子会刊异常:" + ex.ToString());
}
finally
{
process.Dispose();
process.Close();
}
return result;
}
#endregion

然后就是一些文件夹之间的操作:

/// <summary>
/// 删除所有文件(文件夹)包括本身
/// </summary>
/// <param name="dir"></param>
public void DeleteFolderAll(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolderAll(d); //递归删除子文件夹
}
Directory.Delete(dir, true); //删除已空文件夹
}
} /// <summary>
/// 删除某个路径下的所有东西
/// </summary>
/// <param name="dirPath"></param>
public static void DeleteFolder(string dirPath)
{
if (Directory.Exists(dirPath))
{
foreach (string content in Directory.GetFileSystemEntries(dirPath))
{
if (Directory.Exists(content))
{
Directory.Delete(content, true);
}
else if (File.Exists(content))
{
File.Delete(content);
}
}
}
}

好了,结束!