Powershell中快速简单的二进制连接文件 [英] Fast and simple binary concatenate files in Powershell

查看:31
本文介绍了Powershell中快速简单的二进制连接文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Powershell 连接二进制文件的最佳方法是什么?我更喜欢简单易记且快速执行的单行代码.

What's the best way of concatenating binary files using Powershell? I'd prefer a one-liner that simple to remember and fast to execute.

我想到的最好的是:

gc -Encoding Byte -Path ".\File1.bin",".\File2.bin" | sc -Encoding Byte new.bin

这似乎工作正常,但对于大文件来说非常慢.

This seems to work ok, but is terribly slow with large files.

推荐答案

您采用的方法是我在 PowerShell 中采用的方法.但是,您应该使用 -ReadCount 参数来提高性能.您还可以利用位置参数进一步缩短此时间:

The approach you're taking is the way I would do it in PowerShell. However you should use the -ReadCount parameter to improve perf. You can also take advantage of positional parameters to shorten this even further:

gc File1.bin,File2.bin -Encoding Byte -Read 512 | sc new.bin -Encoding Byte

编者注:在跨平台PowerShell (Core) 版本(版本 6 及更高版本),现在必须使用 -AsByteStream 而不是 -Encoding Byte;此外,sc 别名Set-Content cmdlet 已被删除.

Editor's note: In the cross-platform PowerShell (Core) edition (version 6 and up), -AsByteStream must now be used instead of -Encoding Byte; also, the sc alias for the Set-Content cmdlet has been removed.

关于 -ReadCount 参数的使用,我不久前写了一篇关于这个的博客文章,人们可能会觉得有用 - 优化获取大文件内容的性能.

Regarding the use of the -ReadCount parameter, I did a blog post on this a while ago that folks might find useful - Optimizing Performance of Get Content for Large Files.

这篇关于Powershell中快速简单的二进制连接文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