private void btnCompress_Click(object sender, System.EventArgs e) { DateTime dtStartTime; TimeSpan tsElapsed; long lOriginalSize, lCompressedSize, lBytesSaved; // Compress the script JSCompressor jsc = new JSCompressor(chkRemoveLFs.Checked, chkCompressVariables.Checked); // If checked only test variable name compression. The script // itself will not be compressed with the exception of comment // removal. if(chkVarCompTest.Checked) jsc.TestVariableNameCompression = true; dtStartTime = DateTime.Now; this.txtCompressed.Text = jsc.Compress(this.txtScript.Text); tsElapsed = DateTime.Now - dtStartTime; // Display stats lOriginalSize = txtScript.Text.Length; lCompressedSize = txtCompressed.Text.Length; lBytesSaved = lOriginalSize - lCompressedSize; this.lblElapsedTime.Text = String.Format("{0:F2}", tsElapsed.TotalSeconds); this.lblOriginalSize.Text = lOriginalSize.ToString(); this.lblCompressedSize.Text = lCompressedSize.ToString(); this.lblBytesSaved.Text = lBytesSaved.ToString(); this.lblPercentSaved.Text = String.Format("{0:P}", (lBytesSaved < 1) ? 0.0 : (double)lBytesSaved / (double)lOriginalSize); }
static int Main(string[] args) { bool bSuccess = true; string strOpt; Console.WriteLine(@"JavaScript Compressor version {0} Copyright (c) 2003-2006, Eric Woodruff, All rights reserved E-Mail: [email protected]", Assembly.GetExecutingAssembly().GetName().Version.ToString()); if(args.GetUpperBound(0) < 0) { Help(); return 1; } // Create compressor. Default to line feed removal mode. jsc = new JSCompressor(true); foreach(string strArg in args) { strOpt = strArg.ToLower(CultureInfo.InvariantCulture); switch(strOpt) { case "/?": // Show help Help(); break; case "/q": // Shut up about the stats bQuiet = true; break; case "/d": // LF removal enabled (default) jsc.LineFeedRemoval = true; break; case "/k": // LF removal disabled jsc.LineFeedRemoval = false; break; case "/v": // Compress variable names jsc.CompressVariableNames = true; break; case "/t": // Variable name compression test jsc.TestVariableNameCompression = true; break; case "/f": // Force compression in debug mode bForceComp = true; break; case "/r": // Recurse sub-folders on each filespec too bRecurseSubFolders = true; break; default: // If using the VS.NET pre-build command macro // $(ConfigurationName), it may not match "Debug" or // "Release" exactly so accept anything that starts // with those keywords. For the "/Out" option, the // folder to use follows the ":". if(strOpt.Length >= 6 && strOpt.Substring(0, 6) == "/debug") { // Debug build, suppress compression Console.WriteLine("DEBUG BUILD - Script compression disabled unless forced (/f)\n"); bDebug = true; } else if(strOpt.Length >= 8 && strOpt.Substring(0, 8) == "/release") { Console.WriteLine("RELEASE BUILD - Script compression enabled\n"); bDebug = false; } else if(strOpt.Length > 3 && strOpt.Substring(0, 3) == "/o:") { // Set output folder or compress files strOutFolder = strArg.Substring(3); if(strOutFolder.EndsWith("\\") == false) strOutFolder += "\\"; } else bSuccess = CompressFileSpec(strArg); break; } if(!bSuccess) break; } // For debugging #if DEBUG Console.WriteLine("Press ENTER to quit"); Console.ReadLine(); #endif return (bSuccess) ? 0 : 1; }