dongping4461
dongping4461
2014-10-31 07:44

VB.NET:解压缩一个字符串,通过PHP压缩

I have a PHP Script which sends me information like this:

$output = base64_encode(gzencode('Hallo Welt',9));

I get this from a HTTP-Request into a string.

This is what I get into VB.NET:

H4sIAAAAAAACC/NIzMnJVwhPzSkBAHUUKFAKAAAA

Every try to decompress this with the help of System.IO.Compression.GZipStream just doesn't seem to work.

Can anyone please help me? Thank you in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongya0914 dongya0914 7年前

    As I have wrote in my comment: The PHP code first compresses your text using GZip but then also converts the resulting bytes to a base64 string. Before you can decompress this you need to reverse the base64 process. This is how it works in VB:

    'Convert base64 string to byte array
    Dim bytes As Byte() = System.Convert.FromBase64String("H4sIAAAAAAACC/NIzMnJVwhPzSkBAHUUKFAKAAAA")
    
    'Standard GZipStream code for decompression
    Using ms As New IO.MemoryStream(bytes)
        Using gzs As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress)
            Using rdr As New IO.StreamReader(gzs)
                Console.WriteLine(rdr.ReadToEnd)
            End Using 'rdr
        End Using 'gzs
    End Using 'ms
    Console.ReadKey()
    

    It produces your input string "Hallo Welt" again.

    Converting a byte array to base64 when you send it over a text based system is a good idea, because you don't have problems with text encodings and unprintable characters. Base64 uses a printable subset of symbols to represent all values from 0-255, thus making it pretty perfect for sending.

    点赞 评论 复制链接分享