Yahoo.Yui.Compressor压缩.NET版

做B/S程序用到很多JS和CSS,为了提高性能一般都采取文件压缩,之前都是用别人在线的压缩工具,但每次只能压缩一个文件比较郁闷,每次发布更新很是麻烦,这里抽空写了一个简单的压缩工具,用的Yahoo.Yui.Compressor。

PS:博客园附件咋添加,先放个外链下载地址吧。http://uniold.com/ScriptPacker.rar(时间长了会删除,不保证长期有效)

先上简图

Yahoo.Yui.Compressor压缩.NET版_第1张图片

Yahoo.Yui.Compressor压缩.NET版_第2张图片Yahoo.Yui.Compressor压缩.NET版_第3张图片

 string s = null;
            if (folderBrowserDialog1.SelectedPath.Length > 0)
            {
                DirectoryInfo d = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                FileInfo[] fi = d.GetFiles();
                foreach (FileInfo f in fi)
                {
                    if (!Directory.Exists(folderBrowserDialog1.SelectedPath + "\\Min\\"))
                    {
                        Directory.CreateDirectory(folderBrowserDialog1.SelectedPath + "\\Min\\");
                    }
                    if (f.Extension.ToLower() == ".js")
                    {
                        s = JavaScriptCompressor.Compress(ReaderText(folderBrowserDialog1.SelectedPath + "\\" + f.Name), false/*是否详细记录*/, true/*是否混淆*/, 
                            false/*保留所有分号*/, false/*禁用优化*/, 99999/*换行的位置*/, System.Text.Encoding.UTF8/*编码格式*/, null);
                        Create(s, folderBrowserDialog1.SelectedPath + "\\Min\\" + f.Name);
                    }
                    else if (f.Extension.ToLower() == ".css")
                    {
                        s = CssCompressor.Compress(ReaderText(folderBrowserDialog1.SelectedPath + "\\" + f.Name), 99999, CssCompressionType.Hybrid);
                        Create(s, folderBrowserDialog1.SelectedPath + "\\Min\\" + f.Name);
                    }
                }
                DialogResult dr = MessageBox.Show("是否打开压缩后的文件夹?", "是否打开", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start("explorer.exe", folderBrowserDialog1.SelectedPath + "\\Min\\");
                }
                return;
            }
            s = richTex.Text;
            if (checkBox1.Checked)
                richTex.Text = CssCompressor.Compress(s, 99999, CssCompressionType.Hybrid);
            else
                richTex.Text = JavaScriptCompressor.Compress(s, false, true, false, false, 99999, Encoding.UTF8, null);

            richTex.Select();

你可能感兴趣的:(compress)