C# – HTML Webseiten komprimieren

Der folgende Quelltext liegt als separate Klasse vor. Die Klasse kann in jedem Projekt eingebunden werden. Die Dateien werden per Drag & Drop auf die Listbox gezogen. Es werden alle doppelten Leerzeichen, Tabulatoren, Zeilenumbrüche und Kommentare aus den Dokumenten entfernt. Bitte testen Sie die Klasse nicht ohne eine Sicherheitskopie Ihrer Daten anzulegen, da trotz ausgiebiger Prüfungen immer Fehler möglich sind.
 
HTML/Webseiten Komprimieren mir C#
 

C# CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Windows.Forms;
using System.IO;
 
class Komprimieren: Form
{
 private System.Windows.Forms.ListBox listBox1;
 private System.Windows.Forms.Button btnKomprimieren;
 private System.Windows.Forms.CheckBox chkSicherheitskopie;
 
 public Komprimieren()
 {
  this.listBox1 = new System.Windows.Forms.ListBox();
  this.btnKomprimieren = new System.Windows.Forms.Button();
  this.chkSicherheitskopie = new System.Windows.Forms.CheckBox();
  this.SuspendLayout();
  //
  // listBox1
  //
  this.listBox1.FormattingEnabled = true;
  this.listBox1.Location = new System.Drawing.Point(12, 12);
  this.listBox1.Name = "listBox1";
  this.listBox1.Size = new System.Drawing.Size(443, 225);
  //
  // btnKomprimieren
  //
  this.btnKomprimieren.Location = new System.Drawing.Point(347, 243);
  this.btnKomprimieren.Name = "btnKomprimieren";
  this.btnKomprimieren.Size = new System.Drawing.Size(110, 23);
  this.btnKomprimieren.Text = "Komprimieren";
  this.btnKomprimieren.Click += new System.EventHandler(this.btnKomprimieren_Click);
  //
  // chkSicherheitskopie
  //
  this.chkSicherheitskopie.AutoSize = true;
  this.chkSicherheitskopie.Location = new System.Drawing.Point(13, 248);
  this.chkSicherheitskopie.Name = "chkSicherheitskopie";
  this.chkSicherheitskopie.Size = new System.Drawing.Size(148, 17);
  this.chkSicherheitskopie.Text = "Sicherheitskopie anlegen.";
  this.chkSicherheitskopie.UseVisualStyleBackColor = true;
  //
  // Form1
  //
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  this.ClientSize = new System.Drawing.Size(469, 273);
  this.Controls.Add(this.chkSicherheitskopie);
  this.Controls.Add(this.btnKomprimieren);
  this.Controls.Add(this.listBox1);
  this.Text = "HTML komprimieren mit www.tsql.de";
  this.ResumeLayout(false);
  this.PerformLayout();
 
 
  listBox1.AllowDrop = true;
  listBox1.DragOver += new DragEventHandler(listBox1OnDragOver);
  listBox1.DragDrop += new DragEventHandler(listBox1OnDragDrop);
 
  Show();
 
 }
 
 void listBox1OnDragOver(object obj, DragEventArgs dea)
 {
  if (dea.Data.GetDataPresent(DataFormats.FileDrop))
  {
   if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
    dea.Effect = DragDropEffects.Move;
  }
 }
 
 void listBox1OnDragDrop(object obj, DragEventArgs dea)
 {
  if (dea.Data.GetDataPresent(DataFormats.FileDrop))
  {
   string[] myFiles = (string[])dea.Data.GetData(DataFormats.FileDrop);
   listBox1.Items.AddRange(myFiles);
  }
 }
 
 void btnKomprimieren_Click(object sender, EventArgs e)
 {
  foreach (object obj in listBox1.Items)
  {
   if (File.Exists(obj.ToString()) == true)
   {
    //Datei lesen
    StreamReader myReader = File.OpenText(obj.ToString());
    String myContent = myReader.ReadToEnd();
    myReader.Close();
 
    //Leerzeichen, Tabulatoren und Zeilenumbrüche entfernen
    myContent = myContent.Replace("\n", "");
    myContent = myContent.Replace("\r", "");
    myContent = myContent.Replace("\t", " ");
    while (myContent.IndexOf(" ") != -1)
    myContent = myContent.Replace(" ", " ");
 
 
    //Kommentare entfernen
    int iStart = 0;
    int iEnde = 0;
    int i = myContent.IndexOf("<!--", iStart);
 
    while (i != -1)
    {
     iStart = i;
     iEnde = myContent.IndexOf("-->", iStart);
     myContent = myContent.Replace(myContent.Substring(iStart, iEnde - iStart + 3), null);
     i = myContent.IndexOf("<!--", iStart);
    }
 
    //Sicherheitskopie anlegen
    if (chkSicherheitskopie.Checked == true)
    File.Copy(obj.ToString(), obj.ToString() + ".bak", true);
 
    //Datei schreiben
    StreamWriter myWriter = File.CreateText(obj.ToString());
    myWriter.Write(myContent);
    myWriter.Close();
   }
  }
 }
}



Tags: ,