Tuesday, December 26, 2006

.net c# zip compression

In case you haven't found out yet, the .net compression library (System.io.compresison) is not the easiest to use. I didnt find it very intuitive at all. After a short time my limited patience was exausted and I went searching for an opensource solution. Easily, the best i found was SharpZipLib. Tar, Zip, GZip, compress, uncompress, source code, the works. A link to its download page is below. Please contribute to them if you use it. Long live the opensource movement.

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

Saturday, December 23, 2006

C# .Net StreamReader Seek

Today’s development problems gave me more trouble than it should have, so I’m going to post the solution. The problem was using the C# StreamReader to pull a small number of characters from a particular position in a very large file. The methods exposed by the class allowed me to read the stream until the desired location was reached. This was taking too long. The first thing I tried was to use its basestream property, then its seek function. This allowed me to do exactly what I wanted, set the file pointer to a position in the file. This lead me to the next problem.

After the StreamReader.basestream.seek() function was implemented, the usual StreamReader functions are being used to read the file. Unfortunately, the StreamReader does a little processing on its own to cache data itself and speed up its expected method of use. I found that the size of its cache is 1024 characters. It reads the file in chunks of this size. So, now I have to seek to the position of the file that I want, less a number. Yada yada yada… this is my finally code. I’m not going to bother describing every variable. I'm sure you're bright enough.



reader = new StreamReader(_fileName);
buffer = new char[_recordLength];
///...stuff...
if(_InvalidInitalFileRecordCount != 0)

{
if(reader.Peek()!=-1)
{
//need to close because the peek moves the position on its own. easier to start over.
reader.Close();
reader = new StreamReader(fileName);
long actualPosition = 0;
if(_firstRecordStartPosition > 0)
{
actualPosition = ((_invalidInitalFileRecordCount * _recordLength)+(( _firstRecordStartPosition-1 )));
}
else
{
actualPosition = ((_nvalidInitalFileRecordCount * _recordLength ));
}
int num = (int)(actualPosition / 1024);
long usePosition = num * 1024;
reader.BaseStream.Seek( usePosition ,System.IO.SeekOrigin.Begin);
buffer = new char[(int)(actualPosition - usePosition)];
numberOfCharactersRead = reader.Read(buffer,0,(int)(actualPosition - usePosition));
}
}
buffer = new char[this.RecordLength];
while(reader.Peek()!=-1)
{
newRecord = new StringBuilder();
fileRecord = string.Empty;
numberOfCharactersRead = reader.Read(buffer,0,_recordLength);
newRecord.Append(buffer);
fileRecord = newRecord.ToString();
actualLineNumber++;
///...other stuff....
}

Friday, December 22, 2006

Replacing my Saab's Key Battery

Figuring out how to replace my Saab's key battery was such a pain i decided to write about it.

In mid 2006 I purchased a 2002 Saab 93 SE. The first few months were uneventful. I had to figure out why the I could never turn off the headlights (daytime running lights are a permanent feature), that's about it. Five or so months later i start to get a 'replace key battery' warning message. After ignoring it for a month or so, I decided to block out a Saturday to get it fixed. At 4pm Sat I take it to the Saab dealership. The service departments closes at noon, so i get a salesman to help. he tells me to just pop the back part off with a screwdriver and pop in a new battery. Easy enough. I pop the back off and make a run to CVS and Radio Shack before i start to figure out that this battery isn't used too often in the USA. I finally find one in a 'battery plus' (i think that is what it was called) a few days later. Pop the battery in, click unlock, and...nothing. I scour the owners manual and find Nothing. I search the Internet for an hour and finally find some obscure page that says to click the unlock button 4 times. Kinda surprisingly, it worked.