示例#1
0
        public void CompressTest1()
        {
            for (int i = 0; i < 1000; i++)
            {
                byte[] buffer = new byte[1000];

                var rnd = new Random();
                rnd.NextBytes(buffer);


                var deflated = BZip2.Compress(buffer);
                var inflated = BZip2.Decompress(deflated);

                Assert.AreEqual(buffer.Length, inflated.Length);

                //for (var i = 0; i < buffer.Length; i++)
                //{
                //    Assert.AreEqual(buffer[i], inflated[i]);
                //}

                if (i % 100 == 0)
                {
                    Console.WriteLine(MemInfo.GetCurProcessMem());
                }
            }
        }
 /// <summary>
 /// Compress (based on compression level) all files in given file array to output path. Updates given label and progressbar through referenced form.
 /// </summary>
 /// <param name="fileArray">Array of files that will be compressed.</param>
 /// <param name="outputPath">Output path for compressed files.</param>
 /// <param name="compressionLevel">Level of compression (ranging from 1-9).</param>
 /// <param name="statusLabel">Reference to status label. Can be null.</param>
 /// <param name="progressBar">Reference to progress bar. Can be null.</param>
 /// <param name="form">Reference to the form where statusLabel is located.</param>
 public static void compressFiles(FileInfo[] fileArray, string outputPath, int compressionLevel, Label statusLabel, ProgressBar progressBar, Form form)
 {
     foreach (FileInfo file in fileArray) // file: the file that is going to be compressed
     {
         if (statusLabel != null)
         {
             setStatusText(form, statusLabel, "Compressing " + file.Name + "...");
         }
         FileInfo compressedFile = new FileInfo(outputPath + file.Name + ".bz2"); // compressedFile: Output, compressed file
         using (FileStream fileStream = file.OpenRead())
             using (FileStream compressedFileStream = compressedFile.Create())
                 try
                 {
                     BZip2.Compress(fileStream, compressedFileStream, true, compressionLevel);
                     if (progressBar != null)
                     {
                         PerformStepProgressBar(form, progressBar);
                     }
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message, "Failed @ BZip2.Compress(...)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     setStatusText(form, statusLabel, "Failed on " + file.Name + "!");
                     return;
                 }
     }
     if (statusLabel != null)
     {
         setStatusText(form, statusLabel, "Done!");
     }
 }
示例#3
0
        public void CompressTest()
        {
            for (int i = 0; i < 1000; i++)
            {
                byte[] buf = new byte[10000];
                var    rnd = new Random();
                rnd.NextBytes(buf);

                var input_stream  = new MemoryStream(buf);
                var output_stream = new MemoryStream();
                BZip2.Compress(input_stream, output_stream, true, 9);

                input_stream = new MemoryStream(output_stream.ToArray());
                var decompressed_stream = new MemoryStream();
                BZip2.Decompress(input_stream, decompressed_stream, true);

                var decompressed_bytes = decompressed_stream.ToArray();
                Assert.AreEqual(buf.Length, decompressed_bytes.Length);

                //for (int i = 0; i < buf.Length; i++)
                //{
                //    Assert.AreEqual(buf[i], decompressed_bytes[i]);
                //}
                Thread.Sleep(1);
                Console.WriteLine(MemInfo.GetCurProcessMem());
            }
        }
示例#4
0
        private static bool BzipFile(string Path)
        {
            if (!File.Exists(Path))
            {
                return(false);
            }
            FileInfo fileToBeZipped = new FileInfo(Path);
            FileInfo zipFileName    = new FileInfo(string.Concat(fileToBeZipped.FullName, ".bz2"));

            using (FileStream fileToBeZippedAsStream = fileToBeZipped.OpenRead())
            {
                using (FileStream zipTargetAsStream = zipFileName.Create())
                {
                    try
                    {
                        BZip2.Compress(fileToBeZippedAsStream, zipTargetAsStream, true, 4096);
                        File.Delete(Path);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return(false);
                    }
                }
            }
            return(true);
        }
