C++:如何使用 zlib 或 lib_tar 获取未压缩的大小

C++ : how to get the uncompressed size using zlib or lib_tar

我试过下面的代码,但是很慢, 寻找计算未压缩数据大小的最佳方法。 我在某处读到过使用 zstream 结构 -> inflate api 我们可以提高性能。你能帮我完成吗?

 /* skip regfile */
int move_file_pointer(TAR *t)
{

    int i, k;
    size_t size;
    char buf[T_BLOCKSIZE];
    if (!TH_ISREG(t))
    {
        errno = EINVAL;
        return -1;
    }
    size = th_get_size(t);
    std::printf("\n Current File  Position - %lu", gztell(l_gzFile));
    long int luOffset= 0;
    int reminder =  size % T_BLOCKSIZE ;
    if(reminder == 0 )
    {
        luOffset = (size/T_BLOCKSIZE) * T_BLOCKSIZE;
    }
    else
    {
        luOffset = ((size/T_BLOCKSIZE) * T_BLOCKSIZE) + T_BLOCKSIZE ;
    }
    std::printf("\n Targeted Seek offset value %lu", luOffset);
    std::printf("\n Targeted Seek offset value %lu", luOffset);

    /*  
    ######################################################
    If you look here gzseek is taking long time to complete. for small zip file such as 500 MB, it consuming 2 mins to move end of file.
    I have TAR *t datatype here.
    gzFile l_gzFile file pointer here.
    Using these two data types.
    How can i seek to end in fast / optimal way.
    ##################################################### */    
    k = gzseek(l_gzFile, luOffset , SEEK_CUR); // l_gzFile is from gzFile data type.


    if (k == -1)
    {
        if (k != -1)
            errno = EINVAL;
    return -1;
    }
    std::printf("\n After Read Block - %lu", gztell(l_gzFile));
    return 0;
}


void getUnTarFileSize(std::string f_cSourePath)
{
    std::string dest = "/fs/usb0/untar_zlib/test/";
    TAR *l_pTarInfo = NULL;
    char *l_pcTarFileSourcePath = const_cast<char * >(f_cSourePath.c_str());
    char *l_pcTarFileDestPath =  const_cast<char * >(dest.c_str());
    //open tar archive
    if (0 != (tar_open(&l_pTarInfo, l_pcTarFileSourcePath, &gztype,  O_RDONLY, 0, TAR_GNU)))
    {
        std::printf("tar_open(): %s \n", std::strerror(errno));
    }
    else
    {
        int i = 0;
        unsigned long totalSize = 0;
        unsigned long current_size = 0;
        std::printf("\n Current File  Position - %lu \n", gztell(l_gzFile));
        while ((i = th_read(l_pTarInfo)) == 0)
        {
            char *fName = th_get_pathname(l_pTarInfo);
            current_size = th_get_size(l_pTarInfo);

            printf("\n Size of fName %s = %d", fName,current_size);
            totalSize += current_size;

            if (TH_ISREG(l_pTarInfo) && (move_file_pointer(l_pTarInfo) != 0)) {
                  fprintf(stderr, "tar_skip_regfile()\n");
                  printf("\n Value of read=%d, Error=%s\n",i,std::strerror(errno));
                  break;
            }
            fName = NULL;
            printf("\n\n");
        }
        if(-1 == i)
        {
            printf("\n Value of read=%d, Error=%s\n",i,std::strerror(errno));
        }
        else
        {
            printf("\n Total Size of given zip file=%d\n",totalSize);
        }
    }
}

int main()
{
    getUnTarFileSize("/fs/usb0/untar_zlib/a.tar.gz");
}

如果您问如何知道 gzip (.gz) 文件内容的未压缩大小,那么唯一可靠的方法是解压缩它。有关详细信息,请参阅 this answer here