Having problem in Converting File to Bz2

Jun 6, 2015 at 9:59 PM
Hi,

I have File with 10 line, I'm compressing to Bz2 Format, But when i decompress it, I see generated File has only 9 line. There is data loss of 1.5 line. Here is my Code for compressing to Bz2

Following is the code for compression, I'm converting file to UTF-8 and Bz2 File.
 static string Compress(string sourceFile, bool forceOverwrite)
        {
            var outFname = fname + ".bz2";
           
            if (File.Exists(outFname))
            {
                if (forceOverwrite)
                    File.Delete(outFname);
                else
                    return null;
            }
            long rowCount = 0;
            var output = File.Create(outFname);

            try
            {
                  using (StreamReader reader = new StreamReader(fname))
                {
                    using (var compressor = new Ionic.BZip2.ParallelBZip2OutputStream(output))
                    {
                        StreamWriter writer = new StreamWriter(compressor, System.Text.Encoding.UTF8);
                         string line = "";
                        while ((line = reader.ReadLine()) != null)
                        {
                            writer.WriteLine(line);
                            rowCount++;

                            if (rowCount % 100000 == 0)
                                Console.WriteLine("InProgress..Current Row # " + rowCount.ToString());
                        }

                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (output != null)
                    output = null;
            }

            //     Pump(fs, compressor);

            return outFname;
        }
I tired to change read method like below
                // int charsRead;
                        // char[] buffer = new char[2048];
                        // while ((charsRead = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
                        // {
                        //     writer.Write(buffer, 0, charsRead);
                        //     rowCount++;

                        //     if (rowCount % 100000 == 0)
                        //         Console.WriteLine("InProgress..Current Row # " + rowCount.ToString());
                        // }
For Decompression,
here is the code
 public static string Decompress(string fname, bool forceOverwrite)
        {
            var outFname = Path.GetFileNameWithoutExtension(fname);
            if (File.Exists(outFname))
            {
                if (forceOverwrite)
                    File.Delete(outFname);
                else
                    return null;
            }

            using (Stream fs = File.OpenRead(fname),
                   output = File.Create(outFname),
                   decompressor = new Ionic.BZip2.BZip2InputStream(fs))
                Pump(decompressor, output);

            return outFname;
        }
 private static void Pump(Stream src, Stream dest)
        {
            byte[] buffer = new byte[2048];
            int n;
            while ((n = src.Read(buffer, 0, buffer.Length)) > 0)
                dest.Write(buffer, 0, n);

        }

During debugging, I see readline is reading data correctly, not sure whether it is bug in this library dll in convert actual file to Bz2 or reading from Bz2 . Please let me know reason for this issue