示例#5
0
        public void recursiveCompress(DirectoryInfo target)
        {
            compressingState = "Compressing: " + target.FullName;

            var files = target.GetFilesByExtensions(allowedExtensions);

            foreach (FileInfo file in files)
            {
                if (cancelCompress)
                {
                    return;
                }

                using (var inFs = file.OpenRead()) {
                    using (var outFs = File.Create(file.FullName + ".bz2"))
                    {
                        BZip2.Compress(inFs, outFs, true, compressLevel);
                    }
                }

                file.Delete();
            }

            var dirs = target.GetDirectories();

            foreach (var dir in dirs)
            {
                if (cancelCompress)
                {
                    return;
                }
                recursiveCompress(dir);
            }
        }
示例#6
0
        static void Main()
        {
            Console.Title           = "Auto Bzip Compressing   Path: [" + worker + "]";
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Scanning..." + Environment.NewLine);

            var fail = 0;
            var succ = 0;
            var logs = new List <string>();
            var todo = new List <string>();

            Directory.GetFileSystemEntries(worker, "*.*", SearchOption.AllDirectories)
            .OrderBy(p => p).ToList().ForEach(f =>
            {
                var fa = File.GetAttributes(f);
                if (fa.HasFlag(FileAttributes.Directory) || f.Contains(myself) || worker.Equals(Path.GetDirectoryName(f)) || ".bz2".Equals(Path.GetExtension(f)))
                {
                    return;
                }

                todo.Add(f);
            });

            Directory.CreateDirectory(Path.Combine(worker, "bzip"));

            todo.ForEach(f =>
            {
                try
                {
                    var t = Path.Combine(worker, "bzip", f.RelativePath() + ".bz2");
                    Directory.CreateDirectory(Path.GetDirectoryName(t));
                    var i = new FileStream(f, FileMode.Open);
                    var o = new FileStream(t, FileMode.Create);
                    BZip2.Compress(i, o, true, 9);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Compressed => {0}", i.Name.RelativePath());
                    succ++;
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Compression failed => {0} => {1}", f.RelativePath(), e.Message);
                    logs.Add(string.Format("Compression failed => {0} => {1}", f.RelativePath(), e.Message));
                    fail++;
                }
                finally
                {
                    Console.Title = "Auto Bzip Compressing   Path: [" + worker + "]" + "     " + succ + "/" + fail + "/" + todo.Count;
                }
            });

            var list = Path.Combine(worker, "autobz2.log");

            File.WriteAllLines(list, logs.ToArray(), new UTF8Encoding(false));

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Total: {0} files | Success: {1} | Failure: {2}", todo.Count, succ, fail);
            Console.ReadKey(true);
        }
示例#7
0
    //使用BZIP压缩<--单个-->文件的方法
    public static bool BZipFile(string sourcefilename, string zipfilename)
    {
        bool blResult;//表示压缩是否成功的返回结果

        //为源文件创建文件流实例,作为压缩方法的输入流参数
        using (FileStream srcFile = File.OpenRead(sourcefilename))
        {
            //为压缩文件创建文件流实例,作为压缩方法的输出流参数
            using (FileStream zipFile = File.Open(zipfilename, FileMode.Create))
            {
                try
                {
                    //以4096字节作为一个块的方式压缩文件
                    BZip2.Compress(srcFile, zipFile, true, 9);
                    blResult = true;
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.Message);
                    blResult = false;
                }
                srcFile.Close(); //关闭源文件流
                zipFile.Close(); //关闭压缩文件流
                return(blResult);
            }
        }
    }
