.NET Framework有一個允許這樣做的內置支持。
基本上,你將不得不把你的字符串,並將其轉換爲字節數組。然後你將不得不壓縮(壓縮)你的字節數組並將其轉換爲base64字符串。
Public Function ZipString(stringToZip As String) As String
Dim returnValue As String = ""
Using output As New MemoryStream()
Using gzip As New DeflateStream(output, CompressionMode.Compress)
Using writer As New StreamWriter(gzip, Encoding.[Default])
writer.Write(stringToZip)
End Using
End Using
returnValue = Convert.ToBase64String(output.ToArray())
End Using
Return returnValue
End Function
要獲取原始的字符串,你卻反其道而行之:
Public Function UnZipString(stringToUnZip As String) As String
Dim returnValue As String = ""
Using inputStream As New MemoryStream(Convert.FromBase64String(stringToUnZip))
Using gzip As New DeflateStream(inputStream, CompressionMode.Decompress)
Using reader As New StreamReader(gzip, System.Text.Encoding.UTF8)
returnValue = reader.ReadToEnd()
End Using
End Using
End Using
Return returnValue
End Function
然後,您將能夠在JSON存儲爲壓縮串並解壓縮它時,你會需要它。
取決於您使用的編程語言和環境。 – JJJ
您可以嘗試使用GZip將文本壓縮爲二進制文件,然後使用base64進行編碼,以便它仍然可以作爲文本包含在JSON中。雖然YMMV表現明智。 – Jack
@Juhana我正在使用VB.net for windows applicaton。 – rex