使用ZipArchive类添加文件时,不会调用NuGet - install.ps1

时间:2017-11-03 13:32:43

标签: c# visual-studio zip nuget nuget-package

我想通过软件将tools\Install.ps1添加到我的nuget包中。这已经使用以下代码:

Zipper.AddItem(pathToNuGetFile, pathToToolsFolder);

当我安装nuget包时,只会跳过Install.ps1脚本而不会调用它。但它存在!它也在packages文件夹中解压缩。

包文件夹(在解决方案中)

packages

工具(子文件夹)

install

当我打开 * .nupkg 文件(在NuGet-Sources文件夹中,并在packages文件夹中不是)并将Install.ps1重命名为{{1 ,将其重命名为Install.ps并保存" zip" / Install.ps1文件,调用Install.ps1脚本!为什么呢?!

拉链类

*.nupkg

结果

preview

tools

1 个答案:

答案 0 :(得分:1)

Microsoft的ZipArchive类似乎存在问题。当我使用7zip添加我的文件时,一切都按预期工作!

我已在解决方案中实施了7zip并更改了AddItem方法:

/// <summary>
/// is adding an item to an existing Archive
/// </summary>
/// <param name="pathZipFile"></param>
/// <param name="pathItem"></param>
public static void AddItem(string pathZipFile, string pathItem)
{
    string appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "7za.exe");
    if (SystemInformation.Is64BitOperatingSystem())
    {
        appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "x64", "7za.exe");
    }

    //
    // Setup the process with the ProcessStartInfo class.
    //
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = appPath;
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    start.Arguments = $"a -r {pathZipFile} {pathItem}";

    //
    // Start the process.
    //
    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        if(process == null)
            throw new Exception("Failed to start the 7zip Process!");

        using (StreamReader reader = process.StandardOutput)
        {
            string result = reader.ReadToEnd();
            Console.Write(result);
        }

        process.WaitForExit();
        if (process.ExitCode != 0)
            throw new Exception($"Fehlercode von 7zip: {process.ExitCode}");
    }
}

希望这有助于其他人。