仅读文件的读写文件部分(无流)

时间:2015-10-18 10:12:54

标签: windows file activex activexobject

文件的大多数高级表示都是流。 C fopen,ActiveX Scripting.FileSystemObjectADODB.Stream - 实际上,构建在C之上的任何内容都很可能使用流表示来编辑文件。

然而,当修改大型(~4MiB)固定结构二进制文件时,将整个文件读入内存并将几乎完全相同的内容写回磁盘似乎是浪费 - 这几乎肯定会附加性能损失。看看大多数未压缩的文件系统,在我看来,没有理由为什么在不触及周围数据的情况下无法读取和写入文件的一部分。最多,块必须被重写,但通常是4KiB的量级;比大文件的整个文件要少得多。

示例:

00 01 02 03
04 05 06 07
08 09 0A 0B
0C 0D 0E 0F

可能会成为:

00 01 02 03
04 F0 F1 F2
F3 F4 F5 0B
0C 0D 0E 0F

使用现有ActiveX对象的解决方案是理想的,但是无需重新编写整个文件就可以做到这一点。

1 个答案:

答案 0 :(得分:2)

好的,这里是如何在powershell中进行练习(例如hello.ps1):

$path = "hello.txt"
$bw = New-Object System.IO.BinaryWriter([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::ReadWrite))
$bw.BaseStream.Seek(5, [System.IO.SeekOrigin]::Begin)
$bw.Write([byte] 0xF0)
$bw.Write([byte] 0xF1)
$bw.Write([byte] 0xF2)
$bw.Write([byte] 0xF3)
$bw.Write([byte] 0xF4)
$bw.Write([byte] 0xF5)
$bw.Close()

您可以从命令行测试它:

powershell -file hello.ps1

然后,您可以从HTA中调用它:

var wsh = new ActiveXObject("WScript.Shell");
wsh.Run("powershell -file hello.ps1");
相关问题