English 中文(简体)
增加压缩到B64改为权力分配。 转换工程罚款一米,不能确定如何增加gzip或类似压缩
原标题:Add compression to B64 conversion for powershell. The conversion works fine I m not sure how to approach adding gzip or similar compression

然后,我可以将档案转换到B64,然后回来,我想压缩数据,然后再转换到B64。 如果我能够在转换过程中找到某个地方的话,那么Gzip 确实必须使用,任何类型的压缩或缩小规模都是罚款,我想压缩是答案,因为B64在储存时使用的空间超过了原始档案。

<>可见> 档案——和; B64 + B64 -> 档案(在无经济档案的情况下工作罚款)

Goal: File->Compression->B64 + B64->Decompression->File (With no temp files)

非压缩工作实例

FileToB64Text.ps1

[Convert]::ToBase64String([IO.File]::ReadAllBytes( test.pdf )) | Out-File  testpdf.txt"

B64TextToFile.ps1

$file = Get-Content  testpdf.txt 
[IO.File]::WriteAllBytes( test2.pdf , [Convert]::FromBase64String( $file ))

I stumbled across another way to do this with compression, which seems great since B64 conversion increases filesize, but the example is using TextToB64/B64ToText instead of FilesToB64/B64ToFiles. A way to use files as an initial input and final output AND having the compression is what I am trying to achieve.

<>CompressAndEncode.ps1

$s = Get-Content  DecodedText.txt 
$ms = New-Object System.IO.MemoryStream
$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Compress)
$sw = New-Object System.IO.StreamWriter($cs)
$sw.Write($s)
$sw.Close();
$s = [System.Convert]::ToBase64String($ms.ToArray()) | Out-File  b64output.txt 

DecodeAndDecompress.ps1

$encodedstring = Get-Content  b64output.txt 
$data = [System.Convert]::FromBase64String("$encodedstring")
$ms = New-Object System.IO.MemoryStream
$ms.Write($data, 0, $data.Length)
$ms.Seek(0,0) | Out-Null
$sr = New-Object System.IO.StreamReader(New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress))
$sr.ReadToEnd() | Out-File  DecodedText.txt
最佳回答

主要想法是,如果你把tes子改成像64,那么就没有必要这样做,只会增加档案规模,因为b64主要用于信息运输。

  • If you want to Gzip compress to a file:
$infile = Get-Item path	omyfiletocompress.ext
$instream = $infile.OpenRead()
$outstream = (New-Item path	omygzfile.gz).OpenWrite()
$gzip = [System.IO.Compression.GZipStream]::new(
    $outstream,
    [System.IO.Compression.CompressionLevel]::Optimal)
$instream.CopyTo($gzip)

$gzip.Dispose()
$outstream.Dispose()
$instream.Dispose()
  • If you want to expand a Gzip file:
$infile = Get-Item path	omygzfile.gz
$instream = $infile.OpenRead()
$outstream = (New-Item path	omyexpandedgzfile.ext -Force).OpenWrite()
$gzip = [System.IO.Compression.GZipStream]::new(
    $instream,
    [System.IO.Compression.CompressionMode]::Decompress)
$gzip.CopyTo($outstream)

$gzip.Dispose()
$outstream.Dispose()
$instream.Dispose()
  • If you want to read the Gzip file content:
$infile = Get-Item mygzfile.gz
$instream = $infile.OpenRead()
$gzip = [System.IO.Compression.GZipStream]::new(
    $instream,
    [System.IO.Compression.CompressionMode]::Decompress)
$reader = [System.IO.StreamReader] $gzip
$reader.ReadToEnd()

$gzip.Dispose()
$reader.Dispose()
$instream.Dispose()
  • If you have a gzip b64 string in a file and you want to read its content:
$instream = [System.IO.MemoryStream]::new(
    [System.Convert]::FromBase64String((Get-Content .mygzfile.gz -Raw)))
$gzip = [System.IO.Compression.GZipStream]::new(
    $instream,
    [System.IO.Compression.CompressionMode]::Decompress)
$outstream = [System.IO.MemoryStream]::new()
$gzip.CopyTo($outstream)

$gzip.Dispose()
$instream.Dispose()

$outstream.Seek(0, [System.IO.SeekOrigin]::Begin)
$reader = [System.IO.StreamReader] $outstream
$reader.ReadToEnd()

$reader.Dispose()
$outstream.Dispose()

如果你想简化这一进程,请PSCompression。 a 审判:

# compression
Compress-GZipArchive path	ofiletocompress path	odestination.gz

# expansion to the success stream
Expand-GzipArchive path	ocompressed.gz

# expansion to a file
Expand-GzipArchive path	ocompressed.gz path	oexpanded

# expansion of gzip b64 string
Get-Content path	ocompressed.gz | ConvertFrom-GzipString
问题回答

暂无回答




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签