Two methods:
1) read the aspx file in one row and process it
2) read the aspx file at one time and process it.
Processing logic:
Replace "with" "(replace two spaces with one space), replace all linefeeds with empty characters (limit compression)
Note:
1) when one row is used to process the ultimate compression, additional lines of controls on the server are required, for example
Copy codeThe Code is as follows:
Line 1: <asp: Label runat = "server"
Line 2: ID = "lb1 "....
Line 3:.../>
In this case, one row of data may cause a problem.
2) In addition, single line comments inline in JS scripts
We recommend that you use "/**/" instead "//"
Result:
The processing of one row is slightly faster than that of one-time processing. For the aspx files of 200 or 300 rows, the difference is within milliseconds. However, as the number of files increases, the gap should be reflected.
One-time read processing can be performed without extreme compression, so that you do not need to consider the problem of server controls and inline single row annotations.
I usually seldom use inline comments, single line comments, and server controls, so the compression effect is very obvious. Generally, the source code of 1/3-lines is compressed less than 50 lines, and the size is reduced by about.
However, this compression effect may be significantly related to whether you use the server-side data list control and how to use it. Generally, I only use repeater.
Copy codeThe Code is as follows:
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>
/// Compress the blank string and line break of the file in the specified path
/// Compression description
/// 1) read all rows using File. ReadAllLines for processing
/// 2) it is best to write the server control in one row. Only the tail label and runat = "server" cross-row processing are performed to start Tag Cross-behavior processing.
/// 3) the file cannot have a single line comment "//"
/// 4) replaced with line breaks and spaces
/// </Summary>
/// <Param name = "filePath"> 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 ("compressing file: {0} \ r \ n starting at {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 ("ended at {0}...", end. ToString ());
Console. WriteLine ("==== time consumed ===\ r \ n {0} \ r \ n", end-start );
}
/// <Summary>
/// Compress the blank string and line break of the file in the specified path
/// Compression description
/// 1) read all text at a time to replace line breaks and spaces
/// 2) No need to deal with the server control line feed problem
/// 3) the compression is incomplete. There may still be A space between the end tag of Element A and the start tag of Element B.
/// </Summary>
/// <Param name = "filePath"> </param>
Public static void CompressAtOneTime (String filePath)
{
Var start = DateTime. Now;
Console. WriteLine ("compressing file: {0} \ r \ n starting at {1}...", filePath,
Start. ToString ());
Var lines = File. ReadAllText (filePath );
File. WriteAllText (filePath, Replace (lines, "\ r \ n ",
""), "", ""), Encoding. GetEncoding ("GB2312 "));
Var end = DateTime. Now;
Console. WriteLine ("ended at {0}...", end. ToString ());
Console. WriteLine ("==== time consumed ===\ r \ n {0} \ r \ n", end-start );
}