/// <summary>
        /// 压缩对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object CompressData(object data)
        {
            C1ZStreamWriter zipStream = null;

            byte[] buffer = null;

            MemoryStream memStream = new MemoryStream();

            try
            {
                if (data != null)
                {
                    zipStream = new C1ZStreamWriter(memStream, CompressionLevelEnum.BestCompression);
                    new BinaryFormatter().Serialize(zipStream, data);
                    buffer = new byte[memStream.Length];
                    buffer = memStream.ToArray();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (zipStream != null)
                {
                    zipStream.Close();
                }
                if (memStream != null)
                {
                    memStream.Close();
                }
            }
            return(buffer);
        }
예제 #2
0
        // save DataTable object into two files: regular and compressed versions.
        void btnSave_Click(object sender, System.EventArgs e)
        {
            // get data table from grid
            DataTable dt = dataGrid1.DataSource as DataTable;

            Debug.Assert(dt != null);

            // show status
            Cursor          = Cursors.WaitCursor;
            statusBar1.Text = "Serializing data to regular file...";

            // serialize the data set to a regular file
            string          fn = Application.StartupPath + FN_REGULAR;
            FileStream      fs = new FileStream(fn, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(fs, dt);
            long lenRegular = fs.Length;

            fs.Close();

            // show status
            Cursor          = Cursors.WaitCursor;
            statusBar1.Text = "Serializing data to compressed file...";

            // serialize the data set to a compressed file
            fn = Application.StartupPath + FN_COMPRESSED;
            fs = new FileStream(fn, FileMode.Create);
            C1ZStreamWriter compressor = new C1ZStreamWriter(fs);

            bf = new BinaryFormatter();
            bf.Serialize(compressor, dt);
            long lenCompressed = fs.Length;

            fs.Close();

            // show status
            Cursor          = Cursors.Default;
            statusBar1.Text = string.Format(
                "Saved to regular file ({0:#,###} bytes) and " +
                "compressed file ({1:#,###} bytes)",
                lenRegular, lenCompressed);

            // enable load buttons
            btnLoad.Enabled           = true;
            btnLoadCompressed.Enabled = true;
        }
예제 #3
0
        //
        // utilities to compress/expand a string
        //
        public static byte[] CompressString(string str)
        {
            // open memory stream
            MemoryStream ms = new MemoryStream();

            // attach compressor stream to memory stream
            C1ZStreamWriter sw = new C1ZStreamWriter(ms);

            // write data into compressor stream
            StreamWriter writer = new StreamWriter(sw);

            writer.Write(str);

            // flush any pending data
            writer.Flush();

            // return the memory buffer
            // (note: don't use GetBuffer(), it may return an array that is too long)
            return(ms.ToArray());
        }
예제 #4
0
        private void btnCompressData_Click(object sender, System.EventArgs e)
        {
            // open memory stream
            MemoryStream ms = new MemoryStream();

            // attach compressor stream to memory stream
            C1ZStreamWriter sw = new C1ZStreamWriter(ms);

            // attach BinaryWriter to the compressor stream
            BinaryWriter bw = new BinaryWriter(sw);

            // write a bunch of numbers into the stream
            int count = 1000;

            bw.Write(count);
            for (int i = 0; i < count; i++)
            {
                double a = i * Math.PI / 180.0;
                bw.Write(i);
                bw.Write(a);
                bw.Write(Math.Sin(a));
                bw.Write(Math.Cos(a));
            }

            // flush any pending output
            bw.Flush();

            // save the compressed data
            m_CompressedData = ms.ToArray();

            // done
            string msg = string.Format("Generated table with {0} points, saved into {1} bytes", count, m_CompressedData.Length);

            label1.Text = msg;

            // we can now expand it
            btnExpandData.Enabled = true;
        }
예제 #5
0
        //
        // utilities to compress/expand files
        //
        private static bool CompressFile(string dstFile, string srcFile)
        {
            // prepare to compress file
            bool       retval    = true;
            FileStream srcStream = null;
            FileStream dstStream = null;

            try
            {
                // open the files
                srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
                dstStream = new FileStream(dstFile, FileMode.Create, FileAccess.Write);

                // open compressor stream on destination file
                C1ZStreamWriter sw = new C1ZStreamWriter(dstStream);

                // copy source into compressor stream
                StreamCopy(sw, srcStream);
            }
            catch             // exception? tell caller we failed
            {
                retval = false;
            }
            finally             // always close our streams
            {
                if (srcStream != null)
                {
                    srcStream.Close();
                }
                if (dstStream != null)
                {
                    dstStream.Close();
                }
            }

            // done
            return(retval);
        }
예제 #6
0
        public static bool saveCompress(string string_0, Stream stream_0)
        {
            bool       flag   = true;
            FileStream stream = null;

            try
            {
                stream = new FileStream(string_0, FileMode.Open, FileAccess.Write);
                C1ZStreamWriter writer = new C1ZStreamWriter(stream);
                compressWrite(writer, stream_0);
            }
            catch (Exception ex)
            {
                flag = false;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return(flag);
        }
        /// <summary>
        /// Saves the test collection to a stream.
        /// </summary>
        public void SaveTemplate(Stream stream)
        {
            // Compressed to zip
            C1ZStreamWriter zip = new C1ZStreamWriter(stream);

            // Serialize to binary
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(zip, this.MyTests);

            // Close stream
            stream.Close();
        }