Thursday, June 25, 2015

C# byte [ ] Array Compress and Decompress with GZipStream

A small utility class for compressing and decompressing byte [ ] array in C#

using System.IO;
using System.IO.Compression;

public static class Compression
{
    public static byte[] Compress(byte[] data)
    {
        using (var ms = new MemoryStream())
        {
            using (var gzip = new GZipStream(ms, CompressionLevel.Optimal))
            {
                gzip.Write(data, 0, data.Length);
            }
            data = ms.ToArray();
        }
        return data;
    }

    public static byte[] Decompress(byte[] data)
    {
        // the trick is to read the last 4 bytes to get the length
        // gzip appends this to the array when compressing
        var lengthBuffer = new byte[4];
        Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
        int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
        var buffer = new byte[uncompressedSize];
        using (var ms = new MemoryStream(data))
        {
            using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
            {
                gzip.Read(buffer, 0, uncompressedSize);
            }
        }
        return buffer;
    }
}

Monday, August 11, 2014

Fix User Permission in SQL Server

After the restore, it is needed to fix the previous user permissions in SQL Server Database.
EXEC sp_change_users_login 'Auto_Fix', 'webuser'
Here webuser is the user name.

Enable Database Mail in SQL Server

Enable the db mail feature at server level:
sp_configure 'Database Mail XPs', 1
reconfigure

Enable service broker in the MSDB database:
USE [master]
GO
ALTER DATABASE [MSDB] SET  ENABLE_BROKER WITH NO_WAIT
GO

Enable 'xp_cmdshell' SQL Server


-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

Monday, March 10, 2014

Prevent Browser Back Option

Use this following code to prevent the web browsers Back button options.

<script type="text/javascript">
        function preventBack() { window.history.forward(); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
</script> 

This will forward to the last history page.