DEV Community

Cover image for I build a simple file archiver in C, SAR
Iván Naranjo Ortega
Iván Naranjo Ortega

Posted on

I build a simple file archiver in C, SAR

I wanted to sharpen my file management skills in C, not just reading and writing, but also creating, seeking, metadata preservation and compression/decompression with zlib. So I built SAR (Simple ARchiver), a command-line tool to pack and unpack files and directories to a single archive, with optional gzip compression.

It's heavily inspired by tar, but stripped down to the essentials. Just a clean binary and a list of focused actions to perform in this archives, compressed or not.

The format

The archive, .sar format, is just a binary file which consists of a secuence of blocks:

[ FileHeader | raw bytes ][ FileHeader | raw bytes ] ...

Each FileHeader is a fixed-size C struct that stores the filename, file size, permissions and modification timestamp. To read the archive you just walk it linearly: read header, skip forward by file size bytes and repeat until EOF.

Compression

When using pz, SAR compresses the entire archive using zlib's deflate into a standard gzip envelope, the same format produce by gzip tool. This means you can actually decompress SAR compressed files, .sgz or .sar.gz, with gunzip if desired.

Compression if applied to the whole SAR archive after packing, not per file. This gives better compression ratios.

For all other actions, SAR detects whether the archive is compressed or not.

Dependencies

The only dependency is zlib.

Final thoughts

This project is on Github, feedback is very welcome! 🐦.

Happy to answer questions about the implementation, this was a great exercise in low-level file management and I am starting to use it as an real alternative to tar myself.

Top comments (0)