gpt4 book ai didi

mysql - 内存优化的 powershell Out-File - 为大文件编码没有 BOM 的 utf8

转载 作者:行者123 更新时间:2023-11-29 01:03:25 24 4
gpt4 key购买 nike

所以我在 powershell 中运行一个外部命令,将 mysqldump.exe 输出通过管道传输到 .sql 文件中。

& "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" @arguments | Out-File -Encoding utf8 $backupFilePath\$database.sql

首先,文件以 UCS2 编码输出。我设法计算出您可以将 Out-File 命令的编码设置为 -Encoding utf8。但它带有字节顺序标记。有什么方法可以明确指定您不需要字节顺序标记吗?

我尝试使用 WriteAllLines 转换文件,但是这个数据库 .sql 文件输出的大小为 3GB,它会占用内存。

有什么想法吗?

最佳答案

Function Out-FileUtf8NoBom {

[CmdletBinding()]
param(
[Parameter(Mandatory, Position=0)] [string] $LiteralPath,
[switch] $Append,
[switch] $NoClobber,
[AllowNull()] [int] $Width,
[Parameter(ValueFromPipeline)] $InputObject
)

[Environment]::CurrentDirectory = $PWD
$LiteralPath = [IO.Path]::GetFullPath($LiteralPath)

if ($NoClobber -and (Test-Path $LiteralPath)) {
Throw [IO.IOException] "The file '$LiteralPath' already exists."
}

$sw = New-Object IO.StreamWriter $LiteralPath, $Append

$htOutStringArgs = @{}
if ($Width) {
$htOutStringArgs += @{ Width = $Width }
}

try {
$Input | Out-String -Stream @htOutStringArgs | % { $sw.WriteLine($_) }
} finally {
$sw.Dispose()
}

}


Function FixEncoding([string] $path)
{
[IO.SearchOption] $option = [IO.SearchOption]::AllDirectories;
[String[]] $files = [IO.Directory]::GetFiles((Get-Item $path).FullName, '*.*', $option);
foreach($file in $files)
{
"Converting $file...";
Get-Content $file | Out-FileUtf8NoBom $file
}
}

您可以将其用作FixEncoding("C:\path\to\files\")

关于mysql - 内存优化的 powershell Out-File - 为大文件编码没有 BOM 的 utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com