Two ways to compress aspx pages to remove excess Spaces

  • 2020-11-26 18:45:45
  • OfStack

Two methods:
1) Read the aspx file on line 1 and process it
2) Read aspx file once and then process it
Processing logic:
Replace "" for" "(replace two Spaces with one space), and replace all newline characters with null characters (extreme compression)
Notes:
1) Line 1 line 1 handling in extreme compression requires additional handling of server control line breaks, for example


Line 1:<asp:Label  runat="server" 
Line 2: ID="lb1"   .... 
Line 3:.../>  

In this case row 1 row 1 processing is going to be a problem

2) In addition, inline single-line comments in the JS script
"/**/" is recommended instead of "//"
Results:

One-line processing is slightly faster than first-order processing, and for two to three hundred lines of the aspx file, the difference is in milliseconds. But as you process the entire project, the gap should show up as the number of files increases.
One-pass read processing eliminates the need for extreme compression so that server controls and inline single-line comments are not a concern.
I rarely use inline comments and single-line comments and server control so compression effect is very obvious, generally 500-600 lines of source compression after less than 50 lines, size reduced by about one-third.
However, this compression effect may have a lot to do with whether and how you use server-side data list controls. I generally use repeater.


public static String Replace(String source,String oldStr,String newStr)
       {
           int count = Regex.Matches(source, oldStr).Count;
           for (int i = 0; i < count; i++)
           {
               source = source.Replace(oldStr, newStr);
           }
           return source;
       }
       /// <summary>
       ///  Compresses the file blank string and newline character of the specified path 
       ///  Compression specified 
       /// 1 ) take File.ReadAllLines Read all rows per 1 The row does the processing 
       /// 2 ) server controls are best written in 1 Inside the line, only the tail tag is done with runat="server" Cross - line processing, start tag cross - behavior processing 
       /// 3 The file cannot have a single line comment "//"
       /// 4 ) replaces newline characters and Spaces 
       /// </summary>
       /// <param name="filePath"> The file path </param>
       public static void CompressLineByLine(String filePath)
       {
           if (!File.Exists(filePath))
           {
               Console.WriteLine(" The file does not exist, check the path {0}", filePath);
               return;
           }
           var start = DateTime.Now;
           Console.WriteLine(" Compress file in progress :{0}\r\n Began in {1}...",
filePath,start.ToString());
           var lines = File.ReadAllLines(filePath,
Encoding.GetEncoding("GB2312"));
           for (int i = 0; i < lines.Length; i++)
           {
               var item = lines[i].Trim();
               if (item.IndexOf("runat=\"server\"") > -1)
                   item += " ";
               item = item.Replace("\r\n", "");
               item = Replace(item, "  ", " ");
               lines[i] = item;
           }
           File.WriteAllText(filePath, string.Join("", lines),
Encoding.GetEncoding("GB2312"));
           var end = DateTime.Now;
           Console.WriteLine(" End in {0}...", end.ToString());
           Console.WriteLine("==== Time consuming ====\r\n{0}\r\n", end - start);
       }
       /// <summary>
       ///  Compresses the file blank string and newline character of the specified path 
       ///  Compression specified 
       /// 1 ) 1 Secondary reading takes out all text replacements and replaces line breaks and whitespace 
       /// 2 ) you don't have to deal with line wrapping for server controls 
       /// 3 ) Not completely compressed, A The element's closing tag and B There may still be between the start tags 1 A blank space 
       /// </summary>
       /// <param name="filePath"></param>
       public static void CompressAtOneTime(String filePath)
       {
           var start = DateTime.Now;
           Console.WriteLine(" Compress file in progress :{0}\r\n Began in {1}...", filePath,
start.ToString());
           var lines = File.ReadAllText(filePath);
           File.WriteAllText(filePath, Replace(Replace(lines, "\r\n",
""),"  "," "), Encoding.GetEncoding("GB2312"));
           var end = DateTime.Now;
           Console.WriteLine(" End in {0}...", end.ToString());
           Console.WriteLine("==== Time consuming ====\r\n{0}\r\n", end - start);
       }


Related articles: