首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jenkins + PowerShell -将Zip文件转换为Bytes,传输Bytes,然后将Bytes重构为Zip

Jenkins + PowerShell -将Zip文件转换为Bytes,传输Bytes,然后将Bytes重构为Zip
EN

Stack Overflow用户
提问于 2017-09-07 13:39:51
回答 1查看 1.2K关注 0票数 0

由于这个复杂的问题现在有一个可行的解决方案,我想更新它以适应解决方案,以便其他人可以从中受益。

问题

我正在与Jenkins和PowerShell一起执行一系列操作。其中一个操作是收集所有关联日志文件,并将其合并到一个相应命名的文件夹中,以便将其与原始计算机/服务器关联,将该文件夹压缩,以某种方式将其传输回Jenkins工作区环境,将zip存储到单个主文件夹中,并将所述主文件夹转换为可在稍后时间/日期下载以进行调试的工件。然而,由于这个动作往往涉及两跳交换,我碰到了许多墙,这限制了我执行简单文件传输的能力。因此,我现在向社会人士寻求协助,希望他们有更多经验去做这类事情。

对我来说,执行许多我需要的操作的唯一方法(到目前为止)是通过一个Invoke-Command连接到目标机器;但是,我没过多久就开始碰到墙壁了。其中一堵墙是Jenkins和目标机器之间的文件传输问题。我发现,通过创建一个等于被调用的命令( ex:$returnMe = Invoke-Command {} )的对象,我能够从要存储在变量中的操作返回对象。这给了我一个可能的解决办法,就是通过会议把一个项目还给詹金斯.但现在提出的问题是:

问题

  1. 使用PowerShell,是否可以将文件夹压缩,然后将该zip文件转换为要传递的对象,然后使用对象的内容重新构建该zip文件?如果是这样的话,是怎么做的?

解决方案:

向@ArcSet发出特别的呼喊,以帮助它正常工作。

好的,所以这是非常有可能的,但是它需要一系列的关键步骤来实现。其中一件事就是这是一个四到五个步骤的过程:

1)将文件收集到目标文件夹中,并将文件夹压缩;2)将zip文件转换为便于传输的字节;3)通过一个特殊的自定义对象传递这些字节;4)解析对象,以确保多余的字节不会进入文件中。5)将字节转换回Jenkins上的zip文件

下面是说明如何实现这一目标的代码(带注释):

代码语言:javascript
复制
# Parse Through Each Server One By One
ForEach ($server in $servers) {
    # Create a Variable to Take in the Invoke-Command and Receive what is Returned
    $packet = Invoke-Command -ComputerName $server -ArgumentList $server -Credential $creds -ScriptBlock {
        # Function for Creating a Zip File
        Function Create-ZipFromFolder ([string]$Source, [string]$SaveAs) {
            Add-Type -Assembly "System.IO.Compression.FileSystem"
            [IO.Compression.ZipFile]::CreateFromDirectory($Source, $SaveAs)
        }

        # Function for Converting Zip to Bytes For File Transfer
        Function Get-BytesFromFile($Path) {
            return [System.IO.File]::ReadAllBytes($Path)
        }

        # Absorb and Maske Server IP for the Server to Use For Logging Purposes and FileNaming
        $maskedAddress = $args[0] -Replace ('.+(?=\.\d+$)', 'XX.XX.XX')

        # Construct the Path For Consolidated Log Files
        $masterLogFolderPath = ("C:\Logs\RemoteLogs\$maskedAddress")

        # Series of Code to Create Log Folder, Consolidate Files, And Delete Unnecessary Data For Cleanup

        # Establish what to Save the Zip As. You Will Want The Path Included to Prevent it From Finding Path of Least Resistance Upon Saving
        $zipFile = ($masterLogFolderPath + ".zip")

        Try {
           # Here is Where We Call Our Compression Function
           Create-ZipFromFolder -Source $masterLogFolderPath -SaveAs ZipFile

           # Next We Convert the Zip to Bytes
           [byte[]]$FileAsBytes = Get-BytesFromFile -Path $zipFile
        }
        Catch {
            Write-Error $_.Exception.Message
        }

        # Now We Return the New Object to Jenkins
        return New-Object -TypeName PSCustomObject -Property @{Response=$FileAsBytes}
    }

    # Function to Convert the Bytes Back Into a Zip File
    Function Create-FileFromBytes([byte[]]$Bytes, [string]$SaveAs) {
        # It was Discovered that Depending Upon the Environment, Extra Bytes Were Sometimes Added
        # These Next Lines Will Help to Remove Those
        For (($k = 0), ($kill = 0); $kill -lt 1; $k++) {
            If ($Bytes[$k] -gt 0) {
                $kill = 1
                # Truncate the Excess Bytes
                $newByteArray = ($Bytes)[$k..(($Bytes).Length -1)]
            }
        }

        # Reconstruct the Zip File
        [System.IO.File]::WriteAllBytes($SaveAs, $NewByteArray)
    }

    Try {
        # Call the Function to Begin Reconstruction
        Create-FileFromBytes -SaveAs "$env:Workspace\MasterLog\$maskedAddress.zip" -Bytes $packet.response
    }
    Catch {
        Write-Error $_.Exception.Message
    }

现在,这只是一个基本的骨架,没有所有沉重的东西,但这些是关键的部分,把所有的东西。在大多数情况下,遵循这个公式将返回所需的结果。我希望这对处于类似情况的其他人有所帮助。

再次感谢你们的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 14:18:16

因此,首先您需要创建一个zip文件。您需要首先使用添加类型的加载组装系统。使用CreateFromDirectory方法构建和保存zip文件。使用System.IO.File类从zip读取所有字节。此时,您可以将此数据发送到另一台计算机。最后,使用System.IO.File类将字节数组转换回文件

代码语言:javascript
复制
function Create-ZipFromFrolder([string]$Source,[string]$SaveAs){
    Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory($Source, $SaveAs)
    return $SaveAs
}

function Get-BytesFromFile([string]$File){
    return [byte[]]$ZipFileBytes = [System.IO.File]::ReadAllBytes($File)
}

function Create-FileFromBytes([byte[]]$Bytes, [string]$SaveAs){
    return [System.IO.File]::WriteAllBytes($SaveAs, $Bytes)
}

[byte[]]$FileAsBytes = Get-BytesFromFile -File (Create-ZipFromFrolder -Source "C:\ZipFolder" -SaveAs "C:\Test\test.zip")

Create-FileFromBytes -SaveAs "C:\ZipFolder\Test.zip" -Bytes $FileAsBytes
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46097904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档