How can I remove unassigned elements from the end of a TArray

I was hoping to see if there was an easy way for me to do this. I’m grabbing a GZip compressed file with an Http request and attempting to parse the contents into a protobuf class. Since I can’t be sure of the size of the file once decompressed, what I’ve been doing is setting the array size to be much larger than what is expected. The problem I’m encountering is that the data can only parse into the protobuf class if the Array containing the decompressed data is exactly the correct size. Is there a way to access this and then trim the array down to the correct size?

This is the code where I’m using zlib to decompress the file:

        TArray<uint8> data = Response->GetContent();
        TArray<uint8> decompressedData;
        decompressedData.SetNum(1884);          //This is the exact size of the decompressed file
        UE_LOG(LogTemp, Warning, TEXT("Decompressing..."));
        z_stream infstream;
        infstream.zalloc = Z_NULL;
        infstream.zfree = Z_NULL;
        infstream.opaque = Z_NULL;

        infstream.avail_in = data.Num(); 
        infstream.next_in = (Bytef *)data.GetData(); 
        infstream.avail_out = decompressedData.Num();
        infstream.next_out = (Bytef *)decompressedData.GetData(); 
        // the actual DE-compression work.
        inflateInit2(&infstream, 16+MAX_WBITS);
        int err = inflate(&infstream, Z_FINISH);
        inflateEnd(&infstream);
        if (err == Z_STREAM_END) {
            UE_LOG(LogTemp, Warning, TEXT("decompressed!"));
        } else {
            UE_LOG(LogTemp, Error, TEXT("Error decompressing"));
        }

Hello! I have just take a look at Примеры сжатия данных в C/C++ при помощи zlib | Записки программиста and think that maybe you can implement decompressing with data chunks instead of guessing what would be total size… Moreover, this algorithm should answer how many bytes are processed to output.

Managed to find a way of doing it, probably not the be the best method but what I do is simply iterate through the array until I hit a value of zero and cut it there. I’ve tested it on a few files and had success.

//Trim the array down so that it is the correct size
        for (int i = 0; i < decompressedData.Num(); i++) {
            if (decompressedData[i] == 0) {
                UE_LOG(LogTemp, Error, TEXT("estimated size is %i"), i);
                decompressedData.SetNum(i);
                break;
            }
        }