如何使用PowerShell在大型二进制文件中查找和替换?

时间:2015-09-24 10:15:54

标签: powershell replace binary

我有大于50 GB的二进制文件,其中包含一个特定的字符串,我想用等长的全空格字符串替换。我正在寻找的字符串是在文件的开头,比如在第一兆字节内。如何使用PowerShell执行此操作?

我担心[System.IO.File]::ReadAllBytes("myfile.bin")不是解决方案,因为我不想加载整个二进制文件。我想在第一兆字节内搜索和替换。

1 个答案:

答案 0 :(得分:3)

从C#采用,因此可能需要进行一些重构:

$path = "\path\to\binary\file"

$numberOfBytesToRead = 1000000

$stringToSearch = "Hello World!"
$enc = [system.Text.Encoding]::UTF8
[Byte[]]$replacementString = $enc.GetBytes("     ");

$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)

# binary reader to search for the string 
$binaryReader = New-Object System.IO.BinaryReader($fileStream)

# get the contents of the beginning of the file
[Byte[]] $byteArray = $binaryReader.ReadBytes($numberOfBytesToRead)

# look for string
$m = [Regex]::Match([Text.Encoding]::ASCII.GetString($byteArray), $stringToSearch)
if ($m.Success)
{    
    echo "Found '$stringToSearch' at position "$m.Index
}
else
{
    echo "'$stringToSearch' was not found"
}
$fileStream.Close()

# reopen to write
$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite)

$binaryWriter = New-Object System.IO.BinaryWriter($fileStream)

# set file position to location of the string
$binaryWriter.BaseStream.Position = $m.Index; 
$binaryWriter.Write($replacementString)

$fileStream.Close()