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
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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads a setting from the configuration.
        /// </summary>
        /// <param name="key">Key of item to retrieve.</param>
        /// <param name="configStream">If supplied, reads configuration file from this stream instead of the default path</param>
        /// <returns>True if successful, otherwise false.</returns>
        public static string ReadSetting(string key, Stream configStream = null)
        {
            try
            {
                key = key.ToLower();
                string json;

                if (configStream != null)
                {
                    using (BinaryReader br = new BinaryReader(configStream))
                        json = Encoding.Unicode.GetString(br.ReadBytes((int)configStream.Length));
                }
                else
                {
                    if (!File.Exists(Paths.CONFIG + ".gz"))
                    {
                        return(null);
                    }
                    //json = File.ReadAllText(Paths.CONFIG);
                    json = GZip.DecompressString(File.ReadAllBytes(Paths.CONFIG + ".gz"));
                }

                var appSettings = JsonConvert.DeserializeObject <SerializableDictionary <string, string> >(json);

                if (!appSettings.ContainsKey(key))
                {
                    return(null);
                }

                string result = appSettings[key];
                Trace.WriteLine(key + " : " + result);
                return(result);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error reading app settings: " + ex.Message);
                return(null);
            }
        }