示例#8
0
        public static void SaveLocal(ProjectList allProjects, /* Filter filter, */ string output, string basename, bool compressed = true, bool uncompressed = true)
        {
            // var json_string = allProjects.ToFilteredJson(filter);
            var json_string = allProjects.ToPrettyJson();
            // var currentFile = Path.Combine(cache, "current.json");
            var completeFile   = Path.Combine(output, $"{basename}.json");
            var compressedFile = Path.Combine(output, $"{basename}.json.bz2");

            //uncompressed
            if (uncompressed)
            {
                File.WriteAllText(completeFile, json_string);
            }
            // File.WriteAllText(currentFile, json_string);

            //compressed
            if (compressed)
            {
                using (var fileOutStream = File.OpenWrite(compressedFile)) {
                    byte[]       byteArray = Encoding.ASCII.GetBytes(json_string);
                    MemoryStream stream    = new MemoryStream(byteArray);
                    BZip2.Compress(stream, fileOutStream, true, 4096);
                }
            }
        }
示例#9
0
        private CompressionResult GetBZipResult(string source, Stopwatch stopwatch)
        {
            stopwatch.Restart();

            var inputBytes = Encoding.UTF8.GetBytes(source);

            using (var sourceStream = new MemoryStream(inputBytes))
            using (var targetStream = new MemoryStream())
            {
                BZip2.Compress(sourceStream, targetStream, true, 9);

                var result = Convert.ToBase64String(targetStream.ToArray());

                var data = WebUtility.UrlEncode(result);

                stopwatch.Stop();

                return new CompressionResult
                {
                    Name = "BZip",
                    Data = data,
                    PercentOfSource = 100 - (((decimal)data.Length / source.Length) * 100),
                    Time = stopwatch.ElapsedMilliseconds
                };
            }
        }
示例#10
0
        /// <summary>
        /// Create BZip2 File from given JSon
        /// </summary>
        /// <param name="targetFile"></param>
        /// <param name="jsonStr"></param>
        /// <param name="overrideFile"></param>
        private static void CreateBZip2File(string targetFile, string jsonStr, bool overrideFile = false)
        {
            if (overrideFile || !File.Exists(targetFile))
            {
                Console.WriteLine($"Creating: {targetFile}");

                var    uncompressedData = Encoding.UTF8.GetBytes(jsonStr);
                byte[] inputBytes       = uncompressedData;

                byte[] targetByteArray;
                using (MemoryStream sourceStream = new MemoryStream(inputBytes))
                {
                    using (MemoryStream targetStream = new MemoryStream())
                    {
                        BZip2.Compress(sourceStream, targetStream, true, 4096);

                        targetByteArray = targetStream.ToArray();
                        var file = File.Create(targetFile);
                        file.Write(targetByteArray, 0, targetByteArray.Length);
                        file.Flush();
                        file.Close();
                    }
                }
            }
            else
            {
                Console.WriteLine($"File.Exists.Skipping: {targetFile}");
            }
        }
