private static byte[] BZip2Decompress(Stream data, int expectedLength) { using (MemoryStream output = new MemoryStream(expectedLength)) { #if WITH_DOTNETZIP using (var stream = new Ionic.BZip2.BZip2InputStream(data, false)) { stream.CopyTo(output); } #elif WITH_BZIP2NET using (var stream = new Bzip2.BZip2InputStream(data, false)) { stream.CopyTo(output); } #elif WITH_SHARPCOMPRESS /* * using (var stream = new SharpCompress.Compressors.BZip2.BZip2Stream(data, SharpCompress.Compressors.CompressionMode.Decompress, false)) * { * stream.CopyTo(output); * } */ #elif WITH_SHARPZIPLIB ICSharpCode.SharpZipLib.BZip2.BZip2.Decompress(data, output, true); #else throw new NotImplementedException("Please define which compression library you want to use"); #endif return(output.ToArray()); } }
public static void Handle(Uri fileName, Action <Stream> parse) { if (fileName.IsFile && fileName.AbsoluteUri.EndsWith(".bz") || fileName.IsFile && fileName.AbsoluteUri.EndsWith(".bz2")) { using (FileStream fileStream = new FileStream(fileName.OriginalString, FileMode.Open)) { var inputStream = new Bzip2.BZip2InputStream(fileStream, false); parse(inputStream); } } else if (fileName.IsFile && fileName.AbsoluteUri.EndsWith(".xml")) { using (FileStream fileStream = new FileStream(fileName.OriginalString, FileMode.Open)) { parse(fileStream); } } else { WebRequest request = WebRequest.Create(fileName); Stream stream = request.GetResponse().GetResponseStream(); if (stream != null) { using (StreamReader streamReader = new StreamReader(stream, true)) { if (fileName.AbsoluteUri.EndsWith(".bz") || fileName.AbsoluteUri.EndsWith(".bz2")) { var inputStream = new Bzip2.BZip2InputStream(streamReader.BaseStream, false); parse(inputStream); } else if (fileName.AbsoluteUri.EndsWith(".xml")) { parse(streamReader.BaseStream); } } } } }