Friday, February 24, 2012

[ANSI C] How to extract file from TAR archive

Introduction

tarUnpackFile() ANSI C function extracts file from a .TAR archive.

Source code

https://github.com/vurdalakov/codeblog_examples/tree/master/ansi_c/tarunpack

Usage


#include "tarunpack.h"

...

    packedFileHandle = fopen(packedFileName, "rb");

    if (NULL == packedFileHandle) {
        printf("Error opening file '%s'\n", packedFileName);
        return 0;
    }

    unpackedFileHandle = fopen(unpackedFileName, "wb");

    if (NULL == unpackedFileHandle) {
        printf("Error opening file '%s'\n", unpackedFileName);
    }
    else
    {
        result = tarUnpackFile(packedFileHandle, unpackedFileName, unpackedFileHandle);

        switch (result) {
        case 0:
            printf("File '%s' unpacked successfully\n", unpackedFileName);
            break;
        case 2:
            printf("File '%s' not found in '%s'\n", unpackedFileName, packedFileName);
            break;
        default:
            printf("Error %d unpacking file\n", result);
            break;
        }

        fclose(unpackedFileHandle);
    }

    fclose(packedFileHandle);

Links

No comments:

Post a Comment