Two methods are implemented:
1 read an ASPX file on one line and then process
2) Read the ASPX file once and then process
Processing logic:
Replace with "" (replace two spaces with one space), replace all line breaks with null characters (limit compression)
Precautions:
1 One line to handle the need for additional processing of service-side control line wrapping in case of extreme compression, such as
Copy Code code as follows:
Line 1:<asp:label runat= "Server"
Line 2:id= "LB1" ....
Line 3:.../>
This situation can cause problems in one line of work
2 In addition JS script inline line annotation
It is recommended to use "/**/" instead of "//"
Use result:
One line of processing is slightly faster than one-time processing, and for two hundred or three hundred of rows of aspx files, the gap is at the millisecond level. But for the whole project to deal with, as the number of documents increased, the gap should be reflected.
A one-time read processing can be done without limiting compression, so that the problems with server controls and inline Single-line annotations are not considered.
I usually rarely use inline and single-line comments and server controls so the compression effect is very obvious, generally 500-600 lines of source code compression after less than 50 lines, size reduction of about one-third.
However, this compression effect may be related to whether you use a server-side data listing control and how to use it, and I usually use only repeater.
Copy Code code 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>
Compresses file-blank strings and line breaks for the specified path
Compression description
1 Take File.ReadAllLines to read all rows to do the processing work
2 server control It is best to write in a row, only do the tail tag and runat= "Server" across the line processing, start tag cross behavior processing
3 file cannot have one-line annotation "//"
4 replaces line breaks and spaces
</summary>
<param name= "FilePath" > File path </param>
public static void Compresslinebyline (String filePath)
{
if (! File.exists (FilePath))
{
Console.WriteLine ("File does not exist, check path {0}", FilePath);
Return
}
var start = DateTime.Now;
Console.WriteLine ("Compressing file: {0}\r\n started with {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 ("ends with {0} ...", end.) ToString ());
Console.WriteLine ("= = = Time consuming ====\r\n{0}\r\n", end-start);
}
<summary>
Compresses file-blank strings and line breaks for the specified path
Compression description
1 Read all text at once to replace swap line characters and whitespace
2 do not need to handle the problem of server control wrapping
3 compression is not thorough, a element of the end tag and the B start tag may still have a space between
</summary>
<param name= "FilePath" ></param>
public static void Compressatonetime (String filePath)
{
var start = DateTime.Now;
Console.WriteLine ("Compressing the file: {0}\r\n started 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 ("ends with {0} ...", end.) ToString ());
Console.WriteLine ("= = = Time consuming ====\r\n{0}\r\n", end-start);
}