Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:07:35+00:00 2026-06-13T23:07:35+00:00

I created a simple program. I create a string and compress it by following

  • 0

I created a simple program.

I create a string and compress it by following methods and store it in a binary data field type in sql server 2008 (binary(1000) field type).

When I read that binary data and result string is true like original string data with the same length and data but when I want to decompress it it gave me an error.

I use this method to get bytes:

 System.Text.ASCIIEncoding.ASCII.GetBytes(mystring)

And this method to get string:

System.Text.ASCIIEncoding.ASCII.GetString(binarydata)

In hard code in VS2012 editor, result string works fine, but when I read it from sql it gives me this error in first line of decompression method:

 The input is not a valid Base-64 string as it contains a
 non-base 64 character, more   than two padding characters, 
 or a non-white space character among the padding characters.

What’s wrong with my code? These two strings are same but

string test1=Decompress("mystring");

…this method works fine but this gave me that error and can not decompress retrieved string

string temp=System.Text.ASCIIEncoding.ASCII.GetString(get data from sql) ;
string test2=Decompress(temp);

The comparing these string do not shows any deference

int result = string.Compare(test1, test2); // result=0

My compression method:

   public static string Compress(string text)
   {
       byte[] buffer = Encoding.UTF8.GetBytes(text);
       var memoryStream = new MemoryStream();
       using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
       {
           gZipStream.Write(buffer, 0, buffer.Length);
       }

       memoryStream.Position = 0;

       var compressedData = new byte[memoryStream.Length];
       memoryStream.Read(compressedData, 0, compressedData.Length);

       var gZipBuffer = new byte[compressedData.Length + 4];
       Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
       Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
       return Convert.ToBase64String(gZipBuffer);
   }

My decompression method:

   public static string Decompress(string compressedText)
   {
       byte[] gZipBuffer = Convert.FromBase64String(compressedText);
       using (var memoryStream = new MemoryStream())
       {
           int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
           memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

           var buffer = new byte[dataLength];

           memoryStream.Position = 0;
           using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
           {
               gZipStream.Read(buffer, 0, buffer.Length);
           }

           return Encoding.UTF8.GetString(buffer);
       }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:07:36+00:00Added an answer on June 13, 2026 at 11:07 pm

    The most likely issue is the way you are getting the string from the SQL binary filed.

    Currently (I guess, you have not showed how you stored or retrieved your data from SQL)

    • Compress : Text -> UTF8.GetBytes -> compress -> base64 string-> Send to Sql (transformed to binary)
    • Decompress: Binary -> String representation of binary -> base64 decode -> decompress -> UTF8.GetString

    Your issue is the String representation of binary step is not the same as the Send to Sql (transformed to binary). If you are storing this as a varbinary you should be returning the byte array from compress and decompress should take in a byte array.

    public byte[] string Compress(string text)
    {
      //Snip
    }
    
    public static string Decompress(byte[] compressedText)
    {
       //Snip
    }
    

    this changes your process to

    • Compress : Text -> UTF8.GetBytes -> compress -> Send to Sql
    • Decompress: Binary -> decompress -> UTF8.GetString
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot figure out why the following simple program doesn't (create) and then write
I'm trying to create a simple program that does the following: A service (NewsService)
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I am trying to create a simple program using Gstreamer to take the input
I am attempting to create a simple program that will keep a text log
I have been trying to create a simple program with Python which uses OpenCV
I need to create a simple drawing program in c for graphs, i.e. nodes
I want to create a simple Javascript program with a HTML interface. The program
I've created this simple example program, but this doesn't work. it won't connect. any
I want to create an extremely simple iPhone program that will open a telnet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.