问题描述
我想在图像元数据块的中间添加一些字符串.在一些特定的标记下.我必须在字节级别执行此操作,因为.NET不支持自定义元数据字段.
I want to add some string in the middle of image metadata block. Under some specific marker. I have to do it on bytes level since .NET has no support for custom metadata fields.
该块的构建方式类似于1C 02 XX YY YY ZZ ZZ ZZ ...
,其中XX是我需要附加的字段的ID,而YY YY是其大小,ZZ =数据.
The block is built like 1C 02 XX YY YY ZZ ZZ ZZ ...
where XX is the ID of the field I need to append and YY YY is the size of it, ZZ = data.
我认为应该或多或少地读取直到此标记的所有图像数据(1C 02 XX),然后增加大小字节(YY YY),在ZZ的末尾添加数据,然后添加其余的原始文件?这样对吗?
I imagine it should be more or less possible to read all the image data up to this marker (1C 02 XX) then increase the size bytes (YY YY), add data at the end of ZZ and then add the rest of the original file? Is this correct?
我应该如何继续呢?它需要尽可能快地处理4-5 MB JPEG文件.
How should I go on with it? It needs to work as fast as possible with 4-5 MB JPEG files.
推荐答案
通常无法加快此操作的速度.您必须至少读取需要移动的部分,然后将其重新写入更新的文件中.如果可以并行进行读写操作,则创建新文件并将内容复制到其中的速度可能会更快.
In general there is no way to speed up this operation. You have to read at least portion that needs to be moved and write it again in updated file. Creating new file and copying content to it may be faster if you can parallelize read and write operations.
注意:在您的特定情况下,可能无法仅在文件中间插入内容,因为大多数文件格式在设计时都没有考虑到此类修改.通常,文件的某些部分会有偏移,当您移动文件的一部分时,这些偏移将是无效的.指定您尝试使用的文件格式可以帮助其他人提供更好的方法.
Note: In you particular case it may not be possible to just insert content in the middle of the file as most of file formats are not designed with such modifcations in mind. Often there are offsets to portions of the file that will be invalid when you shift part of the file. Specifying what file format you trying to work with may help other people to provide better approaches.
这篇关于在二进制文件中间插入字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!