AA2Install.GZip.DecompressString C# (CSharp) Method

DecompressString() public static method

Decompresses a string in the UTF-8 format.
public static DecompressString ( byte bytes ) : string
bytes byte Array of compressed contents.
return string
        public static string DecompressString(byte[] bytes)
        {
            using (var msi = new MemoryStream(bytes))
                using (var mso = new MemoryStream())
                {
                    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                        gs.CopyTo(mso);

                    return Encoding.UTF8.GetString(mso.ToArray());
                }
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Writes a setting to the configuration.
        /// </summary>
        /// <param name="key">Key of item to write.</param>
        /// <param name="value">Value of item to write.</param>
        /// <returns>True if successful, otherwise false.</returns>
        public static bool WriteSetting(string key, string value)
        {
            try
            {
                key = key.ToLower();
                Trace.WriteLine(key + " : " + value);
                string json = "";
                if (File.Exists(Paths.CONFIG + ".gz"))
                {
                    //json = File.ReadAllText(Paths.CONFIG);
                    json = GZip.DecompressString(File.ReadAllBytes(Paths.CONFIG + ".gz"));
                }
                var settings = JsonConvert.DeserializeObject <SerializableDictionary <string, string> >(json);
                if (settings == null)
                {
                    settings = new SerializableDictionary <string, string>();
                }
                settings[key] = value;

                //File.WriteAllText(Paths.CONFIG, JsonConvert.SerializeObject(settings));
                File.WriteAllBytes(Paths.CONFIG + ".gz", GZip.CompressString(JsonConvert.SerializeObject(settings)));
                return(true);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error writing app settings: " + ex.Message);
                return(false);
            }
        }
All Usage Examples Of AA2Install.GZip::DecompressString