private static HeaderEntry HeaderEntryForFile(string file) { HeaderEntry he = new HeaderEntry(); he.fullFileName = file; byte[] buffer = new byte[2]; FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 32768); FileStream fsToDispose = fs; { he.fileSize = (uint)fs.Length; if (he.fileSize > 2) { // try to decompress if it looks like a gzip file fs.Read(buffer, 0, 2); if ((buffer[0] == 0x1f) && (buffer[1] == 0x8B)) { fs.Seek(0, SeekOrigin.Begin); AsyncDecompressArgs ada = new AsyncDecompressArgs(he, fs); fsToDispose = null; ++g_jobsStarted; #if !DEBUG try { ThreadPool.QueueUserWorkItem(new WaitCallback(GetDecompressedSize), ada); } catch (Exception) { #endif GetDecompressedSize(ada); #if !DEBUG Interlocked.Increment(ref g_jobsFinished); } #endif } } } if (fsToDispose != null) { fsToDispose.Dispose(); } fs = null; return(he); }
private static void GetDecompressedSize(object o) { AsyncDecompressArgs ada = (AsyncDecompressArgs)o; const int bufSize = 128 * 1024; byte[] decompBuffer = new byte[bufSize]; using (ada.File) using (GZipInputStream gzIn = new GZipInputStream(ada.File)) using (MemoryStream ms = new MemoryStream(bufSize * 2)) { try { StreamUtils.Copy(gzIn, ms, decompBuffer); ada.Entry.isCompressed = true; ada.Entry.decompSize = (uint)ms.Position; Interlocked.Increment(ref g_numCompressed); } catch (Exception) { ; // probably isn't a gzip file afterall } Interlocked.Increment(ref g_jobsFinished); } }