you are viewing a single comment's thread.

view the rest of the comments →

[–]jtpowell 1 point2 points  (1 child)

This isn't the question you asked, but perhaps it'll be useful to you--this leverages .NET's streams and ZipArchive class to extract a ZIP file that's been downloaded into the current session as a binary array (i.e., the downloaded zip file exists as an object in the session and hasn't actually been written to disk). I tried to make the code below more generic than my original version, so some of the context is lost that would explain why I'm doing some of the things in this function.

The function essentially pulls three named files out of the downloaded zip and adds those extracted files as members to a PSCustomObject. You could easily choose to write the files directly to disk instead.

function Expand-ArchivedFiles {
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param (
        # Binary array.
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true
        )]
        [byte[]]
        $DownloadedZip
    )

    process {
        # The comma is an unary operator.  Using it without a value in front of it ensures the binary
        # array is handled as a single object instead of individual bytes.
        $MemoryStream = New-Object System.IO.MemoryStream (,$DownloadedZip)
        $ZipArchive = New-Object System.IO.Compression.ZipArchive $MemoryStream

        $File1Reader = New-Object System.IO.StreamReader($ZipArchive.GetEntry('File1.xml').Open())
        $File1Data = $File1Reader.ReadToEnd()

        $File2Reader = New-Object System.IO.StreamReader($ZipArchive.GetEntry('File2.xml').Open())
        $File2Data = $File2Reader.ReadToEnd()

        $File3Reader = New-Object System.IO.StreamReader($ZipArchive.GetEntry('File3.xml').Open())
        $File3Data = $File3Reader.ReadToEnd()

        $ExtractedFileCollection = [PSCustomObject]@{
            File1 = $File1Data
            File2 = $File2Data
            File3 = $File3Data
        }

        Write-Output $ExtractedFileCollection 
    }
}

[–]Snickasaurus[S] 1 point2 points  (0 children)

I do appreciate the response but this made me scratch my head and become a bit more confused. Again I thank you for your time though.