Is there a way to easily and arbitrarily increase the byte size of a file without corrupting its content? For instance, by inserting dummy bytes. The file for my use case is of type png.
I am on OS X, but all solutions are welcome.
Is there a way to easily and arbitrarily increase the byte size of a file without corrupting its content? For instance, by inserting dummy bytes. The file for my use case is of type png.
I am on OS X, but all solutions are welcome.
The literal answer to your question is no, by definition. The size of a file is the length of its content. If you want to change the size, you'll need to change the content.
However, it may be possible to change the content and obtain equivalent content that has a different size. Whether this is possible depends on the file format and what you consider to be equivalent.
For example, if the file is a PNG image, and you consider two files containing PNG images to be equivalent if the displayed images are the same, you can make the file larger by adding (or lengthening) the comment field in the file. For example with ImageMagick:
convert old.png -comment 'This comment will be added to the image file, making it larger' new.png
The maximum portable comment size is 2GB (you might be able to go up to 4GB). In principle you can put multiple comments (or use other field names) but I don't know if common programs support this well.
Continuing the example of a PNG image, many programs accept files containing a valid PNG image plus extra content as if it was just the PNG image without this extra content. So many programs will still accept the image if you append trailing garbage:
cp old.png new.png
echo 'trailing garbage' >>new.png
You can use the truncate
command to easily append null bytes to reach a certain size. Just keep in mind that the resulting file is widely accepted, but technically it's invalid so it might not be accepted everywhere. (It seems to work in every program that uses libpng or a derivative, and this library is highly widespread so you have a good chance that the file will work.)
The truncate
command has a little-known option to increase the size of files by adding null bytes, which won't be visible in the content unless you're using an editor that explicitly shows these, like vim
.
The proper syntax is explained in the man page, but generally, pass the -s
option and give either a size you want the file to be like -s 10k
, or add a '+' as in -s +10k
to add that many null bytes to the file.
The added bytes will be inserted at the end. For your use case, these nulls will not be visible in the image when viewed normally.
truncate -s
only appears to insert nulls. It creates a sparse file. The nulls do not actually exist, they only appear when you try to read the file. See my answer for more.
Depending on your purpose, you can create a sparse file. For example...
dd of=sparse bs=5M seek=1 count=0
This creates a file which appears to be 5 megabytes in size, but takes 0 disk space. When read, the "extra space" will appear to be nulls.
You can make an existing file sparse with truncate -s
. For example, I've made this animated GIF appear to be 5 megabytes in size but only taking 102 kilobytes on disk.
$ ls -l pikachu-nyan.gif
-rw-r--r-- 1 schwern staff 100872 Oct 29 2019 pikachu-nyan.gif
$ truncate -s 5M pikachu-nyan.gif
$ ls -l pikachu-nyan.gif
-rw-r--r-- 1 schwern staff 5242880 Jan 9 14:32 pikachu-nyan.gif
Finder info on the file says... 5,242,880 bytes (102 KB on disk)
Whether this is valid or not depends on how you're using the file. In the example above, the animated GIF continues to work in Firefox.