Jump to: navigation, search


AFLIB (A File Library) is a small C library that facilitates reading from and writing to compressed files.

I have been using functions in the C-library (stdio.h in libc) and have gotten accustomed to using functions like 'fopen', 'fclose', 'fread', and 'fwrite'. Similarly, I have been using analogous functions in ZLIB 'gzopen', 'gzclose', 'gzread', and 'gzwrite'. I then decided to check out LIBBZIP2. LIBBZIP2 was a little bit more difficult to program since multiple streams/blocks could be found within a .bz2 file.

Anyhow, I wanted a generic library and API that I could open a file, read/write from/to the file, and close the file, all without getting bogged down in the details of each implementation. Therefore I wrote a quick C-library that hides the implementation of ZLIB and LIBBZIP2, as well as the LIBC standard IO functions.


You can get AFLIB via GIT:

git clone git:// aflib


I tried to model the functions and data-structure after that found in stdio.h. Example programs can be found in the GIT repository above. Below, I list the function from stdlib or ZLIB that was emulated.

Compression Types

No compression:


BZIP2 compression:


GZIP compression:


Open a file stream

Note: the compression variable must be specified one of the compression types above.

'fopen' (stdio.h) or 'gzopen' (zlib.h)

AFILE *AFILE_afopen(const char* path, const char *mode, int32_t compression);

'fdopen' (stdio.h) or 'gzdopen' (zlib.h)

AFILE *AFILE_afdopen(int fildes, const char *mode, int32_t compression);

Close a file stream

'fclose' (stdio.h) or 'gzclose' (zlib.h)

void AFILE_afclose(AFILE *afp);


'fread' (stdio.h)

size_t AFILE_afread(void *ptr, size_t size, size_t count, AFILE *afp);

'gzread' (zlib.h)

int AFILE_afread2(AFILE *afp, void *ptr, unsigned int len);


'fwrite' (stdio.h) or 'gzwrite' (zlib.h)

size_t AFILE_afwrite(void *ptr, size_t size, size_t count, AFILE *afp);
Personal tools