Question

I have the error while decompressing

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

It compresses fine but doesn't decompress. I've looked at many other examples that have the same problem, and I feel like I'm following what's said but still am getting nothing when I decompress. Here's the compression and decompression methods:

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

        mStream.Position = 0;

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

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


    public static string DecompressData(string compressedData)
    {
        byte[] gZipBffr = Convert.FromBase64String(compressedData);
        using (var mStream = new MemoryStream())
        {
            int dataLength = BitConverter.ToInt32(gZipBffr , 0);
            mStream.Write(gZipBffr , 4, gZipBffr .Length - 4);

            var buffer = new byte[dataLength];

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

            return Encoding.UTF8.GetString(buffer);
        }
    }

 string s = CompressData(s2.Tostring());

where s2 is type XElement string pH = DecompressData(stream2)); where stream2 is type string .. in the database it is stored in nvarchar type column here while compressing am removing the root tags .

for the first time the xml is like peet 3/24/2012 Percent 33.3 10 for next time another student data is added to the existing xml, here everytime while compressing we have to remove the parent tag.

        <student>
        <data>
       <name>peet</name> 
       <date>3/24/2012</date> 
       <field>Percent</field>
       <new>33.3</new> 
       <old>10</old> 
        </data>
    <data>
       <name>raaz</name> 
       <date>3/24/2011</date> 
       <field></field>
       <new>33.3</new> 
       <old>10</old> 
    </data>
        <data>
       <name>arya</name> 
       <date>3/24/2010</date> 
       <field></field>
       <new>33.3</new> 
       <old>10</old> 
    </data>
        </student>
Was it helpful?

Solution

I think your DecompressData method is not quite right - it will be fine for small sizes but when the data gets more than one student data in it I think it will fail because you do not have enough space allocated in your buffer.

A method that will work is this (there may be a better approach than doing the array copies and you really should make the buffer larger than 100 but I made it small to be sure it loops a few times to ensure it works properly):

public static string DecompressData(string compressedData) 
{
    byte[] gZipBffr = Convert.FromBase64String(compressedData); 
    using (var mStream = new MemoryStream()) 
    { 
        mStream.Write(gZipBffr, 4, gZipBffr.Length - 4);      
        mStream.Position = 0;

        var bytes = new byte[0]; 
        using (var gZipStream = new GZipStream(mStream, 
                                               CompressionMode.Decompress)) 
        { 
            byte[] buffer = new byte[100];
            int read;
            while((read = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                var newBytes = new byte[bytes.Length + read];
                Array.Copy(bytes, newBytes, bytes.Length);
                Array.Copy(buffer, 0, newBytes, bytes.Length, read);
                bytes = newBytes;
            }
        }
        return Encoding.UTF8.GetString(bytes); 
   }          
}

If I have understood you correctly you are storing base 64 encoded compressed XML in the database (nvarchar column) and you want to take it out, decompress it, add a node to it, compress it and put it back.

To help you a bit (maybe) here is some code I knocked up that does that process (except the databse calls - you can see where you could put those in):

private static void UpdateStudent(XElement data, int studentId)
{
    XDocument student = ReadStudent(studentId);
    student.Root.Add(data);
    SaveStudent(studentId, student);
}

private static void SaveStudent(int studentId, XDocument data)
{
    string compressed = CompressData(data.ToString());
    SaveStudentData(studentId, compressed);
}

private static XDocument ReadStudent(int studentId)
{
    string data = GetStudentFromDatabase(studentId);
    XDocument ret;
    if (string.IsNullOrEmpty(data))
    {
        ret = XDocument.Parse("<student></student>");
    }
    else
    {
        string decompressed = DecompressData(data).TrimEnd('\0');
        ret = XDocument.Parse(decompressed);
    }
    return ret;
}

// These two methods would query the database but for demo, just store
// in a static member
private static string GetStudentFromDatabase(int studentId)
{ 
    return _data;
}

private static void SaveStudentData(int studentId, string data)
{
    _data = data;
}

private static string _data;

Then to call that I did:

UpdateStudent(XElement.Parse("<data><name>peet</name><date>3/24/2012</date><field>Percent</field><new>33.3</new><old>10</old></data>"), 1);
UpdateStudent(XElement.Parse("<data><name>raaz</name><date>3/24/2011</date><field></field><new>33.3</new><old>10</old></data>"),1);
UpdateStudent(XElement.Parse("<data><name>arya</name><date>3/24/2010</date><field></field><new>33.3</new><old>10</old></data> "), 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top