我想将文件夹压缩为扩展名为.7z的文件,使用7zip。
我想知道我会怎么做,因为我不确定(这就是我要问的原因。)
这是在C#中。
链接到页面或示例代码会很有帮助。
答案 0 :(得分:1)
使用7zip压缩或解压缩文件的代码
此代码用于压缩文件夹
public void CreateZipFolder(string sourceName, string targetName)
{
// this code use for zip a folder
sourceName = @"d:\Data Files"; // folder to be zip
targetName = @"d:\Data Files.zip"; // zip name you can change
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"D:\7-Zip\7z.exe";
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
此代码用于压缩文件
public void CreateZip(string sourceName, string targetName)
{
// file name to be zip , you must provide file name with extension
sourceName = @"d:\ipmsg.log";
// targeted file , you can change file name
targetName = @"d:\ipmsg.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"D:\7-Zip\7z.exe";
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
此代码用于解压缩
public void ExtractFile(string source, string destination)
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string zPath = @"D:\7-Zip\7zG.exe";
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) { }
}
答案 1 :(得分:0)
我同意这是重复的,但我之前使用过此代码项目中的演示,它非常有帮助:
http://www.codeproject.com/Articles/27148/C-NET-Interface-for-7-Zip-Archive-DLLs
向下滚动页面以获得演示并祝你好运!
答案 2 :(得分:0)
这里fileDirPath是我的文件夹的路径,它包含我的所有文件,preferredPath是我想要的.zip文件的路径。
例如: var fileDirePath = @“C:\ Temp”; var prefferedPath = @“C:\ Output \ results.zip”;
private void CreateZipFile(string fileDirPath, string prefferedPath)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"C:\Program Files\7-Zip\7z.exe";
p.Arguments = "a \"" + prefferedPath + "\" \"" + fileDirPath + "\"";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
return;
}