Sometimes you just have to save files in the database. The reason? Because "Simon" said so. So without further ado, here is a simple example on how to compress files using C# and SharpZipLib:
1
2 using System;
3 using System.IO;
4 using ICSharpCode.SharpZipLib.Zip.Compression;
5 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
6
7 namespace YourNamespace {
8 public class GenericCompressor {
9 public static byte[] Compress(byte[] buffer) {
10 MemoryStream ms = new MemoryStream();
11
12 Stream stream = new DeflaterOutputStream(
13 ms,
14 new Deflater(Deflater.BEST_COMPRESSION),
15 buffer.Length
16 );
17
18 try {
19 stream.Write(buffer, 0, buffer.Length);
20 }
21 finally {
22 stream.Close();
23 stream.Dispose();
24 }
25
26 return ms.ToArray();
27 }
28
29 public static byte[] Decompress(byte[] buffer) {
30 MemoryStream ms = new MemoryStream();
31 byte[] data = new byte[4096];
32
33 Stream stream = new InflaterInputStream(
34 new MemoryStream(buffer)
35 );
36
37 try {
38 while (true) {
39 int bytes = stream.Read(data, 0, data.Length);
40
41 if (bytes < 1) {
42 break;
43 }
44
45 ms.Write(data, 0, bytes);
46 }
47 }
48 finally {
49 stream.Close();
50 stream.Dispose();
51 }
52
53 return ms.ToArray();
54 }
55 }
56 }
To compress files in ASP.Net, the code would be something like this:
1
2 byte[] data = new byte[fileUpload.PostedFile.ContentLength];
3
4 fileUpload.PostedFile.InputStream.Read(data, 0, data.Length);
5
6 byte[] compressedData = GenericCompressor.Compress(data);
7
8 // Then save to your favorite database...
Conclusion:
I prefer having this utility over other solutions because I don't have to be tied up to a specific database vendor and of course, no database component or assembly to install. No hassle.
With the new extension method feature of C# 3.5 it's now easy to extend your classes for additional functionality. Here are some useful functionalities to add to your favorite System.String:
1
2 // Sample namespace
3 namespace CompanyName.ProjectName.Common {
4 public static class StringExtension {
5 public static string Left(
6 this string text,
7 int length
8 ) {
9
10 if (text.Length < length) {
11 return text;
12 }
13
14 return text.Substring(0, length);
15 }
16
17 public static string Right(
18 this string text,
19 int length
20 ) {
21
22 if (text.Length < length) {
23 return text;
24 }
25
26 return text.Substring(
27 text.Length - length,
28 length
29 );
30 }
31
32 public static string Mid(
33 this string text,
34 int start
35 ) {
36
37 return Mid(
38 text,
39 start,
40 text.Length - (start - 1)
41 );
42 }
43
44 public static string Mid(
45 this string text,
46 int start, int length
47 ) {
48
49 if (text.Length < length + 1) {
50 length = text.Length - 1;
51 }
52
53 return text.Substring(start - 1, length);
54 }
55 }
56 }
57
Sample usage would be like this:
1
2 string text = "ABCDE";
3
4 Console.WriteLine(text.Left(1));
5 Console.WriteLine(text.Right(2));
6 Console.WriteLine(text.Mid(3));
7 Console.WriteLine(text.Mid(3, 1));
8
9 // Output:
10 A
11 DE
12 CDE
13 C
14
Taken from the MSDN documentation:
The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception.
So in other words whatever code you have in your "finally" block will always execute when an exception occurs, right? Wrong. The correct answer is - it depends on the error.
Let me give you a simple example:
1
2 static void Main(string[] args) {
3 try {
4 Console.WriteLine("Try block");
5
6 Recurse();
7 }
8 catch {
9 Console.WriteLine("Catch block");
10 }
11 finally {
12 Console.WriteLine("Finally block");
13
14 Console.ReadLine();
15 }
16 }
17
18 static void Recurse() {
19 Recurse();
20 }
21
The System.StackOverflowException is an exception that will NOT execute codes in your catch and finally blocks.
Hope this helps.
A little gathering took place last May 16, 2008 in Rod's condo. 'Twas fun!!! Here are some pictures of the gang.

Charm, girl in red, on her 10th cup of Bacardi :-P

Knights of the rectangle table

Some random pose with the Ryan's better half - "The Doc"

Hey, Olem's drunk and Jen's crying :-P

4 alcoholic bastards watching Angel Locsin's Lobo on TV, darn!

PeeGees, my friends...;-)