XML压缩和解压

2015-04-16 11:17:27  访问(1374) 赞(0) 踩(0)




        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected System.Text.Encoding GetCurEncoding()
        {
            string strText = comboBox_编码.SelectedText;

            switch (strText)
            {
                case "GB2312":
                    return System.Text.Encoding.GetEncoding("gb2312");
                case "UTF8":
                    return System.Text.Encoding.UTF8;
                case "Unicode":
                    return System.Text.Encoding.Unicode;
                default:
                    return System.Text.Encoding.Default;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_选择参考目录_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            if (fd.ShowDialog() != DialogResult.OK)
                return;

            comboBox_选择文件.Text = fd.FileName;
 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_选择操作目录_Click(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();

            if (fd.ShowDialog() != DialogResult.OK)
                return;

            comboBox_保存文件.Text = fd.FileName;
        }
  
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_执行_Click(object sender, EventArgs e)
        {
            try
            {
                Run();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected void Run()
        {
            string compareDir = comboBox_选择文件.Text.Trim();

            if (compareDir.Length == 0)
            {
                MessageBox.Show("请选择要转换的文件。");
                comboBox_选择文件.Focus();
                comboBox_选择文件.Select();
                return;
            }

            FileInfo sInfo = new FileInfo(compareDir);

            if (!sInfo.Exists)
            {
                MessageBox.Show("要转换的文件不存在。");
                comboBox_选择文件.Focus();
                comboBox_选择文件.Select();
                return;
            }

            string operDir = comboBox_保存文件.Text.Trim();

            if (operDir.Length == 0)
            {
                MessageBox.Show("请选择要保存的文件。");
                comboBox_保存文件.Focus();
                comboBox_保存文件.Select();
                return;
            }

            FileInfo dInfo = new FileInfo(operDir);

            if (dInfo.FullName == sInfo.FullName)
            {
                MessageBox.Show("操作文件和保存文件相同,请重新选择。");
                comboBox_保存文件.Focus();
                comboBox_保存文件.Select();
                return;
            }


            if (dInfo.Exists)
            {
                if (MessageBox.Show("要保存的文件已存在,是否删除源文件", "确认操作", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;

                dInfo.Delete();
            }
            else
            {
                if (!dInfo.Directory.Exists)
                    dInfo.Directory.Create();
            }

            System.Text.Encoding en = GetCurEncoding();

            if (radioButton_转换.Checked)
            {
                string sData = File.ReadAllText(sInfo.FullName, en);

                WriteString(dInfo.FullName, sData, en);
            }
            else
            {
                RebackFile(sInfo.FullName, dInfo.FullName);
            }

            DialogResult dg = MessageBox.Show
                 (
                        "保存成功,是否复制文件或打开文件所在目录(点No)?",
                        "选择操作",
                        MessageBoxButtons.YesNoCancel
                 );

            if (dg == DialogResult.Cancel)
                return;

            if (dg == DialogResult.No)
            {

                System.Diagnostics.Process.Start("explorer.exe", dInfo.Directory.FullName);

                return;
            }

            if (dg == DialogResult.Yes)
            {

                System.Collections.Specialized.StringCollection stringCollectionValue
                    = new System.Collections.Specialized.StringCollection();

                stringCollectionValue.Add(dInfo.FullName);

                Clipboard.SetFileDropList(stringCollectionValue);
            }

        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="en"></param>
        /// <returns></returns>
        public static long WriteString
            (
                string fileName,
                string data,
                System.Text.Encoding en
            )
        {
            long lResult = 0;
            // byte[] bs = Encoding.UTF8.GetBytes(data);
            byte[] bs = en.GetBytes(data);

            MemoryStream ms = new MemoryStream();
            //创建文件流
            FileStream fs = new FileStream(fileName, FileMode.Create);
            try
            {
                DeflateStream compressedzipStream = null;
                try
                {
                    compressedzipStream = new DeflateStream(ms, CompressionMode.Compress, true);
                    compressedzipStream.Write(bs, 0, bs.Length);//把bs中的数据进行二进制压缩后写入到ms中。
                    lResult = ms.Length;
                }
                finally
                {
                    if (null != compressedzipStream)
                        compressedzipStream.Close();
                }
                byte[] bl = BitConverter.GetBytes((long)bs.Length);//把没有压缩的数据长度保存下来,以在还原时使用。
                fs.Write(bl, 0, bl.Length);
                ms.WriteTo(fs);
                fs.Flush();
            }
            finally
            {
                fs.Close();
                ms.Close();
            }
            return lResult;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sFullName"></param>
        /// <param name="dFullName"></param>
        public void RebackFile(string sFullName, string dFullName)
        {
            byte[] td = LoadBytes(sFullName);

            File.WriteAllBytes(dFullName, td);
        }


        /// <summary>
        /// 解压,返回byte数组
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private   byte[] LoadBytes(string fileName)
        {
            //源数据长度的byte数据
            byte[] bs;
            long nSize;//bs转换后的实际值
            //结果
            byte[] decompressBuffer;
            FileStream fs = new FileStream(fileName, FileMode.Open);
            try
            {
                //读入源数据的字节长度
                byte[] bl = new byte[8];
                fs.Read(bl, 0, 8);
                nSize = BitConverter.ToInt64(bl, 0);
                //把文件流中字符的二进制数据写入到bs数组中
                bs = new byte[fs.Length - 8];
                fs.Read(bs, 0, (int)fs.Length - 8);
            }
            finally
            {
                fs.Close();
            }

            //bs数据写入到ms流中
            MemoryStream ms = new MemoryStream();
            DeflateStream DecompressedzipStream = null;
            try
            {
                ms.Write(bs, 0, bs.Length);
                ms.Position = 0;
                //解压
                DecompressedzipStream = new DeflateStream(ms, CompressionMode.Decompress);
                DecompressedzipStream.Flush();
                //解压后的数据
                decompressBuffer = new byte[nSize];
                DecompressedzipStream.Read(decompressBuffer, 0, decompressBuffer.Length);
            }
            finally
            {
                if (null != DecompressedzipStream)
                    DecompressedzipStream.Close();
                if (null != ms)
                    ms.Close();
            }
            return decompressBuffer;
        }
 



标签:XML压缩和解压 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)