Example #1
0
        static string Compress(string fname, bool forceOverwrite)
        {
            var outFname = fname + ".bz2";
            if (File.Exists(outFname))
            {
                if (forceOverwrite)
                    File.Delete(outFname);
                else
                    return null;
            }

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

            return outFname;
        }
Example #2
0
        static string Compress(string fname, bool forceOverwrite)
        {
            var outFname = fname + ".bz2";

            if (File.Exists(outFname))
            {
                if (forceOverwrite)
                {
                    File.Delete(outFname);
                }
                else
                {
                    return(null);
                }
            }

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

            return(outFname);
        }
Example #3
0
        [Timeout(15 * 60*1000)] // 60*1000 = 1min
        public void BZ_Basic()
        {
            TestContext.WriteLine("Creating fodder file.");
            // select a random text string
            var line = TestStrings.ElementAt(this.rnd.Next(0, TestStrings.Count)).Value;
            int n = 4000 + this.rnd.Next(1000); // number of iters
            var fname = "Pippo.txt";
            // emit many many lines into a text file:
            using (var sw = new StreamWriter(File.Create(fname)))
            {
                for (int k=0; k < n; k++)
                {
                    sw.WriteLine(line);
                }
            }
            int crcOriginal = GetCrc(fname);
            int blockSize = 0;

            Func<Stream,Stream>[] getBzStream = {
                new Func<Stream,Stream>( s0 => {
                        var decorator = new Ionic.BZip2.BZip2OutputStream(s0, blockSize);
                        return decorator;
                    }),
                new Func<Stream,Stream>( s1 => {
                        var decorator = new Ionic.BZip2.ParallelBZip2OutputStream(s1, blockSize);
                        return decorator;
                    })
            };

            int[] blockSizes = { 1,2,3,4,5,6,7,8,9 };

            for (int k=0; k < getBzStream.Length; k++)
            {
                for (int m=0; m < blockSizes.Length; m++)
                {
                    blockSize = blockSizes[m];
                    var getStream = getBzStream[k];
                    var root = Path.GetFileNameWithoutExtension(fname);
                    var ext = Path.GetExtension(fname);
                    // compress into bz2
                    var bzFname = String.Format("{0}.{1}.blocksize{2}{3}.bz2",
                                                root,
                                                (k==0)?"SingleThread":"MultiThread",
                                                blockSize, ext);

                    TestContext.WriteLine("Compress cycle ({0},{1})", k,m);
                    TestContext.WriteLine("file {0}", bzFname);
                    using (var fs = File.OpenRead(fname))
                    {
                        using (var output = File.Create(bzFname))
                        {
                            using (var compressor = getStream(output))
                            {
                                CopyStream(fs, compressor);
                            }
                        }
                    }

                    TestContext.WriteLine("Decompress");
                    var decompressedFname = Path.GetFileNameWithoutExtension(bzFname);
                    using (Stream fs = File.OpenRead(bzFname),
                           output = File.Create(decompressedFname),
                           decompressor = new Ionic.BZip2.BZip2InputStream(fs))
                    {
                        CopyStream(decompressor, output);
                    }

                    TestContext.WriteLine("Check CRC");
                    int crcDecompressed = GetCrc(decompressedFname);
                    Assert.AreEqual<int>(crcOriginal, crcDecompressed,
                                         "CRC mismatch {0:X8} != {1:X8}",
                                         crcOriginal, crcDecompressed);
                    TestContext.WriteLine("");

                    // just for the sake of disk space economy:
                    File.Delete(decompressedFname);
                    File.Delete(bzFname);
                }
            }
        }
Example #4
0
        [Timeout(15 * 60 * 1000)] // 60*1000 = 1min
        public void BZ_Basic()
        {
            TestContext.WriteLine("Creating fodder file.");
            // select a random text string
            var line  = TestStrings.ElementAt(this.rnd.Next(0, TestStrings.Count)).Value;
            int n     = 4000 + this.rnd.Next(1000); // number of iters
            var fname = "Pippo.txt";

            // emit many many lines into a text file:
            using (var sw = new StreamWriter(File.Create(fname)))
            {
                for (int k = 0; k < n; k++)
                {
                    sw.WriteLine(line);
                }
            }
            int crcOriginal = GetCrc(fname);
            int blockSize   = 0;

            Func <Stream, Stream>[] getBzStream =
            {
                new Func <Stream, Stream>(s0 => {
                    var decorator = new Ionic.BZip2.BZip2OutputStream(s0,blockSize);
                    return(decorator);
                }),
                new Func <Stream, Stream>(s1 => {
                    var decorator = new Ionic.BZip2.ParallelBZip2OutputStream(s1,blockSize);
                    return(decorator);
                })
            };

            int[] blockSizes = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            for (int k = 0; k < getBzStream.Length; k++)
            {
                for (int m = 0; m < blockSizes.Length; m++)
                {
                    blockSize = blockSizes[m];
                    var getStream = getBzStream[k];
                    var root      = Path.GetFileNameWithoutExtension(fname);
                    var ext       = Path.GetExtension(fname);
                    // compress into bz2
                    var bzFname = String.Format("{0}.{1}.blocksize{2}{3}.bz2",
                                                root,
                                                (k == 0)?"SingleThread":"MultiThread",
                                                blockSize, ext);

                    TestContext.WriteLine("Compress cycle ({0},{1})", k, m);
                    TestContext.WriteLine("file {0}", bzFname);
                    using (var fs = File.OpenRead(fname))
                    {
                        using (var output = File.Create(bzFname))
                        {
                            using (var compressor = getStream(output))
                            {
                                CopyStream(fs, compressor);
                            }
                        }
                    }

                    TestContext.WriteLine("Decompress");
                    var decompressedFname = Path.GetFileNameWithoutExtension(bzFname);
                    using (Stream fs = File.OpenRead(bzFname),
                           output = File.Create(decompressedFname),
                           decompressor = new Ionic.BZip2.BZip2InputStream(fs))
                    {
                        CopyStream(decompressor, output);
                    }

                    TestContext.WriteLine("Check CRC");
                    int crcDecompressed = GetCrc(decompressedFname);
                    Assert.AreEqual <int>(crcOriginal, crcDecompressed,
                                          "CRC mismatch {0:X8} != {1:X8}",
                                          crcOriginal, crcDecompressed);
                    TestContext.WriteLine("");

                    // just for the sake of disk space economy:
                    File.Delete(decompressedFname);
                    File.Delete(bzFname);
                }
            }
        }