r/AskProgramming • u/liberianjoe • Aug 09 '26
Resources on Creating Custom File format
I would appreciate it if anyone could share articles or research papers that I can read to start building my own custom file format. I’m particularly interested in managing file data size - as well as other optimization strategies for a file format.
0
Upvotes
13
u/TheCommieDuck Aug 09 '26
This is like asking for resources on creating a recipe but not knowing what food you plan to make, but just need recipe making resources.
7
u/Lumpy-Notice8945 Aug 09 '26
What about file formats do you need to learn? A file format is a deffinition, you define something and put the bits in that defined pattern.
Compression is then mostly layered on top, so if its compression you are interested in you dont need file formats.
4
u/SufficientGas9883 Aug 09 '26
A file format is like a footprint for a building. It can be a little hut or a nuclear power plant. You need to define it based on your application needs.
3
u/BasicCrows Aug 09 '26
I think you could benefit from learning some basic C as a way to understand these sort of things. At least that was how I got my understanding of how different file formats work.
2
u/-goldenboi69- Aug 09 '26
What do you want to store/describe with your custom format? Let's start there.
2
u/Recent-Day3062 Aug 09 '26
Try building an idiotically simple on on Unix.
Getta-byte-Getta-byte-Getta-byte-byte-byte…
2
u/latkde Aug 09 '26
You have provided way to little information about the data you're trying to store, so it's difficult to give you useful resources.
However, I want to point you to a common pattern of applying compression as a layer to another data format.
Many file formats are actually just ZIP files. For example, .docx or .xlsx files are a collection of XML files in a ZIP file. Java .jar archives are a collection of .class files in a ZIP file.
Formats like .tar.gz are similar – a gzipped data stream containing a TAR archive. This is conceptually even simpler and allows for stronger compression, but you have to decompress the entire datastream to get the contents. In contrast, a ZIP file lets you extract each included file separately.
Many media formats like .mp4 use a similar container approach to ZIP: there's some outside structure that lets us navigate to compressed chunks (thus allowing us to seek forward/backwards without having to decode everything first). Often, the media codec that provides compression is configurable, so the same container format can be used for different encoding and compression algorithms.
When choosing a lossless compression algorithm, there are tons of choices on the market. They differ a lot in how expensive compression and decompression is, with compression always being the much more difficult part. Many algorithms have a parameter for tuning how much effort you want to put into compression. The gzip compression is very widely used, but it's no longer state of the art. There are many successors, but specifically zstd is worth highlighting as an extremely versatile compression algorithm. Depending on settings it is so fast that using compression for everything is faster than handling uncompressed data.
1
u/soundman32 Aug 09 '26
Follow what ms office does. Store all your files in a folder, store the data as xml with relative references to and binaries, and zip the whole thing up and change the extension.
1
u/SocksOnHands Aug 09 '26
Do you mean a binary file? Typically, you might have a "header" with information about the file, and blocks of data. In something like C, you could just dump the bytes of variables/structs/arrays to a file and the load them by reading the file back into these data structures - though you have to be mindful of struct padding to ensure consistency. Of course, pointers would no longer be valid, so it is easier when related data is referenced by index or some kind of id. If tou intend to support a broader range of machines than typical home computers, you might need to be concerned with little vs big endian byte order.
1
u/Turbulent-Impact6939 Aug 10 '26
you do not manage file data size via custom file formats, you do that via the disk filesystem. And there are already several well established file systems that already do this.
1
u/kilkil Aug 12 '26
why do you need a custom file format?
reason I ask is, I want to avoid the XY Problem if possible
-1
u/KingofGamesYami Aug 09 '26
Check out Apache Avro and Apache Parquet. There's a good chance one of those two will fit your use case, and there is no need for something fully custom.
7
u/[deleted] Aug 09 '26
[removed] — view removed comment