【问题标题】:Powershell read file in chunksPowershell分块读取文件
【发布时间】:2017-01-13 16:40:16
【问题描述】:

我有一个用 Powershell 编写的脚本,它通过 FTP 传输文件,使用以下方法可以正常工作:

$content = [System.IO.File]::ReadAllBytes($backup_app_data)

但是一旦文件大小达到 2Gb,它就会停止工作,并抛出一个错误,指出此方法仅限于读取不超过此大小的文件。

使用“1”参数调用“ReadAllBytes”的异常:“文件是 太长。此操作目前仅限于支持文件较少 大小超过 2 GB。

现在,我正在尝试修改我的脚本并使用 Read 的替代方法,据我了解,这将允许我分块读取文件,然后写入这些块等等。

不幸的是,虽然我修改的脚本似乎可以工作,但由于在我的 FTP 位置上创建了一个文件,因此没有数据正在传输,脚本也没​​有引发错误。

在目标文件夹中只创建了一个 0kb 的文件,脚本结束。

我尝试进行一些调试,但似乎找不到问题所在。以下是我目前正在使用的代码。

$file = 'G:\Backups\DATA_backup_2016_09_05.zip'
$dest_name = 'DATA_backup_2016_09_05.zip'

$ftp = [System.Net.FtpWebRequest]::Create($ftp+$dest_name)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($user, $pass)

$ftp.UseBinary = $true
$ftp.UsePassive = $true

# determine the size of the file
$file_size = (Get-Item $file).length
$chunk_size = 512mb

$bytes_to_read = $file_size
$iterations = $file_size / $chunk_size
$iter = 0

$fstream = [System.IO.FileStream]
[byte[]] $byte_array 

while ($bytes_to_read > 0){
    if($iterations > 1) {
        $content = $fstream.Read($byte_array, $iter, $chunk_size)
        }
    else {
        $content = $fstream.Read($byte_array, $iter, $bytes_to_read)
    }

    $ftp.ContentLength = $content.Length

    $rs = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)

    # keep the loop going
    $iter = $iter + 1
    $iterations = $iterations - 1
    $bytes_to_read = $bytes_to_read - $chunk_size
}

$rs.Close()
$rs.Dispose()

感谢任何帮助。

【问题讨论】:

  • Upload BIG files via HTTP 的可能重复项 - 虽然对于 HTTP,它使用相同的 API、WebRequest 和流,因此来自 answer by the @vvchik 的循环也可以用于 FTP。
  • 我怀疑你需要摆脱你的 iterations 变量,因为一般来说你无法知道你需要多少次读取,因为你赢了'不一定会得到尽可能多的字节数。
  • 你试过Get-Content -Raw吗?
  • 请记住,PowerShell 使用 -gt 而不是 >...
  • 感谢大家的投入,我目前正在根据其他问题的建议解决方案编写脚本版本。我会尽快发布我的发现并希望有一个工作脚本。

标签: powershell ftp


【解决方案1】:

因此,根据评论部分中的建议,并使用可能重复的the answer 中的示例question,我自己设法找到了解决方案。

下面是我用来通过 FTP 传输 3.74Gb 的 .zip 存档的代码。

$ftp_addr = "ftp://ftp.somerandomwebsite.com/folder1/"
$user = "usr"
$pass = "passwrd"
$bufSize = 256mb

# ... missing code where I identify the file I want to FTP ... #

# Initialize connection to FTP
$ftp = [System.Net.FtpWebRequest]::Create($ftp_addr + $backup_file + ".zip")
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($user, $pass)

$ftp.Timeout = -1              #infinite timeout
$ftp.ReadWriteTimeout = -1     #infinite timeout

$ftp.UseBinary = $true
$ftp.UsePassive = $true

$requestStream = $ftp.GetRequestStream()
$fileStream = [System.IO.File]::OpenRead($file_to_ftp)
$chunk = New-Object byte[] $bufSize

while ( $bytesRead = $fileStream.Read($chunk, 0, $bufSize) ){
    $requestStream.write($chunk, 0, $bytesRead)
    $requestStream.Flush()
}

$fileStream.Close()
$requestStream.Close()

到目前为止,这段代码已经完美运行了大约多次(20 多次)。我希望它可以帮助其他人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    相关资源
    最近更新 更多