VB.NET COM preSS DECOM preSS字符串 [英] VB.NET compress Decompress String

查看:231
本文介绍了VB.NET COM preSS DECOM preSS字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何COM preSS / DECOM preSS字符串在VB.NET? 我想通过网络发送长字符串,需要他们在发送之前要尽可能的小。

How do I compress/decompress a string in VB.NET ? I am trying to send long string through the Network and need them to be as small as possible before sending.

感谢

推荐答案

您可以使用GzipStream。

You could use GzipStream.

下面是一个例子:

//Compress
Dim mem As New IO.MemoryStream
Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
Dim sw As New IO.StreamWriter(gz)
sw.WriteLine("hello compression")
sw.Close()

//Decompress
Dim mem2 As New IO.MemoryStream(mem.ToArray)
gz = New System.IO.Compression.GZipStream(mem2, IO.Compression.CompressionMode.Decompress)
Dim sr As New IO.StreamReader(gz)
MsgBox(sr.ReadLine)
sr.Close()

编辑2年后...

这并没有为我相当的工作,也许是因为我需要将数据存储在一个字节数组。这个工作对于COM preSS:

edit 2 years later...

That didn't quite work for me, perhaps because I needed to store the data in a byte array. This worked for Compress:

'Compress
 Dim mem As New IO.MemoryStream
 Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
 Dim sw As New IO.StreamWriter(gz)
 sw.Write(value)
 mem.Seek(0, IO.SeekOrigin.Begin)
 Dim sr As New IO.BinaryReader(mem)
 _zippedXML = sr.ReadBytes(CInt(mem.Length))
 sw.Close()

那么对于DECOM preSS我刚过字节数组到MEM2,而不是mem.ToArray的构造。

Then for Decompress I just passed the byte array into the constructor of mem2 instead of mem.ToArray.

这篇关于VB.NET COM preSS DECOM preSS字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