在C#中如何使用7Zip压缩文件夹?

5
我想使用7zip将文件夹压缩成.7z格式的文件。请问如何操作?因为我不确定,所以需要询问。
这是在C#中进行的操作。
提供页面链接或示例代码会很有帮助。

1
可能是这个问题的重复,https://dev59.com/JGsz5IYBdhLWcg3w0bOl - Aung Kaung Hein
对于简单的解决方案,请使用 Process.Start(这需要安装7zip)。否则,请参阅LZMA SDK - user703016
我只想将它简单地压缩成.7z文件,仅此而已。 - user3026440
3个回答

3

使用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) { }
        }

image is used to know files in 7zip folder


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;
    }

0

如果是重复的话我很抱歉。搜索功能好像没有工作。不过,我会检查一下的。 - user3026440

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接