我想使用7zip将文件夹压缩成.7z格式的文件。请问如何操作?因为我不确定,所以需要询问。
这是在C#中进行的操作。
提供页面链接或示例代码会很有帮助。
这是在C#中进行的操作。
提供页面链接或示例代码会很有帮助。
使用7zip压缩或解压文件的代码
此代码用于将文件夹压缩成zip文件。
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) { }
}
这里的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;
}
我同意这是一个重复的问题,但我以前使用过来自这个codeproject的演示程序,它非常有帮助:
http://www.codeproject.com/Articles/27148/C-NET-Interface-for-7-Zip-Archive-DLLs
向下滚动页面以查看演示,祝你好运!
Process.Start
(这需要安装7zip)。否则,请参阅LZMA SDK。 - user703016