Prash's Blog

Compress and Uncompress in C# using .Net APIs December 3, 2012

Filed under: C# — prazjain @ 4:02 pm
Tags: , ,

Code snippet below for uncompressing a zip file to text file and compressing a text file back to zip file.
It is really minimal and I was able to uncompress 3+ gb text files using this, and as it just deals with stream you do not need to store the contents of the file in memory!
Here you go

        internal void UncompressZipFile(string fullPath, string zipPath)
        {
            //uncompress it
            Log.InfoFormat("Uncompressing XML zip file : Start");
            using (GZipStream gzs = new GZipStream(new FileStream(zipPath, FileMode.Open), CompressionMode.Decompress))
            {
                using (FileStream xmlFile = File.Create(fullPath))
                {
                    gzs.CopyTo(xmlFile);
                }
            }
            Log.InfoFormat("Uncompressing XML zip file : Finish");
        }

        internal void CompressCSVFile(string fullPath, string zipPath)
        {
            //Compress it
            Log.Info("Compressing CSV file : Start");
            using (FileStream csvFile = new FileStream(fullPath, FileMode.Open))
            {
                using (GZipStream gzs = new GZipStream(new FileStream(zipPath, FileMode.Create), CompressionMode.Compress))
                {
                    csvFile.CopyTo(gzs);
                }
            }
            Log.Info("Compressing CSV file : Finish");
        }

 

Tutorial to Setup Hadoop on Windows with Eclipse October 23, 2012

Filed under: Hadoop — prazjain @ 9:19 pm
Tags:

This is how I setup Hadoop on Windows, for anyone looking to setup Hadoop go through this : http://v-lad.org/Tutorials/Hadoop/00%20-%20Intro.html. Everything else that I tried was too complicated or had steps missing or inconsistent.

 

Could not find default endpoint element that references contract in the ServiceModel client configuration section October 3, 2012

Filed under: Uncategorized — prazjain @ 2:23 pm

I get this error when testing one of my methods that makes WCF service calls, and at service invocation I get this error :

Could not find default endpoint element that references contract ‘XYZ.IMyInterface’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

This happens because the wcf service configuration is possibly missing from app.config file of the test project.
So add service configuration from app.config of the wcf library project and copy it to your test project.

This can also happen if WCF service is in a dll, and that dll is being invoked from another startup project, so then the config will need to be in app.config of the startup project.

 

How to setup a Command Prompt Here in Windows context menu October 1, 2012

Filed under: Uncategorized — prazjain @ 9:55 pm

I find it really useful to have an option in windows context menu (right click menu) to open command prompt with present directory as its current directory, so no need to open command prompt and change dir.
This is how to set it up, create a .reg file with following contents :

[HKEY_CLASSES_ROOT\Directory\shell\Command]
@="Command &Prompt Here"
[HKEY_CLASSES_ROOT\Directory\shell\Command\command]
@="cmd.exe \\\"%1\\\""

Now double click on the file, this will enter the settings in registry, and you are ready to use the shortcut.

 

Unable to bind parameter id that is oracle guid type raw using ODP.Net September 28, 2012

Filed under: Uncategorized — prazjain @ 7:10 pm
Tags: , , , ,

I have had this problem of trying to save a GUID in oracle as type RAW.

When using oracle version 11.2.0.3, there was no problem saving it the way it was.

Working version for 11.2.0.3 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID).ToArray(), ParameterDirection.Input);

But when we downgraded to oracle version 11.2.0.2, this threw an error, saying unable to bind parameter id (which was a type GUID) to oracle raw types.

Workaround version for 11.2.0.2 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID.ToByteArray()).ToArray(), ParameterDirection.Input);
 

Querying XML with XPath in C#

Filed under: Uncategorized — prazjain @ 6:49 pm
Tags: ,

There have been so many times I had to resort to string comparision on XMLs, but this a handy article to using XPath for querying and even comparing values in XML through C#.

http://support.microsoft.com/kb/308333

http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C

 

BadImageFormatException : An attempt was made to load a program with an incorrect format.

Filed under: Uncategorized — prazjain @ 9:00 am
Tags:

System.InvalidOperationException : Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

This is the error I got when running nunit tests in my 64 bit Windows 7 machine.
I had 64 bit VS 2010, 64 bit Oracle installed.

I had nunit plugin for Visual studio, and I could right click on a test project and select “Test with NUnit”, but this was always loading up nunit-86 which is 32 bit version of nunit.
This meant it was loading up 32 Oracle client lib.

So I added nunit/bin dir to my path and loading nunit.exe from bin/debug of the test project dir and ran the dll in 64 bit nunit exe and lo and behold my tests pass!

 

 
Follow

Get every new post delivered to your Inbox.