none
Using Gzip RRS feed

  • Question

  • Hey

    I have a 3dXML that contains a step file which appears to be compressed using GZip as shown here in this snippet

    <STEPFile encoding="gzip;base64">
    H4sIAAAAAAACC7R9W69cx7HeuwH/BwLnQXJA7vSl+oaADzS1ZROgSIGknOMngkdmTgTYkiFL
    JzkI8t9TX3X36po9q3vNnnGcmInJmW/W6kvd66tX798+s8Yb/8zZ//bb3/zx/sVX9+/4//P1
    q9f3H7+6f//y3atvP7x6++bLL7/44ndPv3D/zX7xu/7Pb158c//lF+/ffhNfvvz6C/5HYwuD
    PXP5wxdP5fNffvHyp798/l+ffv789MmrH7+/47/67W++ePnu/u2Tb1+8469/ePfq5ZPf//nJ
    tx9ePnn15uXTJ+lpMYaxLvnMeJL3L/94/80LfsYX3314+83bD6/+JA//6g9vnvyfJ/aJeSKv
    +MRZ4v9V/8///eJ3+Pr9m6/e37/k/89XLz684P/nX/zzly/efeDvvnjz8du3r9584Bd5+qW5
    

    Does anyone know a good vb.net method decompressing the code? Do i need to save it as a file first and if so, what would the file type be?


    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Saturday, February 1, 2020 7:46 AM

All replies

  • The simple way is shown right in the documentation

    Or perhaps try the DecompressString method.

    Option Infer On
    
    Imports System.IO
    Imports System.IO.Compression
    Imports System.Text
    
    Namespace CompressStringOperations
        Public NotInheritable Class StringCompressor
    
            Private Sub New()
            End Sub
    
            ''' <summary>
            ''' Compresses the string.
            ''' </summary>
            ''' <param name="text">The text.</param>
            ''' <returns></returns>
            Public Shared Function CompressString(text As String) As String
                Dim buffer() As Byte = Encoding.UTF8.GetBytes(text)
                Dim memoryStream = New MemoryStream()
                Using gZipStream = New GZipStream(memoryStream, CompressionMode.Compress, True)
                    gZipStream.Write(buffer, 0, buffer.Length)
                End Using
    
                memoryStream.Position = 0
    
                Dim compressedData = New Byte(CInt(memoryStream.Length - 1)) {}
                memoryStream.Read(compressedData, 0, compressedData.Length)
    
                Dim gZipBuffer = New Byte(compressedData.Length + 3) {}
    
                System.Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length)
                System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4)
    
                Return Convert.ToBase64String(gZipBuffer)
    
            End Function
    
            ''' <summary>
            ''' Decompresses the string.
            ''' </summary>
            ''' <param name="compressedText">The compressed text.</param>
            ''' <returns></returns>
            Public Shared Function DecompressString(compressedText As String) As String
    
                Dim gZipBuffer() As Byte = Convert.FromBase64String(compressedText)
    
                Using memoryStream = New MemoryStream()
                    Dim dataLength As Integer = BitConverter.ToInt32(gZipBuffer, 0)
                    memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4)
    
                    Dim buffer = New Byte(dataLength - 1) {}
    
                    memoryStream.Position = 0
    
                    Using gZipStream = New GZipStream(memoryStream, CompressionMode.Decompress)
                        gZipStream.Read(buffer, 0, buffer.Length)
                    End Using
    
                    Return Encoding.UTF8.GetString(buffer)
    
                End Using
            End Function
        End Class
    End Namespace
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, February 1, 2020 2:42 PM
    Moderator
  • Hi

    Thanks for the reply. Like always, documentation doesnt always reflect the current version of what you have. in this instance, its suggested to create the VBCompression.sln example but that doesnt exist on my machine...

    Using your sample, i can see how the string could be decompressed but how would i go about saving that decompression as a file?

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Sunday, February 2, 2020 2:08 AM
  • Write the buffer byte array to a file e.g. File.WriteAllBytes

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, February 2, 2020 2:22 AM
    Moderator
  • You can use 7-Zip and Process.

    George Frias - AWWshop @ Wikidot, Wiki1978

    Sunday, February 2, 2020 2:26 AM
  • Hey Karen

    thanks. I had remembered i did something similar a while ago and grabbed that code. One problem i am having with decompressing the code, im getting an error

    System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. '

    on this line

     Dim gZipBuffer() As Byte = Convert.FromBase64String(compressedText)

    The byte code i am trying to convert / decompress is this. It should create a STEP file at the end which is a 3D model currently saved in an XML file as a byte

    <STEPFile encoding="gzip;base64">
    H4sIAAAAAAACC7S9W69cx5Em+m7A/4FAP0gekHvyEnnDgA80uW0ToEiBpHzaTwRb5vQIsKWG
    LPVM4+D89xNfZObKqF0rc9Wu8njGnDFZ9dVaeYl7fPH6w7tn1njjnzn7P377mz/dv3h1/57/
    P394/eb+06v7Dy/fv/724+t3b7/++quvfvf0K/c/7Fe/6//89sU3919/9eHdN79/aV9+xf9o
    nHlm3DNjP371VD7/9Vcvf/rrl//9+ecvT5+8/vH7O/6r3/7mq5fv7989+fbFe/76x/evXz75
    /V+efPvx5ZPXb18+fZKfGmMY65LPjCf58PJP99+84Gd88d3Hd9+8+/j6z/Lwr//49sn/+8Q+
    MU/kFZ84S/y/6v/5/776Hb5+//bVh/uX/P959eLjC/5//sU/f/ni/Uf+7ou3n7599/rtR36R
    AuuviP4Ouf/j8vblLqcIMaAA==
    	</STEPFile>

    note, i had to truncate the byte file as there is a limt of 6000 chars in the post..

    i tried without the XML headers but it threw a different error about not fining the GZip tag


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Sunday, February 2, 2020 2:29 AM