String compressing with System.IO.Compression
-
Basicly I want to be able to compress a String using System.IO.Compression to another String and be able to decompress it later. I want to use this for compressing XML (and other string values) into a String variable which I store elsewhere. It MUST be compressed to a String - no binary data. Also, I do not want to save the XML to a file and compress/decompress from there; compressing/decompressing must be in-memory only. Any advice or sample code (VB.NET or C#) ? Thanks in advance ;)
-
Basicly I want to be able to compress a String using System.IO.Compression to another String and be able to decompress it later. I want to use this for compressing XML (and other string values) into a String variable which I store elsewhere. It MUST be compressed to a String - no binary data. Also, I do not want to save the XML to a file and compress/decompress from there; compressing/decompressing must be in-memory only. Any advice or sample code (VB.NET or C#) ? Thanks in advance ;)
-
"You do realize that the Compression class only outputs binary data?" Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now). Also, I'm NOT looking for something to compress a stream... Anybody else, please ?
-
"You do realize that the Compression class only outputs binary data?" Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now). Also, I'm NOT looking for something to compress a stream... Anybody else, please ?
MrBean wrote:
Also, I'm NOT looking for something to compress a stream...
Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
MrBean wrote:
Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now).
You could probably do what you want with a StringBuilder[^] object. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
MrBean wrote:
Also, I'm NOT looking for something to compress a stream...
Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
MrBean wrote:
Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now).
You could probably do what you want with a StringBuilder[^] object. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Dave Kreskowiak wrote:
Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
Yes, you are right - but I'm looking for some code which shows me how to do it in more detail :| I got the compression code to work just fine, but can't seem to decompress it. Here's the compression code which seems to work : Public Function CompressString(ByVal strInput As String) As String Dim buffer() As Byte = Encoding.UTF8.GetBytes(strInput) Dim memoryBuffer As New MemoryStream() Dim compressedZipStream As New GZipStream(memoryBuffer, CompressionMode.Compress, False) compressedZipStream.Write(buffer, 0, buffer.Length) compressedZipStream.Close() Return Convert.ToBase64String(memoryBuffer.GetBuffer) End Function
Dave Kreskowiak wrote:
You could probably do what you want with a StringBuilder[^] object.
Ok, but Convert.ToBase64String seems to work also :)
-
Dave Kreskowiak wrote:
Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
Yes, you are right - but I'm looking for some code which shows me how to do it in more detail :| I got the compression code to work just fine, but can't seem to decompress it. Here's the compression code which seems to work : Public Function CompressString(ByVal strInput As String) As String Dim buffer() As Byte = Encoding.UTF8.GetBytes(strInput) Dim memoryBuffer As New MemoryStream() Dim compressedZipStream As New GZipStream(memoryBuffer, CompressionMode.Compress, False) compressedZipStream.Write(buffer, 0, buffer.Length) compressedZipStream.Close() Return Convert.ToBase64String(memoryBuffer.GetBuffer) End Function
Dave Kreskowiak wrote:
You could probably do what you want with a StringBuilder[^] object.
Ok, but Convert.ToBase64String seems to work also :)
OK. That seems to work. Now how about the decompression code? It should be doing the exact same thing in reverse... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
OK. That seems to work. Now how about the decompression code? It should be doing the exact same thing in reverse... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Well... can't get it to work :( Below is the code that I'm currently "fiddling" with. The CompressString function has been revised and seems to work correctly now. But the Decompress function method somehow still doesn't work. ZipStream.Read simply doesn't decompress the data in the memoryStream and returns 0 ??
Public Function CompressString(ByVal strInput As String) As String Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput) Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput) Dim ms As New MemoryStream() Dim ZipStream As New GZipStream(ms, CompressionMode.Compress, False) ZipStream.Write(bufIn, 0, bufIn.Length) Dim bufOut() As Byte = ms.GetBuffer ZipStream.Close() Return Convert.ToBase64String(bufOut) End Function Public Function DeCompressString(ByVal strInput As String) As String Dim bufIn() As Byte = Convert.FromBase64String(strInput) Dim ms As New MemoryStream() ms.Write(bufIn, 0, bufIn.Length) ' Compressed data ms.Position = 0 Dim ZipStream As New GZipStream(ms, CompressionMode.Decompress) Dim bufOut(30000) As Byte ' Predefined buffer-size just for test purposes!! Dim nRead As Integer nRead = ZipStream.Read(bufOut, 0, bufOut.Length) ' Returns 0 ???? ZipStream.Close() Return Convert.ToString(bufOut) ' never mind this... WIP End Function
-
Well... can't get it to work :( Below is the code that I'm currently "fiddling" with. The CompressString function has been revised and seems to work correctly now. But the Decompress function method somehow still doesn't work. ZipStream.Read simply doesn't decompress the data in the memoryStream and returns 0 ??
Public Function CompressString(ByVal strInput As String) As String Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput) Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput) Dim ms As New MemoryStream() Dim ZipStream As New GZipStream(ms, CompressionMode.Compress, False) ZipStream.Write(bufIn, 0, bufIn.Length) Dim bufOut() As Byte = ms.GetBuffer ZipStream.Close() Return Convert.ToBase64String(bufOut) End Function Public Function DeCompressString(ByVal strInput As String) As String Dim bufIn() As Byte = Convert.FromBase64String(strInput) Dim ms As New MemoryStream() ms.Write(bufIn, 0, bufIn.Length) ' Compressed data ms.Position = 0 Dim ZipStream As New GZipStream(ms, CompressionMode.Decompress) Dim bufOut(30000) As Byte ' Predefined buffer-size just for test purposes!! Dim nRead As Integer nRead = ZipStream.Read(bufOut, 0, bufOut.Length) ' Returns 0 ???? ZipStream.Close() Return Convert.ToString(bufOut) ' never mind this... WIP End Function
This code should do the trick: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.IO.Compression; namespace CompressDecompress { class Program { static void Main(string[] args) { string LstrTest = "This is a test"; string LstrOutput; LstrOutput = CompressString(LstrTest); LstrTest = DeCompressString(LstrOutput); Console.WriteLine(LstrTest); } public static string CompressString(string PstrInput) { MemoryStream Lms; GZipStream LstmZipStream; byte[] LbytBufIn; byte[] LbytBufOut; LbytBufIn = Encoding.UTF8.GetBytes(PstrInput); Lms = new MemoryStream(); LstmZipStream = new GZipStream(Lms, CompressionMode.Compress, false); LstmZipStream.Write(LbytBufIn, 0, LbytBufIn.Length); LbytBufOut = Lms.GetBuffer(); LstmZipStream.Close(); return Convert.ToBase64String(LbytBufOut); } public static string DeCompressString(string PstrInput) { MemoryStream Lms; GZipStream LstmZipStream; UTF8Encoding Lutf; byte[] LbytBufIn; byte[] LbytBufOut; int LintRead; LbytBufIn = Convert.FromBase64String(PstrInput); Lms = new MemoryStream(); Lms.Write(LbytBufIn, 0 , LbytBufIn.Length); Lms.Position = 0; LbytBufOut = new byte[LbytBufIn.Length]; LstmZipStream = new GZipStream(Lms, CompressionMode.Decompress); LintRead = LstmZipStream.Read(LbytBufOut, 0, LbytBufOut.Length); LstmZipStream.Close(); Lutf = new UTF8Encoding(); return Lutf.GetString(LbytBufOut, 0 , LintRead); } } }
-
This code should do the trick: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.IO.Compression; namespace CompressDecompress { class Program { static void Main(string[] args) { string LstrTest = "This is a test"; string LstrOutput; LstrOutput = CompressString(LstrTest); LstrTest = DeCompressString(LstrOutput); Console.WriteLine(LstrTest); } public static string CompressString(string PstrInput) { MemoryStream Lms; GZipStream LstmZipStream; byte[] LbytBufIn; byte[] LbytBufOut; LbytBufIn = Encoding.UTF8.GetBytes(PstrInput); Lms = new MemoryStream(); LstmZipStream = new GZipStream(Lms, CompressionMode.Compress, false); LstmZipStream.Write(LbytBufIn, 0, LbytBufIn.Length); LbytBufOut = Lms.GetBuffer(); LstmZipStream.Close(); return Convert.ToBase64String(LbytBufOut); } public static string DeCompressString(string PstrInput) { MemoryStream Lms; GZipStream LstmZipStream; UTF8Encoding Lutf; byte[] LbytBufIn; byte[] LbytBufOut; int LintRead; LbytBufIn = Convert.FromBase64String(PstrInput); Lms = new MemoryStream(); Lms.Write(LbytBufIn, 0 , LbytBufIn.Length); Lms.Position = 0; LbytBufOut = new byte[LbytBufIn.Length]; LstmZipStream = new GZipStream(Lms, CompressionMode.Decompress); LintRead = LstmZipStream.Read(LbytBufOut, 0, LbytBufOut.Length); LstmZipStream.Close(); Lutf = new UTF8Encoding(); return Lutf.GetString(LbytBufOut, 0 , LintRead); } } }