<commit_before>#region auto-generated FILE INFORMATION

// ==============================================
// This file is distributed under the MIT License
// ==============================================
// 
// Filename: Compaction.cs
// Version:  2016-10-18 23:33
// 
// Copyright (c) 2016, Si13n7 Developments (r)
// All rights reserved.
// ______________________________________________

#endregion

namespace SilDev
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.IO.Compression;
    using System.Text;

    /// <summary>
    ///     Provides static methods for compressing and decompressing of data.
    /// </summary>
    public static class Compaction
    {
        /// <summary>
        ///     Compresses the specifed <see cref="string"/> value to zip.
        /// </summary>
        /// <param name="text">
        ///     The string to compress.
        /// </param>
        public static byte[] TextToZip(this string text)
        {
            try
            {
                var ba = Encoding.UTF8.GetBytes(text);
                using (var msi = new MemoryStream(ba))
                {
                    var mso = new MemoryStream();
                    using (var gs = new GZipStream(mso, CompressionMode.Compress))
                        msi.CopyTo(gs);
                    ba = mso.ToArray();
                }
                return ba;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///     Decompresses a compressed sequence of bytes back to a <see cref="string"/>
        ///     value.
        /// </summary>
        /// <param name="bytes">
        ///     The sequence of bytes to decompress.
        /// </param>
        public static string TextFromZip(this byte[] bytes)
        {
            try
            {
                string s;
                using (var mso = new MemoryStream())
                {
                    var msi = new MemoryStream(bytes);
                    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                        gs.CopyTo(mso);
                    s = Encoding.UTF8.GetString(mso.ToArray());
                }
                return s;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///     Extracts all the files in the specified zip archive to the specified directory on
        ///     the file system.
        /// </summary>
        /// <param name="srcPath">
        ///     The path of the zip archive to extract (environment variables are accepted).
        /// </param>
        /// <param name="destDir">
        ///     The path to the directory to place the extracted files in (environment variables
        ///     are accepted).
        /// </param>
        /// <param name="delSrcPath">
        ///     true to delete the source archive after extracting; otherwise, false.
        /// </param>
        public static bool Unzip(string srcPath, string destDir, bool delSrcPath = true)
        {
            try
            {
                var src = PathEx.Combine(srcPath);
                if (string.IsNullOrEmpty(src))
                    throw new ArgumentNullException(nameof(src));
                if (!File.Exists(src))
                    throw new FileNotFoundException();
                var dest = PathEx.Combine(destDir);
                if (string.IsNullOrEmpty(dest))
                    throw new ArgumentNullException(nameof(dest));
                using (var zip = ZipFile.OpenRead(src))
                    zip.ExtractToDirectory(dest);
                if (delSrcPath)
                    File.Delete(src);
                return true;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return false;
            }
        }

        #region 7-Zip Helper

        /// <summary>
        ///     ***This is an undocumented class and can be changed or removed in the future
        ///     without futher notice.
        /// </summary>
        public static class Zip7Helper
        {
            public static string ExePath { get; set; } =<commit_msg>Add the functionality to compress a sequence of bytes.<commit_after>#region auto-generated FILE INFORMATION

// ==============================================
// This file is distributed under the MIT License
// ==============================================
// 
// Filename: Compaction.cs
// Version:  2017-05-21 06:59
// 
// Copyright (c) 2017, Si13n7 Developments (r)
// All rights reserved.
// ______________________________________________

#endregion

namespace SilDev
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.IO.Compression;
    using System.Text;

    /// <summary>
    ///     Provides static methods for compressing and decompressing of data.
    /// </summary>
    public static class Compaction
    {
        /// <summary>
        ///     Compresses the specifed sequence of bytes.
        /// </summary>
        /// <param name="bytes">
        ///     The sequence of bytes to compress.
        /// </param>
        public static byte[] Zip(this byte[] bytes)
        {
            try
            {
                using (var msi = new MemoryStream(bytes))
                {
                    var mso = new MemoryStream();
                    using (var gs = new GZipStream(mso, CompressionMode.Compress))
                        msi.CopyTo(gs);
                    bytes = mso.ToArray();
                }
                return bytes;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///     Compresses the specifed <see cref="string"/> value.
        /// </summary>
        /// <param name="text">
        ///     The string to compress.
        /// </param>
        public static byte[] ZipText(this string text)
        {
            try
            {
                var ba = Encoding.UTF8.GetBytes(text);
                ba = ba.Zip();
                return ba;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///     Decompresses a compressed sequence of bytes.
        /// </summary>
        /// <param name="bytes">
        ///     The sequence of bytes to decompress.
        /// </param>
        public static byte[] Unzip(this byte[] bytes)
        {
            try
            {
                byte[] ba;
                using (var mso = new MemoryStream())
                {
                    var msi = new MemoryStream(bytes);
                    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                        gs.CopyTo(mso);
                    ba = mso.ToArray();
                }
                return ba;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///     Extracts all the files in the specified zip archive to the specified directory on
        ///     the file system.
        /// </summary>
        /// <param name="srcPath">
        ///     The path of the zip archive to extract.
        /// </param>
        /// <param name="destDir">
        ///     The path to the directory to place the extracted files in.
        /// </param>
        /// <param name="delSrcPath">
        ///     true to delete the source archive after extracting; otherwise, false.
        /// </param>
        public static bool Unzip(string srcPath, string destDir, bool delSrcPath = true)
        {
            try
            {
                var src = PathEx.Combine(srcPath);
                if (string.IsNullOrEmpty(src))
                    throw new ArgumentNullException(nameof(src));
                if (!File.Exists(src))
                    throw new FileNotFoundException();
                var dest = PathEx.Combine(destDir);
                if (string.IsNullOrEmpty(dest))
                    throw new ArgumentNullException(nameof(dest));
                using (var zip = ZipFile.OpenRead(src))
                    zip.ExtractToDirectory(dest);
                if (delSrcPath)
                    File.Delete(src);
                return true;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return false;
            }
        }

        /// <summary>
        ///     Decompresses a compressed sequence of bytes back to a <see cref="string"/>
        ///     value.
        /// </summary>
        /// <param name="bytes">
        ///     The sequence of bytes to decompress.
        /// </param>
        public static string UnzipText(this byte[] bytes)
        {
            try
            {
                var ba = bytes.Unzip();
                var s = Encoding.UTF8.GetString(ba);
                return s;
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                return null;
            }
        }

        #region 7-Zip Helper

        /// <summary>
        ///     ***This is an undocumented class and can be changed or removed in the future
        ///     without futher notice.
        /// </summary>
        public static class Zip7Helper
        {
            public static string ExePath { get; set; } =
{'fraction_non_alphanumeric': 0.11993313832010029, 'fraction_numerical': 0.004805683242791475, 'mean_word_length': 1.2011956771671648, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1326218', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2276, 'n_words': 684}