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

时间:2015-08-13 13:42:12

标签: zlib

我尝试了以下代码,但速度非常慢, 寻找任何最佳方法来计算未压缩的数据大小。 我在某处使用zstream结构阅读 - >膨胀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");
}

1 个答案:

答案 0 :(得分:0)

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