【发布时间】:2014-07-11 06:50:41
【问题描述】:
我已经在其他地方看到了文本文件的答案,但我需要为压缩文件执行此操作。
我有一个 6G 的二进制文件,需要分成 100M 个块。我是否在某处错过了 unix“头”的模拟?
【问题讨论】:
标签: powershell
我已经在其他地方看到了文本文件的答案,但我需要为压缩文件执行此操作。
我有一个 6G 的二进制文件,需要分成 100M 个块。我是否在某处错过了 unix“头”的模拟?
【问题讨论】:
标签: powershell
没关系。给你:
function split($inFile, $outPrefix, [Int32] $bufSize){
$stream = [System.IO.File]::OpenRead($inFile)
$chunkNum = 1
$barr = New-Object byte[] $bufSize
while( $bytesRead = $stream.Read($barr,0,$bufsize)){
$outFile = "$outPrefix$chunkNum"
$ostream = [System.IO.File]::OpenWrite($outFile)
$ostream.Write($barr,0,$bytesRead);
$ostream.close();
echo "wrote $outFile"
$chunkNum += 1
}
}
假设:bufSize 适合内存。
【讨论】:
$stream.seek? Read 方法会自动设置当前位置,对吧?
推论问题的答案:如何将它们重新组合在一起?
function stitch($infilePrefix, $outFile) {
$ostream = [System.Io.File]::OpenWrite($outFile)
$chunkNum = 1
$infileName = "$infilePrefix$chunkNum"
$offset = 0
while(Test-Path $infileName) {
$bytes = [System.IO.File]::ReadAllBytes($infileName)
$ostream.Write($bytes, 0, $bytes.Count)
Write-Host "read $infileName"
$chunkNum += 1
$infileName = "$infilePrefix$chunkNum"
}
$ostream.close();
}
【讨论】:
我回答了 bernd_k 在这个问题的 cmets 中提到的问题,但在这种情况下我会使用 -ReadCount 而不是 -TotalCount,例如
Get-Content bigfile.bin -ReadCount 100MB -Encoding byte
这会导致Get-Content 在块大小为文本编码的行或字节编码的字节时读取文件的块。请记住,当它执行此操作时,您会得到一个沿管道传递的数组,而不是单个字节或文本行。
【讨论】: