这篇文章介绍了C#通过cmd调用7z软件实现压缩和解压文件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
压缩文件:
public object CompressZipFile(string sourceFile, string destinationFile)
{
object resObj;
Process process = new Process();
string tempDestinationFile = "";
try
{
if (process == null)
{
process = new Process();
}
tempDestinationFile = destinationFile.ToLower().EndsWith(".zip") ? destinationFile : destinationFile + ".zip";
process.StartInfo.FileName = "cmd.exe";
string command1 = @"cd c:\progra~1\7-zip";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.WriteLine(@"c:");
process.StandardInput.WriteLine(@"cd\");
process.StandardInput.WriteLine(command1);
process.StandardInput.WriteLine(@"7Z a -tzip " + destinationFile + " " + sourceFile);
process.StandardInput.WriteLine("exit");
string output = process.StandardOutput.ReadToEnd();
if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
{
resObj = new object[] { 0, "Compress file[" + destinationFile + "] OK" };
}
else
{
resObj = new object[] { 1, "Compress File[" + destinationFile + "] Error!" };
}
}
catch (Exception ex)
{
resObj = new object[] { 1, "Compress File[" + destinationFile + "] exception:" + ex.Message };
}
return resObj;
}
解压文件:
public object decompressFile(string zipFilepath, string FileDir)
{
object resObj;
try
{
string batfiledata = @"c:\progra~1\7-zip\7z.exe x " + zipFilepath + " -o" + FileDir + " -y";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + batfiledata;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
{
resObj = new object[] { 0, "解压完成" };
}
else
{
resObj = new object[] { 1, "解压出错!" };
}
}
catch (Exception ex)
{
resObj = new object[] { 1, ex.Message };
}
return resObj;
}
到此这篇关于C#通过cmd调用7z软件实现压缩和解压文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
织梦狗教程
本文标题为:C#通过cmd调用7z软件实现压缩和解压文件


基础教程推荐
猜你喜欢
- C# 解析XML和反序列化的示例 2023-04-14
- Unity shader实现高斯模糊效果 2023-01-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#中的Linq to JSON操作详解 2023-06-08
- C#中 Json 序列化去掉null值的方法 2022-11-18