English 中文(简体)
转至Gzip记忆流的密码,然后将这一档案转换到基地64,然后出口到文字档案(或瓦尔)。
原标题:Convert file to Gzip memory stream, then convert that to Base64 before exporting to text file (or var)

我如何改变下文,以便把Gzip数据与[System.Convert]一起使用:ToBase64String(,而不是一个外来文件,然后将THAT产出(b64)送交一个外部文件。

** 收到一个非常多的成员在前一个职位上提供的帮助。 它是一个答案,但我的目标被误解。

现在这确实是INFILE>Compress>OUTFILE,我希望做的是INFILE>Compress>Convert>OUTBase64

$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()

与此相反,我想利用基地64作为瓦尔的投入,并且首先从第64基地转换成一个编目。

∗ ∗∗ 我愿在以下网址上发言:INBase64>Convert>Decompress>OUTFILE

$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()

现在,使用IO,我这样做的方式没有压缩。 参看RepAllBytes, 撰稿人。

file>B64

[Convert]::ToBase64String([IO.File]::ReadAllBytes( anyfile.ext )) | Out-File  anyfile.txt"

B64>File

[IO.File]::WriteAllBytes( anyfile.ext , [Convert]::FromBase64String( anyfile.txt ))

在与B64和B64编码的卷宗相比较少时,我测试了使用gzip文档而不是原始文件的结果,因此值得使用。

最佳回答

您需要使用<条码>MemoryStream而不是<条码>。

# `$content` could also be a hardcoded string here
$content = Get-Content path	ofile.txt -Raw
$outstream = [System.IO.MemoryStream]::new()
$gzip = [System.IO.Compression.GZipStream]::new(
    $outstream,
    [System.IO.Compression.CompressionLevel]::Optimal)
$bytes = [System.Text.Encoding]::UTF8.GetBytes($content)
$gzip.Write($bytes, 0, $bytes.Length)
$gzip.Dispose()

# This is the b64 gzip string
[System.Convert]::ToBase64String($outstream.ToArray())
$outstream.Dispose()
问题回答

暂无回答




相关问题
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 ...

热门标签