示例#1
0
        /// <summary>
        ///     Decompresses the specified compressed item.
        /// </summary>
        /// <param name="compressedItem">The compressed item.</param>
        /// <returns>The uncompressed value of the compressed data.</returns>
        /// <externalUnit cref="CompressedItem"/>
        /// <revision revisor="dev06" date="1/11/2009" version="1.0.0.0">
        ///     Member Created
        /// </revision>
        public static string Decompress(CompressedItem compressedItem)
        {
            // Review: should this be in Try/catch block?

            // create return param
            string results = string.Empty;

            // create a memory stream which stores the compressed data
            using (var compressedStream = new MemoryStream(compressedItem.Data))
            {
                // create stream which will unzip data
                using (var zipStream =
                           new InflaterInputStream(compressedStream))
                {
                    // create a byte array to store the uncompressed data
                    byte[] decompressedData =
                        new byte[compressedItem.UncompressedSize];

                    // read the unzipped data to the byte array
                    zipStream.Read(
                        decompressedData, 0, decompressedData.Length);

                    // convert the byte array back into a string
                    results = Encoding.Unicode.GetString(decompressedData);
                }
            }

            return(results);
        }
示例#2
0
        /// <summary>
        ///     Compresses the specified string to compress.
        /// </summary>
        /// <param name="stringToCompress">The string to compress.</param>
        /// <returns>
        ///     A <see cref="CompressedItem"/> containing details about
        ///     the compressed string.
        /// </returns>
        /// <externalUnit cref="CompressedItem"/>
        /// <externalUnit cref="MemoryStream"/>
        /// <externalUnit cref="Encoding"/>
        /// <externalUnit cref="DeflaterOutputStream"/>
        /// <revision revisor="dev06" date="1/11/2009" version="1.0.0.0">
        ///     Member Created
        /// </revision>
        public static CompressedItem Compress(string stringToCompress)
        {
            // create parm for return
            var compressedItem = new CompressedItem();

            // get the bytes of the string
            byte[] uncompressedData =
                Encoding.Unicode.GetBytes(stringToCompress);

            // save the uncompressed size of the byte array to
            // use when decompressing
            compressedItem.UncompressedSize = uncompressedData.Length;

            // create a stream for storing the compressed data
            using (var compressedStream = new MemoryStream())
            {
                // create stream for compressing the data
                using (var zipStream =
                           new DeflaterOutputStream(compressedStream))
                {
                    // write the data to the stream - which will compress it
                    // and put it it the compressed stream object
                    zipStream.Write(
                        uncompressedData, 0, uncompressedData.Length);
                }

                // set the compressed data on the return item
                compressedItem.Data = compressedStream.ToArray();
            }

            return(compressedItem);
        }
        /// <summary>
        ///     Froms the XML.
        /// </summary>
        /// <param name="xml">The XML to serialize</param>
        /// <returns>
        ///     A <see cref="CompressedItem"/> created from the passed-in
        ///     xml string.
        /// </returns>
        /// <externalUnit cref="StringReader"/>
        /// <externalUnit cref="XmlSerializer"/>
        /// <externalUnit cref="CompressedItem"/>
        /// <revision revisor="dev06" date="1/12/2009" version="1.0.4.13">
        ///     Member Created
        /// </revision>
        public static CompressedItem FromXml(string xml)
        {
            CompressedItem item = null;

            // read the file and deserialize
            using (var reader = new StringReader(xml))
            {
                // create xml serializer to transform sample xml
                // into cartridge store
                var serializer = new XmlSerializer(typeof(CompressedItem));

                // recreate the cartridge store object from the xml data
                item = (CompressedItem)serializer.Deserialize(reader);
            }

            return(item);
        }