Using zlib.net

In one of our project,
we required to compress a file using zlib.net and store it in database in byte array format,
and on retrieving it, uncompressed byte array and create a file on disc.

Hope this post helps others …. !

download Zlib.net click here

Add reference of above dll.

using zlib;

public void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}

//compress function gets the file path to be compresss and fills in byte array arrCompressed
public void compress( ref byte[] arrCompressed, string strFileSrcPath)
{
MemoryStream oOutStream = new MemoryStream();
ZOutputStream ostream = new ZOutputStream(oOutStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);
FileStream inStream = new FileStream(strFileSrcPath, FileMode.Open);
try
{
CopyStream(inStream, ostream);
ostream.finish();
arrCompressed = oOutStream.ToArray();
}
finally
{
ostream.Close();
oOutStream.Close();
inStream.Close();
}
}

//uncompress function gets back file from compressed btye Array
public void uncompress( byte[] arrCompressed, string strFileDesName)
{
MemoryStream oInStream = new MemoryStream(arrCompressed);
ZInputStream oZInstream = new ZInputStream(oInStream);
MemoryStream oOutStream = new MemoryStream();

byte[] buffer = new byte[2000];
int len;
while ((len = oZInstream.read(buffer, 0, 2000)) > 0)
{
oOutStream.Write(buffer, 0, len);
}
oOutStream.Flush();
byte[] arrUncompressed = oOutStream.ToArray();
oZInstream.Close();
oOutStream.Close();
writeByteArrayToFile(arrUncompressed, strFileDesName);
}

//Writes Byte Array to Disc
public void writeByteArrayToFile(byte[] buff, string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff);
bw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

Methods in Global.asax

Application & Session Events in the Global.asax File

Methods corresponding to events that fire on each request

Application_BeginRequest() – fired when a request for the web application comes in.

Application_AuthenticateRequest() –fired just before the user credentials are authenticated. You can specify your own authentication logic over here.

Application_AuthorizeRequest() – fired on successful authentication of user’s credentials. You can use this method to give authorization rights to user.

Application_ResolveRequestCache() – fired on successful completion of an authorization request.

Application_AcquireRequestState() – fired just before the session state is retrieved for the current request.

Application_PreRequestHandlerExecute() – fired before the page framework begins before executing an event handler to handle the request.

Application_PostRequestHandlerExecute() – fired after HTTP handler has executed the request.

Application_PreSendRequestHeaders()-fired just before ASP.Net sends HTTP Headers to the client. This can be useful if you want to modify a header.

Application_PreSendRequestContent() – just before ASP.Net sends content to the client.

Application_ReleaseRequestState() – fired before current state data kept in the session collection is serialized.

Application_UpdateRequestCache() – fired before information is added to output cache of the page.

Application_EndRequest() – fired at the end of each request

Methods corresponding to events that do not fire on each request

Application_Start() – fired when the first resource is requested from the web server and the web application starts.

Application_Init() – fired after_Start and is used for initializing code.

Session_Start() – fired when session starts on each new user requesting a page.

Application_Error() – fired when an error occurs.

Session_End() – fired when the session of a user ends.

Application_End() – fired when the web application ends.

Application_Disposed() – fired when the web application is destroyed.

Tables vs Divs

Hi All,

Over the last several years, developers have moved from table to div based website structures, WHY?

Well, I like to mention good / bad points of both table and div.

Starting with table

Table

For beginner tables are easy to learn and implement, we see tabular data every day, so the concept is well known.

The existence table attributes makes it easy to implement as developer doesn’t have to use a separate style sheet.

Tables don’t break when the content is too wide. Columns are not squeezed under other columns as they are in a div-based structure.

But at the same time, using tables increase in the number of line of code and nested tables add the complexity to it. The colspan and rowspan attributes make the code even more complex, and any developer maintaining the page in future has to go through alot of code to understand its structure.

More lines of code mean larger file sizes, which mean longer page loading time.

Table structure provides less flexibility in updating the existing code due to its rigid row-column structure.

Div

Div is not wrapped in a parent element the way td tags are in tables, so they do not have the limitation of it parent tag.

Using Div and CSS is immensely cleaner and more easily editable. All you need to do is make changes to your CSS code to change the all website pages across the board

Div–based website renders on your browser much faster as they are lighter in weight.

Div based website is completely flexible and it Increases accessibility and usability of a site. A good example this is site called Zen Garden – The beauty of CSS Design, all the templates are design without changing the HTML, simply by generating the CSS style sheet.

To develop div-based website developer is required to have good understanding of technology and experience writing CSS. it takes a more time to get the effect (compare to table-based website) and requires a lot of work, time and patience in order to make your website compatible with older browsers.

Considering all above point I have made my mind, that I’ll use table only for displaying tabular data and not structuring the website , for structuring I’ll go with div-based structure.

You make your own choice J

Regards,

Deepak waje