【问题标题】:C# + 7z.exe doesn't seem to work [duplicate]C# + 7z.exe 似乎不起作用[重复]
【发布时间】:2014-03-07 17:40:37
【问题描述】:
string path = @"C:\Users\<user>\Documents\Visual Studio\Projects\7ZipFile\RequiredDocs\";
ProcessStartInfo zipper = new ProcessStartInfo(@"C:\Program Files\7-Zip\7z.exe");
zipper.Arguments = string.Format("a -t7z {0}.7z {0} *.txt -mx9", path);
zipper.RedirectStandardInput = true;
zipper.UseShellExecute = false;
zipper.CreateNoWindow = true;
zipper.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(zipper);

目标:压缩“路径”中的所有 *.txt 文件并将该压缩文件保存在“路径”中,并且这些 .txt 文件在压缩后不应出现在“路径”中

当我运行代码时,似乎什么都没有发生(0 错误)...

请帮忙!

谢谢

更新:我正在使用 7Zip,并已在 Windows 上安装了 7Zip 应用程序,此代码将与 .NET 3.5 一起使用。

【问题讨论】:

  • 不设置所有这些额外的ProcessStartInfo 属性是否有效?
  • 如果您在命令提示符下键入,您正在使用的命令行是否工作?
  • @David,你在说哪些属性?如果我删除 *.txt 和 mx 参数,它不会改变任何东西。
  • 这是一种在 .NET 中而不是使用 7zip 的快速方法。 stackoverflow.com/questions/34773371/…

标签: c# 7zip


【解决方案1】:

从程序中使用 7Zip 的正常方式是调用 7za.exe(不是已安装的 7z 程序)并将 7za 包含在您的应用程序中。

This 页面有一个很好的使用教程。每次我需要以编程方式进行 zip/7zip 压缩时,效果都很好。

如果您想以纯 .NET 方式实现正常的 zip 功能(需要 .NET 4.5),也可以使用 ZipArchive

此外,如果有空格,您的路径应该用引号引起来。请注意,引号用“\”转义。 "" 也是 C# 中引号的有效转义序列:

string.Format("a -t7z \"{0}.7z\" \"{0}\" *.txt -mx9", path);

【讨论】:

  • 我在安装的 7zip 目录中没有看到 7za.exe..?
  • 您必须从 7Zip 中单独下载。这是链接:downloads.sourceforge.net/sevenzip/7za920.zip
  • @user1569220 查看我的更新,您需要在路径周围使用转义引号。
  • @user1569220,在运行删除代码之前,在新创建的进程上调用 Process.WaitForExit。
  • 第一个参数的引用似乎很快就要结束了,“文件 - 名称在这里”不应该在引号中吗?与您的第二个相同,尽管您可能对通配符扩展有疑问。另外,请尝试直接从命令行(而不是代码)运行它,并确保您知道有效命令的外观,然后与调试器中的命令进行比较。
【解决方案2】:

这是我的应用程序中的一个示例。此示例提取档案,但它向您展示了如何设置该过程。只需将命令更改为 7z 和参数即可。此示例假定您将 7za.exe 与您的应用程序一起提供。祝你好运。

        public static bool ExtractArchive(string f) {
        string tempDir = Environment.ExpandEnvironmentVariables(Configuration.ConfigParam("TEMP_DIR"));

        if (zipToolPath == null) return false;

        // Let them know what we're doing.
        Console.WriteLine("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory.");
        LogFile.LogDebug("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory '" + tempDir + "'.",
            System.IO.Path.GetFileName(f));

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        if (pid == PlatformID.Win32NT || pid == PlatformID.Win32S || pid == PlatformID.Win32Windows || pid == PlatformID.WinCE) {
            p.StartInfo.FileName = "\"" + Path.Combine(zipToolPath, zipToolName) + "\"";
            p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " \"" + f + "\"";
        } else {
            p.StartInfo.FileName = Path.Combine(zipToolPath, zipToolName);
            p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " " + f;
        }
        try {
            p.Start();
        } catch (Exception e) {
            Console.WriteLine("Failed to extract the archive '" + f + "'.");
            LogFile.LogError("Exception occurred while attempting to list files in the archive.");
            LogFile.LogExceptionAndExit(e);
        }
        string o = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        string[] ls = o.Split('\n');
        for (int i = 0; i < ls.Count(); i++) {
            string l = ls[i].TrimEnd('\r');
            if (l.StartsWith("Error")) {
                LogFile.LogError("7za: Error '" + ls[i + 1] + "'.", f);
                Console.WriteLine("Failed to extract the archive '" + f + "'.");
                return false;
            }
        }
        return true;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多