ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

StringToZip

Compactação de string utilizado geralmente para compactar Base64

Source

<Extension()> _
Public Function toZip(text As String) As String
    Dim buffer As Byte() = System.Text.Encoding.Unicode.GetBytes(text)
    Dim ms As New MemoryStream()
    Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, True)
        zipStream.Write(buffer, 0, buffer.Length)
    End Using

    ms.Position = 0
    Dim outStream As New MemoryStream()

    Dim compressed As Byte() = New Byte(ms.Length - 1) {}
    ms.Read(compressed, 0, compressed.Length)

    Dim gzBuffer As Byte() = New Byte(compressed.Length + 3) {}
    System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length)
    System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4)
    Return Convert.ToBase64String(gzBuffer)
End Function

Example

Dim IMG_64 As String = PictureBox1.Image.toBase64
  Dim Zip64 As String = IMG_64.toZip

Author: Alex Martins

Submitted on: 23 aug 2017

Language: VB

Type: StringToZip

Views: 1489