c++crarunrar

libunrar RARReadHeader always return ERAR_BAD_DATA


guys, I'm recently working on the libunrar, but got error when I use RAR_OM_LIST option, I can get the first item in the rar file printed, but got ERAR_BAD_DATA afterwards. here's the code:

struct RAROpenArchiveData openArcData = {
        .ArcName = (char *)"/Users/oxnz/Developer/Nanook/unrar/x.rar",
        .OpenMode = RAR_OM_LIST,
    };
    HANDLE hArcData = RAROpenArchive(&openArcData);
    if (openArcData.OpenResult == ERAR_SUCCESS) {
        struct RARHeaderDataEx headerDataEx;
        int ret;
        while (ERAR_SUCCESS == (ret = RARReadHeaderEx(hArcData, &headerDataEx))) {
            printf("file: [%s]\n", headerDataEx.FileName);
        }
        switch (ret) {
            case ERAR_BAD_DATA:
                printf("bad data\n");
                break;
            case ERAR_END_ARCHIVE:
                printf("END OF ARCHIVE\n");
                break;
            default:
                printf("unknown error: %d", ret);
                break;
        }
        RARCloseArchive(hArcData);
    }

any information would be appreciated. thanks in advance.


Solution

  • After each call to RARReadHeaderEx you need to call RARProcessFile, even if it's only to skip over the data. E.g.

    while (ERAR_SUCCESS == (ret = RARReadHeaderEx(hArcData, &headerDataEx)))
    {
        printf("file: [%s]\n", headerDataEx.FileName);
        if (ERAR_SUCCESS != (ret = RARProcessFile(hArcData, RAR_SKIP, NULL, NULL)))
            break;
    }