示例#11
0
        public void Compress(string target)
        {
            FileInfo fileToBeZipped = new FileInfo(target);
            FileInfo zipFileName    = new FileInfo(string.Concat(fileToBeZipped.FullName, ".bz2"));

            if (System.IO.File.Exists(zipFileName.FullName))
            {
                System.IO.File.Delete(zipFileName.FullName);
            }

            using (FileStream fileToBeZippedAsStream = fileToBeZipped.OpenRead())
            {
                using (FileStream zipTargetAsStream = zipFileName.Create())
                {
                    try
                    {
                        BZip2.Compress(fileToBeZippedAsStream, zipTargetAsStream, true, 9);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
示例#12
0
 private void button_buildPackages_Click(object sender, EventArgs e)
 {
     Packages = GetPackages();
     Directory.CreateDirectory(tmpPath);
     using (StreamWriter writer = new StreamWriter(File.Create(tmpPath + @"\Packages")))
     {
         foreach (List <string> package in Packages)
         {
             foreach (string line in package)
             {
                 writer.WriteLine(line);
             }
             writer.WriteLine("");
         }
     }
     using (FileStream packagesFile = new FileInfo(tmpPath + @"\Packages").OpenRead())
     {
         using (FileStream bzfile = File.Create(repoPath + @"\Packages.bz2"))
         {
             try
             {
                 BZip2.Compress(packagesFile, bzfile, true, 4096);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
     Directory.GetFiles(tmpPath).ToList().ForEach(x => File.Delete(x));
     Directory.Delete(tmpPath, true);
 }
示例#13
0
        private byte[] CompressData(CompressionType compressionType, byte[] data)
        {
            if (compressionType == CompressionType.None)
            {
                return(data);
            }

            if (compressionType == CompressionType.Bzip2)
            {
                using var outputStream = new MemoryStream();
                BZip2.Compress(new MemoryStream(data), outputStream, true, 1);
                // Remove BZh1 (note that 1 is the block size/compression level).
                return(outputStream.ToArray().Skip(4).ToArray());
            }

            if (compressionType == CompressionType.Gzip)
            {
                using var outputStream = new MemoryStream();
                GZip.Compress(new MemoryStream(data), outputStream, true, 512, 9);
                return(outputStream.ToArray());
            }

            if (compressionType == CompressionType.Lzma)
            {
                throw new NotImplementedException("LZMA compression is currently not implemented.");
            }

            throw new EncodeException($"Unknown compression type {compressionType}.");
        }
示例#14
0
        // This moves the snapshot from memory to harddisk
        internal void WriteToFile()
        {
            lock (this)
            {
                if (isdisposed)
                {
                    return;
                }
                if (isondisk)
                {
                    return;
                }
                isondisk = true;

                // Compress data
                recstream.Seek(0, SeekOrigin.Begin);
                MemoryStream outstream = new MemoryStream((int)recstream.Length);
                BZip2.Compress(recstream, outstream, 300000);

                // Make temporary file
                filename = General.MakeTempFilename(General.Map.TempPath, "snapshot");

                // Write data to file
                File.WriteAllBytes(filename, outstream.ToArray());

                // Remove data from memory
                recstream.Dispose();
                recstream = null;
                outstream.Dispose();
            }
        }
示例#15
0
    public static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            ShowHelp();
            return(1);
        }

        var parser = new ArgumentParser(args);

        switch (parser.Command)
        {
        case Command.Help:
            ShowHelp();
            break;

        case Command.Compress:
            Console.WriteLine("Compressing {0} to {1} at level {2}", parser.Source, parser.Target, parser.Level);
            BZip2.Compress(File.OpenRead(parser.Source), File.Create(parser.Target), true, parser.Level);
            break;

        case Command.Decompress:
            Console.WriteLine("Decompressing {0} to {1}", parser.Source, parser.Target);
            BZip2.Decompress(File.OpenRead(parser.Source), File.Create(parser.Target), true);
            break;
        }

        return(0);
    }
示例#16
0
文件: Packer.cs 项目: jrfl/exeopt
        /// <summary>
        /// Take an input file and compress the whole thing into a stream
        /// </summary>
        /// <remarks>
        /// This method was lifted from OblivionModManager.
        /// In this case it makes no sense to take the second stream as a parameter:
        /// It should create a MemoryStream in the method then return it.
        /// </remarks>
        private static void Compress(string InFile, Stream OutFile)
        {
            int        blocksize = 9 * BZip2Constants.baseBlockSize;
            FileStream infile    = File.OpenRead(InFile);

            BZip2.Compress(infile, OutFile, blocksize);
            infile.Close();
        }
示例#17
0
        /// <summary>
        /// 制作压缩包(单个文件压缩)
        /// </summary>
        /// <param name="sourceFileName">原文件</param>
        /// <param name="zipFileName">压缩文件</param>
        /// <param name="zipEnum">压缩算法枚举</param>
        /// <returns>压缩成功标志</returns>
        public static bool ZipFile(string srcFileName, string zipFileName, ZipEnum zipEnum, ref Exception e)
        {
            bool     flag = true;
            ZipEntry ent  = null;

            try
            {
                switch (zipEnum)
                {
                case ZipEnum.BZIP2:

                    FileStream inStream  = File.OpenRead(srcFileName);
                    FileStream outStream = File.Open(zipFileName, FileMode.Create);

                    //参数true表示压缩完成后,inStream和outStream连接都释放
                    BZip2.Compress(inStream, outStream, true, 4096);

                    inStream.Close();
                    outStream.Close();


                    break;

                case ZipEnum.GZIP:
                {
                    string strDirecory = zipFileName.Substring(0, zipFileName.LastIndexOf('\\'));
                    if (!Directory.Exists(strDirecory))
                    {
                        Directory.CreateDirectory(strDirecory);
                    }

                    FileStream srcFile = File.OpenRead(srcFileName);

                    ZipOutputStream zipFile  = new ZipOutputStream(File.Open(zipFileName, FileMode.Create));
                    byte[]          fileData = new byte[srcFile.Length];
                    srcFile.Read(fileData, 0, (int)srcFile.Length);
                    ent = new ZipEntry(Path.GetFileName(srcFileName));
                    zipFile.PutNextEntry(ent);
                    zipFile.SetLevel(6);
                    zipFile.Write(fileData, 0, fileData.Length);

                    srcFile.Close();
                    zipFile.Close();
                }

                break;

                default: break;
                }
            }
            catch (Exception ex)
            {
                e    = ex;
                flag = false;
            }
            return(flag);
        }
示例#18
0
        /// <summary>
        /// Compress the specified byte array
        /// </summary>
        /// <param name="req">The byte array to compress</param>
        /// <returns>The compressed byte array</returns>
        public byte[] Compress(byte[] req)
        {
            var sourceStream = new MemoryStream(req);
            var targetStream = new MemoryStream();

            BZip2.Compress(sourceStream, targetStream, true, 9);

            return(targetStream.ToArray());
        }
示例#19
0
 /// <summary>
 /// 压缩bizp字符串
 /// </summary>
 /// <param name="utf8"></param>
 /// <returns></returns>
 public static byte[] BZipCompress(byte[] bs)
 {
     using (var _from = new MemoryStream(bs))
         using (var _to = new MemoryStream())
         {
             BZip2.Compress(_from, _to, true, Deflater.BEST_COMPRESSION);
             return(_to.ToArray());
         }
 }
示例#20
0
 /// <summary>Komprimiert eine ByteArray Daten mit dem BZip2 Algorithmus aus dem SharpZipLib.</summary>
 /// <param name="inData">Das Bytearray mit Daten welches komprimiert werden soll.</param>
 /// <returns>Das komprimierte Bytearray.</returns>
 private byte[] compressData(byte[] inData)
 {
     using (var inStream = new MemoryStream(inData)) {
         using (var outStream = new MemoryStream()) {
             BZip2.Compress(inStream, outStream, 1024);
             return(outStream.ToArray());
         }
     }
 }
示例#21
0
 public void CompressFile(string sourceFilename, string compressedFilename)
 {
     using (var sourceStream = new FileStream(sourceFilename, FileMode.Open))
     {
         using (var compressedStream = new FileStream(compressedFilename, FileMode.OpenOrCreate))
         {
             BZip2.Compress(sourceStream, compressedStream, false, 5);
         }
     }
 }
示例#22
0
        public static byte[] BZip2Compress(byte[] input)
        {
            const int compressionLevel = 9;

            using (MemoryStream inputMemoryStream = new MemoryStream(input))
                using (MemoryStream outputMemoryStream = new MemoryStream())
                {
                    BZip2.Compress(inputMemoryStream, outputMemoryStream, true, compressionLevel);
                    return(outputMemoryStream.ToArray());
                }
        }
示例#23
0
 public static void Main(string[] args)
 {
     if (args[0] == "-d")           // decompress
     {
         BZip2.Decompress(File.OpenRead(args[1]), File.Create(Path.GetFileNameWithoutExtension(args[1])));
     }
     else             // compress
     {
         BZip2.Compress(File.OpenRead(args[0]), File.Create(args[0] + ".bz"), 4096);
     }
 }
示例#24
0
        public override void OnExecute(EtlPipelineContext context)
        {
            Parallel.ForEach(Input, new ParallelOptions {
                MaxDegreeOfParallelism = _degreeOfParallelism
            }, item =>
            {
                var outFileName = item.FilePath + _fileSuffix;
                BZip2.Compress(File.OpenRead(item.FilePath), File.OpenWrite(outFileName), true, _compressionLevel);
                Emit(new NodeOutputWithFilePath(outFileName));
            });

            SignalEnd();
        }
示例#25
0
        public void Compress_Should_Throw_If_Level_Is_Invalid(int compressLevel)
        {
            // Given
            var fileSystem  = Substitute.For <IFileSystem>();
            var environment = Substitute.For <ICakeEnvironment>();
            var log         = Substitute.For <ICakeLog>();
            var zip         = new BZip2(fileSystem, environment, log);

            // Then
            Assert.That(() => zip.Compress(rootPath, outputPath, filePaths, compressLevel),
                        Throws.InstanceOf <ArgumentOutOfRangeException>()
                        .And.Property("ParamName").EqualTo("level"));
        }
示例#26
0
        public void Compress_Should_Throw_If_FilePaths_Are_Null()
        {
            // Given
            var fileSystem  = Substitute.For <IFileSystem>();
            var environment = Substitute.For <ICakeEnvironment>();
            var log         = Substitute.For <ICakeLog>();
            var zip         = new BZip2(fileSystem, environment, log);

            // Then
            Assert.That(() => zip.Compress(rootPath, outputPath, null, level),
                        Throws.InstanceOf <ArgumentNullException>()
                        .And.Property("ParamName").EqualTo("filePaths"));
        }
        /// <summary>
        ///     使用Zip压缩字符串
        /// </summary>
        /// <param name="text">压缩字符串</param>
        /// <returns>压缩后的字符串</returns>
        public static string Compress(this string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(text);
            }
            var inputBytes = Encoding.UTF8.GetBytes(text);

            using var outStream = new MemoryStream();
            BZip2.Compress(new MemoryStream(inputBytes), outStream, true, 2);
            outStream.Close();
            return(Convert.ToBase64String(outStream.ToArray()));
        }
示例#28
0
        public void Compress_Creates_BZip2_File()
        {
            // Given
            var environment = FakeEnvironment.CreateUnixEnvironment();
            var fileSystem  = new FakeFileSystem(environment);

            fileSystem.CreateFile("/Root/file.txt");
            var log = Substitute.For <ICakeLog>();
            var zip = new BZip2(fileSystem, environment, log);

            // Then
            Assert.That(() => zip.Compress(rootPath, outputPath, filePaths, level), Throws.Nothing);
            Assert.That(fileSystem.Exist(outputPath), Is.True);
        }
示例#29
0
        public static void ToCERAS <T>(this T obj, string filename, ref SerializerConfig config)
        {
            var ceras = new CerasSerializer(config);

            byte[] buffer = null;
            ceras.Serialize(obj, ref buffer);
            using (var m1 = new MemoryStream(buffer))
                using (var m2 = new MemoryStream())
                {
                    BZip2.Compress(m1, m2, false, 9);
                    m2.Seek(0, SeekOrigin.Begin);
                    File.WriteAllBytes(filename, m2.ToArray());
                }
        }
示例#30
0
    /// <summary>
    /// Bzip壓縮文件
    /// </summary>
    /// <param name="sourcename">源文件</param>
    /// <param name="zipfilename">压缩后文件</param>
    /// <returns></returns>
    public static bool BzipCompressFile(string sourcename, string zipfilename) //232-89     2.74KB-114字節  38.3KB-412字節
    {
        bool blResult;                                                         //表示压缩是否成功的返回结果
                                                                               //为源文件创建读取文件的流实例
        FileStream srcFile = File.OpenRead(sourcename);

        using (var stream = File.Open(zipfilename, FileMode.Create))
        {
            //压缩级别为1-9,1是最低,9是最高的
            BZip2.Compress(srcFile, stream, true, 9);
            blResult = true;
        }
        return(blResult);
    }