Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. String compressing with System.IO.Compression

String compressing with System.IO.Compression

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpxmlperformancequestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBean
    wrote on last edited by
    #1

    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 ;)

    D 1 Reply Last reply
    0
    • M MrBean

      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 ;)

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You do realize that the Compression class only outputs binary data? Outputs to any stream by the way, even a MemoryStream object. You can find an example here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You do realize that the Compression class only outputs binary data? Outputs to any stream by the way, even a MemoryStream object. You can find an example here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        M Offline
        M Offline
        MrBean
        wrote on last edited by
        #3

        "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 ?

        D 1 Reply Last reply
        0
        • M MrBean

          "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 ?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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

            M Offline
            M Offline
            MrBean
            wrote on last edited by
            #5

            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 :)

            D 1 Reply Last reply
            0
            • M MrBean

              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 :)

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • D Dave Kreskowiak

                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

                M Offline
                M Offline
                MrBean
                wrote on last edited by
                #7

                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

                D 1 Reply Last reply
                0
                • M MrBean

                  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

                  D Offline
                  D Offline
                  Darell F Butch Jr
                  wrote on last edited by
                  #8

                  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); } } }

                  M 1 Reply Last reply
                  0
                  • D Darell F Butch Jr

                    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); } } }

                    M Offline
                    M Offline
                    MrBean
                    wrote on last edited by
                    #9

                    Thanks for the code... I'll try it later :) So far I have solved my problems by using a 3rd party component (Xceed) which was part of a suite we had allready purchased. It workes perfectly, is very easy to use and is apparently more efficient :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups