Powershell解压缩字节数组需要花费大量时间

时间:2018-06-02 07:48:05

标签: powershell compression

我将30MB压缩的UTF-8编码二进制数组传递给此PowerShell函数。它需要超过30分钟的时间来处理和崩溃powershell而不提供输出。关于如何加快速度的任何建议?

function Get-DecompressedByteArray {

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [byte[]] $byteArray = $(Throw("-byteArray is required"))
    )
    Process {
        Write-Verbose "Get-DecompressedByteArray"
        $input = New-Object System.IO.MemoryStream( , $byteArray )
        $output = New-Object System.IO.MemoryStream
        $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
        $gzipStream.CopyTo( $output )
        $gzipStream.Close()
        $input.Close()
        [byte[]] $byteOutArray = $output.ToArray()
        Write-Output $byteOutArray
    }
}

调用函数

$result=Get-DecompressedByteArray -byteArray $res.content

1 个答案:

答案 0 :(得分:1)

使用带有Add-Type的C#代码比PowerShell代码快4倍。我在这里使用5 MB大小的gzip文件进行了测试。也许您应该使用powershell脚本中的C#代码来获得更高的性能。

Add-Type -typedef @"
    using System;
    using System.IO;
    using System.IO.Compression;

    namespace myGzip
    {
        public static class Decompressor
        {
            public static byte[] Decompress(string filePath)
            {
                byte[] data = File.ReadAllBytes(filePath);

                using (MemoryStream compressedStream = new MemoryStream(data))
                {
                    using (GZipStream zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                    {
                        using (MemoryStream resultStream = new MemoryStream())
                        {
                            zipStream.CopyTo(resultStream);
                            return resultStream.ToArray();
                        }
                    }
                }
            }
        }
    }

"@

function Get-DecompressedByteArray {

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [byte[]] $byteArray = $(Throw("-byteArray is required"))
    )
    Process {
        Write-Verbose "Get-DecompressedByteArray"
        $input = New-Object System.IO.MemoryStream( , $byteArray )
        $output = New-Object System.IO.MemoryStream
        $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
        $gzipStream.CopyTo( $output )
        $gzipStream.Close()
        $input.Close()
        [byte[]] $byteOutArray = $output.ToArray()
        Write-Output $byteOutArray
    }
}

# Powershell
(Measure-Command {

    $inBytes  = [System.IO.File]::ReadAllBytes("D:\test.gz");
    $bytes    = Get-DecompressedByteArray -byteArray $inBytes


}).Milliseconds
$bytes.Length

# C#
(Measure-Command {

    $bytes = [myGzip.Decompressor]::Decompress("D:\test.gz")

}).Milliseconds
$bytes.Length