简体   繁体   English

将 ZIP 文件直接嵌入到 PowerShell 脚本中,而不是单独的文件中

[英]Embed ZIP files directly within a PowerShell script, not in a separate file

I would like to include some small archives, Zip and 7z into PS1.我想包括一些小档案,Zip 和 7z 到 PS1。 How to put the archive into $string for example?例如,如何将存档放入 $string 中? Or what is the best way to include the archive into the script itself, not in separate file?或者将存档包含到脚本本身而不是单独文件中的最佳方法是什么? Thank you!谢谢!

I tried this: https://github.com/AveYo/Compressed2TXT/blob/master/Compressed%202%20TXT.bat我试过这个: https://github.com/AveYo/Compressed2TXT/blob/master/Compressed%202%20TXT.bat

First convert the ZIP file to base-64, by entering one of these commands into the console, depending on your PowerShell version:首先将 ZIP 文件转换为 base-64,方法是在控制台中输入以下命令之一,具体取决于您的 PowerShell 版本:

# PowerShell 5.1
[convert]::ToBase64String((Get-Content 'test.zip' -Encoding byte)) | 
    Set-Content test.zip.base64 -NoNewline

# PowerShell Core 7+
[convert]::ToBase64String((Get-Content 'test.zip' -AsByteStream)) |
    Set-Content test.zip.base64 -NoNewline

Copy-paste the content of the.base64 text file into your PowerShell script and assign it to variable $zipBase64将 .base64 文本文件的内容复制粘贴到您的 PowerShell 脚本中并将其分配给变量$zipBase64

# Base-64 encoded ZIP file, containing two files 'file1.txt' and 'file2.txt'
$zipBase64 = 'UEsDBAoAAAAAANZbNVblYOeeBQAAAAUAAAAJAAAAZmlsZTEudHh0ZmlsZTFQSwMECgAAAAAA4Vs1Vl8x7gcFAAAABQAAAAkAAABmaWxlMi50eHRmaWxlMlBLAQI/AAoAAAAAANZbNVblYOeeBQAAAAUAAAAJACQAAAAAAAAAICAAAAAAAABmaWxlMS50eHQKACAAAAAAAAEAGAC2cplqgy3ZAQAAAAAAAAAAAAAAAAAAAABQSwECPwAKAAAAAADhWzVWXzHuBwUAAAAFAAAACQAkAAAAAAAAACAgAAAsAAAAZmlsZTIudHh0CgAgAAAAAAABABgAPC3ydIMt2QEAAAAAAAAAAAAAAAAAAAAAUEsFBgAAAAACAAIAtgAAAFgAAAAAAA=='

# Convert the base-64 string into a byte array
$zipByteArray = [convert]::FromBase64String( $zipBase64 )

# Write the byte array into a ZIP file within the current directory
$zipPath = Join-Path $PWD.Path 'output.zip'
[IO.File]::WriteAllBytes( $zipPath, $zipByteArray )

# Extract the ZIP file into sub directory 'files' of the current directory
$outputDir = (New-Item 'files' -ItemType Directory -Force).FullName
Expand-Archive -Path $zipPath -DestinationPath $outputDir

This is straightforward code, but creating an intermediate ZIP file isn't always wanted.这是简单的代码,但并不总是需要创建中间 ZIP 文件。

If you have a PowerShell version available that isn't too old (I believe you need PowerShell 5 at a minimum), you can use the ZipArchive class to extract the files from the ZIP directly from an in-memory stream without first writing an intermediate ZIP file.如果你有一个不太旧的 PowerShell 版本(我相信你至少需要 PowerShell 5),你可以使用ZipArchive class 直接从内存 stream中提取文件 stream 而无需先编写中间件ZIP 文件。

# Create an in-memory ZipArchive from the $zipByteArray
$zipArchive = [IO.Compression.ZipArchive]::new( [IO.MemoryStream]::new( $zipByteArray ) )

# Create the output directory
$outputDir = (New-Item 'files' -ItemType Directory -Force).FullName

# For each entry in the ZIP archive
foreach( $entry in $zipArchive.Entries ) { 
    
    # Build full output path
    $fullPath = Join-Path $outputDir $entry.FullName

    if( $fullPath.EndsWith( [IO.Path]::DirectorySeparatorChar ) ) {
        # This is a directory
        $null = New-Item $fullPath -ItemType Directory -Force
    }
    else {
        # Create output file
        $fileStream = [IO.File]::OpenWrite( $fullPath )
        try {
            # Extract file from ZIP
            $entryStream = $entry.Open()
            $entryStream.CopyTo( $fileStream )
        }
        finally {
            # Make sure to always close the output file otherwise
            # you could end up with an incomplete file.
            $fileStream.Dispose()
        }
    }
}

If you only want to get the content of the files within the ZIP without extracting them to files on disk, you could read the data from the $entryStream in the above example.如果只想获取 ZIP 中的文件内容,而不是将其解压到磁盘文件中,则可以从上例中的$entryStream中读取数据。 See System.IO.Stream for available methods to read data from the stream.有关从 stream 读取数据的可用方法,请参阅System.IO.Stream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM