首页 > 解决方案 > PowerShell - 将 zip(或任何)文件拆分为多个文件 - 脚本不起作用

问题描述

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 = "C:\OutDir\$outPrefix$chunkNum"
    $ostream = [System.IO.File]::OpenWrite($outFile)
    $ostream.Write($barr,0,$bytesRead);
    $ostream.close();
    echo "wrote $outFile"
    $chunkNum += 1
  }
}

split "C:\File_To_Split.iso" "Splits_" "10240000"

我有上面的脚本,我正在尝试开始工作。我需要将一个 1.5GB 的 zip 文件拆分为 100 多个迷你文件,然后在它们被移动后将它们重新组合在一起。

当我运行上面的脚本时,它什么也没做。显然,我正在处理事情。谁能看到我往南走的地方?

问候,-罗恩

标签: powershellfilesplit

解决方案


您需要在定义后调用split函数。

添加以下内容:

split -inFile $inFile -outPrefix "prefixGoesHere" -bufSize 4MB 

在脚本底部的新行上。更改"prefixGoesHere"4MB为您要传递的适当值。


如果您希望能够将参数传递给脚本本身,请在param脚本顶部添加一个块,然后将参数传递给split函数:

param(
  [string]$inFile = "C:\InnoWake\Server\MiningServer.zip",
  [string]$outPrefix = "",
  [int]$bufSize = 4MB
)

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
  }
}

# splatting the $PSBoundParameters variable will pass all the parameter arguments that were originally passed to the script, to the `split` function
split @PSBoundParameters

推荐阅读