Verify/Validate GZip

Peter Volz 1,295 Reputation points
2023-06-06T15:25:09.97+00:00

Hello all.

There's a nasty bug in the forums disallowing me from replying to my own thread:

https://learn.microsoft.com/en-us/answers/questions/1299741/gzip-detection?page=1&orderby=Helpful&comment=answer-1265950

However, to perform a thorough validation of the entire Gzip file, is this method wise?

Using MyGZipStream As New Compression.GZipStream(MyImportStream, Compression.CompressionMode.Decompress, False)
    Try
        MyGZipStream.CopyTo(MyExportStream)
        Return True
    Catch Exception As Exception
        Return False
    End Try
End Using

I'd prefer to not have MyExportStream to be faster, just force GZipStream to read it to validate, and send the output to NULL, or there is any better way to verify a Gzip file?

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. osjaetor 480 Reputation points
    2023-06-06T17:40:06.8966667+00:00

    Hi Peter Volz,

    Here is a snippet for you to check if it solves your problem.

    using (var myGZipStream = new Compression.GZipStream(MyImportStream, Compression.CompressionMode.Decompress))
    {
        try
        {
            byte[] _buffer = new byte[4096]; //Here, we read decompressed data, but don't write it to an output stream
            while (myGZipStream.Read(_buffer, 0, _buffer.Length) > 0) {}
            return true; //If it gets this far, your Gzip is valid.
        }
        catch (Exception)
        {
            return false; //If an exception occurs, it indicates that your Gzip is invalid
        }
    }
    
    Using myGZipStream As New GZipStream(MyImportStream, Compression.CompressionMode.Decompress)
        Try
            Dim buffer As Byte() = New Byte(4096) {} 'Here, we read decompressed data, but don't write it to an output stream
            While myGZipStream.Read(buffer, 0, buffer.Length) > 0
            End While
            Return True 'If it gets this far, your Gzip is valid.
        Catch
            Return False 'If an exception occurs, it indicates that your Gzip is invalid
        End Try
    End Using
    

    Regards,

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. osjaetor 480 Reputation points
    2023-06-06T17:42:07.1233333+00:00

    Hi Peter Volz,

    Here is a snippet for you to check if it solves your problem.

    using (var myGZipStream = new Compression.GZipStream(MyImportStream, Compression.CompressionMode.Decompress))
    {
        try
        {
            byte[] _buffer = new byte[4096]; //Here, we read decompressed data, but don't write it to an output stream
            while (myGZipStream.Read(_buffer, 0, _buffer.Length) > 0) {}
            return true; //If it gets this far, your Gzip is valid.
        }
        catch (Exception)
        {
            return false; //If an exception occurs, it indicates that your Gzip is invalid
        }
    }
    

    Regards,

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.