开发者

Is there a simple way to trim a file?

开发者 https://www.devze.com 2023-04-10 03:22 出处:网络
I have a large file (typically more than 1GB) with following format: [header information, 128 bytes] [content, 1GB]

I have a large file (typically more than 1GB) with following format:

[header information, 128 bytes] [content, 1GB]

My question: Is there a simple way to trim the header information without have to read the content part and save to disk again? I mean I just need to modifying the existing file and delete the header part.

Update:

The header is at the beginning of 开发者_运维问答the file


If the header is at the beginning of the file then it is simply impossible to remove that header without completely re-writing the entire file. That's simply the nature of file systems.

The obvious solution to the conundrum is to put the header right at the end of the file. It's a trivial operation to strip off the end of a file. Another option would be to put the header in a separate file or even an alternate file stream.


From the perspective of the file system, a file content is just a sequence of blocks. While file system normally allows some "slack" (i.e. unused space) in the last block (to accommodate a file size that is not multiple of block size), the file system is simply not constructed to allow any slack in the first block. In other words, the file content must begin at the beginning of the first block.

So you cannot trim arbitrary amount of data from the start of the file. Even trimming the amount of data equal to the block size is not exposed through any API I know of.


Short answer: No, there isn't. Every bit of the contents has to get moved.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号