/// <summary> /// 批压缩 /// </summary> /// <param name="fileRootPath"></param> /// <param name="fileMidPath"></param> /// <param name="fileName"></param> /// <param name="setting"></param> /// <param name="destPath"></param> /// <returns></returns> public string DoCompressBat(string fileRootPath, string fileMidPath, string fileName, SettingInfo setting, string destPath) { string result = string.Empty; string fullFileName = string.Empty; string strContent = string.Empty; try { if (!fileRootPath.EndsWith("\\")) fileRootPath += "\\"; if (!fileMidPath.EndsWith("\\")) { if (!string.IsNullOrEmpty(fileMidPath)) fileMidPath += "\\"; } if (!destPath.EndsWith("\\")) destPath += "\\"; string name = Path.GetFileNameWithoutExtension(fileName); string ext = Path.GetExtension(fileName).ToLower(); fullFileName = fileRootPath + fileMidPath + fileName; Encoding encodingCssSet = Utils.GetEncoding(setting.CssEncodeName); Encoding encodingJsSet = Utils.GetEncoding(setting.JsEncodeName); Encoding encodingFile = null; strContent = Utils.ReadFile(fullFileName, out encodingFile); if (encodingCssSet == null) { if (encodingFile == null) setting.CssEncodeName = Encoding.UTF8.EncodingName; else setting.CssEncodeName = encodingFile.EncodingName; } else { setting.CssEncodeName = encodingCssSet.EncodingName; } if (encodingJsSet == null) { if (encodingFile == null) setting.JsEncodeName = Encoding.UTF8.EncodingName; else setting.JsEncodeName = encodingFile.EncodingName; } else { setting.JsEncodeName = encodingJsSet.EncodingName; } destPath += fileMidPath; result = DoCompress(fullFileName, strContent, setting, destPath); } catch (Exception ex) { LogHelper.Write("Wind.Compressor.Business.FileBusiness.CompressBat error,fullFileName=" + fullFileName, ex); } return result; }
/// <summary> /// 保存设置项到XML文件 /// </summary> /// <param name="setting"></param> public static void SaveSetting(SettingInfo setting) { try { SerializerHelper.XmlSerializerToFile(setting, Application.StartupPath + "\\" + ConfigName); } catch (Exception ex) { LogHelper.Write("Wind.Compressor.Business.Utils.SaveSetting error", ex); } }
/// <summary> /// 单个文件压缩 /// </summary> /// <param name="fullFileName">文件名,用于自动保存</param> /// <param name="strContent">(仅压缩strContent)</param> /// <param name="setting"></param> /// <param name="destPath"></param> /// <returns></returns> public string DoCompress(string fullFileName, string strContent, SettingInfo setting, string destPath) { string result = string.Empty; try { if (!destPath.EndsWith("\\")) destPath += "\\"; string name = Path.GetFileNameWithoutExtension(fullFileName); string ext = Path.GetExtension(fullFileName).ToLower(); if (ext == ".css") { int CssColumnWidth = 0; if (setting.CssIsColumnWidth) CssColumnWidth = setting.CssColumnWidth; result = CssCompress(strContent, CssColumnWidth, setting.CssCompressionType, setting.CssRemoveComments); if (setting.CssIsAutoSave) { if (setting.CssIsAutoSave) name += setting.CssAutoSaveEndStr; if (setting.CssIsVersion) name += setting.CssVersion; Utils.WriteFile(destPath + name + ext, result, Utils.GetEncoding(setting.CssEncodeName)); } } else if (ext == ".js") { int jsLineBreakPosition = 0; if (setting.JsIsLineBreakPosition) jsLineBreakPosition = setting.JsLineBreakPosition; result = JsCompress(strContent, setting.JsIsVerboseLogging, setting.JsIsObfuscateJavascript, setting.JsPreserveAllSemicolons, setting.JsDisableOptimizations, jsLineBreakPosition, Encoding.UTF8, CultureInfo.CurrentCulture, setting.JsIsEvalIgnored, setting.JsCompressionType ); if (setting.JsIsAutoSave) { if (setting.JsIsAutoSave) name += setting.JsAutoSaveEndStr; if (setting.JsIsVersion) name += setting.JsVersion; Utils.WriteFile(destPath + name + ext, result, Utils.GetEncoding(setting.JsEncodeName)); } } } catch (Exception ex) { LogHelper.Write("Wind.Compressor.Business.BaseWindCompressor.Compress error", ex); } return result; }
private void initSetting() { try { setting = Utils.LoadSetting(); this.txt_Encoding.Text = Encoding.Default.EncodingName; this.txt_CultureInfo.Text = CultureInfo.CurrentCulture.DisplayName; if (setting != null) { //css this.cbb_CssCompressionType.SelectedIndex = (int)setting.CssCompressionType; if (setting.CssRemoveComments) this.chb_CssRemoveComments.Checked = true; if (setting.CssIsColumnWidth) { this.chb_CssColumnWidth.Checked = true; this.num_CssColumnWidth.Enabled = true; } this.num_CssColumnWidth.Value = setting.CssColumnWidth; if (setting.CssIsVersion) { this.chb_CssVesion.Checked = true; this.txt_CssVesion.Enabled = true; } this.txt_CssVesion.Text = setting.CssVersion; if (setting.CssIsAutoSave) { this.chb_CssIsAutoSave.Checked = true; this.txt_CssAutoSaveEndStr.Enabled = true; } this.txt_CssAutoSaveEndStr.Text = setting.CssAutoSaveEndStr; this.cbb_CssEncodeName.Text = setting.CssEncodeName; //js this.cbb_JsCompressionType.SelectedIndex = (int)setting.JsCompressionType; this.txt_Encoding.Text = setting.JsEncodingName; this.txt_CultureInfo.Text = setting.JsThreadCulture; if (setting.JsIsEvalIgnored) this.chb_JsIsEvalIgnored.Checked = true; if (setting.JsIsObfuscateJavascript) this.chb_JsIsObfuscateJavascript.Checked = true; if (setting.JsIsVerboseLogging) this.chb_JsIsVerboseLogging.Checked = true; if (setting.JsPreserveAllSemicolons) this.chb_JsPreserveAllSemicolons.Checked = true; if (setting.JsDisableOptimizations) this.chb_JsDisableOptimizations.Checked = true; if (setting.JsIsLineBreakPosition) { this.chb_JsLineBreakPosition.Checked = true; } this.num_JsLineBreakPosition.Value = setting.JsLineBreakPosition; if (setting.JsIsVersion) { this.chb_JsVesion.Checked = true; this.txt_JsVesion.Enabled = true; } this.txt_JsVesion.Text = setting.JsVersion; if (setting.JsIsAutoSave) { this.chb_JsAutoSave.Checked = true; this.txt_JsAutoSaveEndStr.Enabled = true; } this.txt_JsAutoSaveEndStr.Text = setting.JsAutoSaveEndStr; this.cbb_JsEncodeName.Text = setting.JsEncodeName; //html if (setting.HtmlIsRemoveSpace) { this.chb_Html_RemoveEmptySpace.Checked = true; } if (setting.HtmlIsCompressCssInStyle) { this.chb_Html_Css.Checked = true; } if (setting.HtmlIsRemoveCssComment) { this.chb_Html_CssComment.Checked = true; } this.cbb_HtmlEncodeName.Text = setting.HtmlEncodeName; } } catch (Exception ex) { LogHelper.Write("Wind.Compressor.FormSetting.initSetting error", ex); } }
private void bt_Save_Click(object sender, EventArgs e) { try { SettingInfo setting = new SettingInfo(); //Css setting.CssCompressionType = (CssCompressionType)this.cbb_CssCompressionType.SelectedIndex; if (this.chb_CssRemoveComments.Checked) setting.CssRemoveComments = true; if (this.chb_CssColumnWidth.Checked) setting.CssIsColumnWidth = true; setting.CssColumnWidth = (int)this.num_CssColumnWidth.Value; if (this.chb_CssVesion.Checked) { setting.CssIsVersion = true; setting.CssVersion = this.txt_CssVesion.Text; } if (this.chb_CssIsAutoSave.Checked) { setting.CssIsAutoSave = true; setting.CssAutoSaveEndStr = txt_CssAutoSaveEndStr.Text; } setting.CssEncodeName = cbb_CssEncodeName.Text; //Js setting.JsCompressionType = (JavaScriptCompressionType)this.cbb_JsCompressionType.SelectedIndex; setting.JsEncodingName = this.txt_Encoding.Text; setting.JsThreadCulture = this.txt_CultureInfo.Text; if (this.chb_JsIsEvalIgnored.Checked) setting.JsIsEvalIgnored = true; if (this.chb_JsIsObfuscateJavascript.Checked) setting.JsIsObfuscateJavascript = true; if (this.chb_JsIsVerboseLogging.Checked) setting.JsIsVerboseLogging = true; if (this.chb_JsPreserveAllSemicolons.Checked) setting.JsPreserveAllSemicolons = true; if (this.chb_JsDisableOptimizations.Checked) setting.JsDisableOptimizations = true; if (this.chb_JsLineBreakPosition.Checked) setting.JsIsLineBreakPosition = true; setting.JsLineBreakPosition = (int)this.num_JsLineBreakPosition.Value; if (this.chb_JsVesion.Checked) { setting.JsIsVersion = true; setting.JsVersion = this.txt_JsVesion.Text; } if (this.chb_JsAutoSave.Checked) { setting.JsIsAutoSave = true; setting.JsAutoSaveEndStr = this.txt_JsAutoSaveEndStr.Text; } setting.JsEncodeName = cbb_JsEncodeName.Text; //html if (this.chb_Html_RemoveEmptySpace.Checked) { setting.HtmlIsRemoveSpace = true; } if (this.chb_Html_Css.Checked) { setting.HtmlIsCompressCssInStyle = true; } if (this.chb_Html_CssComment.Checked) { setting.HtmlIsRemoveCssComment = true; } setting.HtmlEncodeName = this.cbb_HtmlEncodeName.Text; Utils.SaveSetting(setting); this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Dispose(); } catch (Exception ex) { LogHelper.Write("Wind.Compressor.FormSetting.bt_Save_Click error", ex); } }
public static string DoCompress(string fileRootPath, string fileMidPath, string fileName, SettingInfo setting, string destPath) { string result = string.Empty; try { if (!fileRootPath.EndsWith("\\")) fileRootPath += "\\"; if (!fileMidPath.EndsWith("\\")) { if (!string.IsNullOrEmpty(fileMidPath)) fileMidPath += "\\"; } if (!destPath.EndsWith("\\")) destPath += "\\"; string fullFileName = fileRootPath + fileMidPath + fileName; Encoding encodingHtmlSet = Utils.GetEncoding(setting.HtmlEncodeName); Encoding encodingFile = null; string strContent = Utils.ReadFile(fullFileName, out encodingFile); if (encodingHtmlSet == null) { if (encodingFile == null) setting.HtmlEncodeName = Encoding.UTF8.EncodingName; else setting.HtmlEncodeName = encodingFile.EncodingName; } else { setting.HtmlEncodeName = encodingHtmlSet.EncodingName; } string fullname = Path.GetFileName(fileName); result = HtmlCompress(strContent, setting.HtmlIsRemoveSpace, setting.HtmlIsCompressCssInStyle, setting.HtmlIsRemoveCssComment); Utils.WriteFile(destPath + fileMidPath + fullname, result, Utils.GetEncoding(setting.HtmlEncodeName)); } catch (Exception ex) { LogHelper.Write("Wind.Compressor.Business.HtmlCompressor.DoCompress error", ex); } return result; }