Perbandingan Kinerja Algoritma Kompresi Inverted Elias Gamma dan Capocelli Code Pada File Teks
viii
LAMPIRAN LISTING PROGRAM
1. Form Home
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void compressToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Form2 newMDIChild = new Form2();
Universitas Sumatera Utara
ix
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 newMDIChild = new Form5();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void decompressToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 newMDIChild = new Form3();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 newMDIChild = new Form4();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
Universitas Sumatera Utara
x
}
}
2. Form Kompresi
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string kalimat;
public string nwkalimat;
public Form2()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
Universitas Sumatera Utara
xi
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
textBox1.Text = file;
}
}
public static String readingfiledoc(String kode)
{
Microsoft.Office.Interop.Word.Application word = new
Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref
path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = "";
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
}
string text = totaltext;
return text;
Universitas Sumatera Utara
xii
docs.Close();
word.Quit();
}
private void button2_Click(object sender, EventArgs e)
{
string target = textBox1.Text;
string extension = Path.GetExtension(target);
if (extension == ".docx" | extension == ".txt")
{
string namafolder=Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
//calling Run inverted elias gamma function on inverted elias gamma clas
invertedeliasgamma.runInvertedEliasGamma(target, namafolder, namafile);
//attach process result to inverted elias gamma text
richTextBox1.Text =invertedeliasgamma.kalimatieg;
//calling Run Capocelli function on capocelli class
capocelli.RunCapocelliCompression(target, namafolder, namafile);
//attach process result to capocelli text
nwkalimat = capocelli.kalimatcpl;
richTextBox3.Text = nwkalimat;
}
else {
string msg = "Your file extension are not txt or docx !!!";
kotakpesan(msg);
}
Universitas Sumatera Utara
xiii
}
private void kotakpesan(string msg)
{
string message = msg;
string caption = "Warning";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.OK)
{
// Closes the parent form.
// this.Close();
}
}
}
public class invertedeliasgamma{
private static int[] fr;
private static String cs;
public static String kalimatieg;
private static String[] egc;
private static String[] iegc;
public static String GetCharSet(String st)
{
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
Universitas Sumatera Utara
xiv
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
public static int[] CountFreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] freq = new int[n];
for (int i = 0; i < n; i++)
freq[i] = CountChar(st, charset[i]);
return freq;
}
public static void InsertionSort(int[] freq, String charset)
{
fr = freq;
cs = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (fr[i] > fr[j])
{
char sbtemp = sb[i];
sb[i] = sb[j];
Universitas Sumatera Utara
xv
sb[j] = sbtemp;
}
cs = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
public static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
public static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
Universitas Sumatera Utara
xvi
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
// converts strings into stringbits
public static String StringToStb(String st, String c, String[] s)
{
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static StringBuilder MultiAppend(StringBuilder sb, String s, int t)
Universitas Sumatera Utara
xvii
{
for (int i = 0; i < t; i++)
sb.Append(s);
return sb;
}
private static void InvertedEliasGamma(String st)
{
int[] freq = CountFreq(st);
String charset = GetCharSet(st);
int t = charset.Length;
InsertionSort(freq, charset);
freq = fr;
charset = cs;
egc = new String[t];
iegc = new String[t];
int c = 0;
int ic = 0;
for (int n = 1; n < t + 1; n++)
{
String b = DecToBin(n);
int M = b.Length;
StringBuilder u = new StringBuilder();
u = MultiAppend(u, "0", M - 1);
u.Append("1");
StringBuilder s1 = new StringBuilder();
for (int j = 0; j < M; j++)
{
s1.Append(b[j]);
s1.Append(u[j]);
}
String s2 = s1.ToString();
String s = s2.Substring(1, s2.Length - 1);
Universitas Sumatera Utara
xviii
egc[c] = s;
c++;
StringBuilder s3 = new StringBuilder();
for (int j = 0; j < s.Length; j++)
{
if (s[j] == '0')
s3.Append("1");
else
s3.Append("0");
}
iegc[ic] = s3.ToString();
ic++;
}
}
public static void runInvertedEliasGamma(string target, string folder, string
nama){
string content=Form2.readingfiledoc(target);
InvertedEliasGamma(content);
String stb = StringToStb(content, cs, iegc);
int uncompressed_bits = target.Length * 8;
int compressed_bits = stb.Length;
double CR = (uncompressed_bits * 1.0) / compressed_bits;
double SS = (1.0 - 1.0 / CR) * 100;
kalimatieg = "String = " + content.Length + "\n" + "Char = " + cs + "\n" +
"Compressed_bits = " + compressed_bits + "\n" + "Uncompressed_bits = " +
uncompressed_bits + "\n" + "Compression Ratio = " + CR + " : 1" + "\n" + "Space
Savings = " + SS + " %" + "\n Compressi Success 100%";
string text = stb;
string path = folder + "/"+nama+".ieg";
System.IO.File.WriteAllText(path, text);
Universitas Sumatera Utara
xix
}
}
//end inverted elias gamma class
public class capocelli {
private static int[] vr;
private static String ass;
public static String kalimatcpl;
private static String[] vc;
private static String GetCharSet(String st)
{
StringBuilder s = new StringBuilder();
int n = st.Length;
for (int i = 0; i < n; i++)
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] Countvreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] vreq = new int[n];
Universitas Sumatera Utara
xx
for (int i = 0; i < n; i++)
vreq[i] = CountChar(st, charset[i]);
return vreq;
}
private static void InsertionSort(int[] vreq, String charset)
{
vr = vreq;
ass = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (vr[i] > vr[j])
{
int vrtemp = vr[i];
vr[i] = vr[j];
vr[j] = vrtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
ass = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
Universitas Sumatera Utara
xxi
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
public static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
Universitas Sumatera Utara
xxii
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static StringBuilder Pad(String bin, int bit)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bit - bin.Length; i++)
sb.Append("0");
sb.Append(bin);
return sb;
}
private static void Capocelli(String st, int r)
{
Universitas Sumatera Utara
xxiii
int MAX = 20;
String charset = GetCharSet(st);
int t = charset.Length;
StringBuilder suffix = new StringBuilder();
suffix.Append("0");
for (int i = 0; i < r; i++)
suffix.Append("1");
String[] cc = new String[(int)Math.Pow(2, MAX) + 1];
cc[0] = suffix.ToString();
int prefix_length = 1;
int count = 1;
while (true)
{
for (int i = 0; i < (int)Math.Pow(2, prefix_length); i++)
{
StringBuilder prefix = Pad(DecToBin(i), prefix_length);
if ((prefix.ToString()).Contains(suffix.ToString()) == false)
cc[count++] = (prefix.Append(suffix)).ToString();
}
prefix_length += 1;
if (count > t)
break;
}
vc = new String[t];
for (int i = 0; i < t; i++)
vc[i] = cc[i];
}
public static void RunCapocelliCompression(string target, string folder, string
nama)
{
int r = 3;
Universitas Sumatera Utara
xxiv
string content=Form2.readingfiledoc(target);
int[] vreq =Countvreq(content);
String charset = GetCharSet(content);
int t = charset.Length;
InsertionSort(vreq, charset);
Capocelli(content, r);
String stb = StringToStb(content, ass, vc);
int uncompressed_bits = content.Length * 8;
int compressed_bits = stb.Length;
double CR = (uncompressed_bits * 1.0) / compressed_bits;
double SS = (1.0 - 1.0 / CR) * 100;
kalimatcpl = "String = " + content.Length + "\n" + "Char = " + ass + "\n" + "R
=" + r + "\n" + "Compressed_bits = " + compressed_bits + "\n" +
"Uncompressed_bits = " + uncompressed_bits + "\n" + "Compression Ratio = " + CR
+ " : 1" + "\n" + "Space Savings = " + SS + " %" + "\n Compressi Success 100%";
string text = stb;
string path = folder + "/"+ nama +".cpl";
System.IO.File.WriteAllText(path, text);
}
}
}
3. Form Uncompresi
using System;
using System.IO;
using System.Collections.Generic;
Universitas Sumatera Utara
xxv
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
textBox1.Text = file;
Universitas Sumatera Utara
xxvi
}
}
public static String readingfiledoc(String kode)
{
Microsoft.Office.Interop.Word.Application word = new
Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = kode;
object readOnly = true;
object visible = false;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref
path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = "";
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
}
string text = totaltext;
return text;
docs.Close();
word.Quit();
}
private void button2_Click(object sender, EventArgs e)
{
string target = textBox1.Text;
string extension = Path.GetExtension(target);
Universitas Sumatera Utara
xxvii
if (extension == ".cpl")
{
string namafolder = Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
string target1= namafolder+"/"+namafile+".docx";
//calling Run inverted elias gamma function on inverted elias gamma clas
CapocelliDecompression.RunDecompressionCpl(target,target1, namafolder,
namafile);
//attach process result to inverted elias gamma text
richTextBox2.Text = CapocelliDecompression.kalimat;
richTextBox1.Text = CapocelliDecompression.klm;
}
else if (extension == ".ieg")
{
string namafolder = Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
string target1 = namafolder + "/" + namafile + ".docx";
//calling Run inverted elias gamma function on inverted elias gamma clas
IegDecompression.RunDecompressionIeg(target, target1, namafolder,
namafile);
//attach process result to inverted elias gamma text
richTextBox2.Text = IegDecompression.kalimat;
richTextBox1.Text = IegDecompression.klm;
}
else
{
string msg = "Your file extension are not ieg or cpl !!!";
kotakpesan(msg);
}
}
Universitas Sumatera Utara
xxviii
private void kotakpesan(string msg)
{
string message = msg;
string caption = "Warning";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.OK)
{
// Closes the parent form.
// this.Close();
}
}
}
public class CapocelliDecompression
{
private static int[] vr;
private static String ass;
public static String klm;
public static String kalimat;
private static String[] vc;
private static String GetCharSet(String st)
{
StringBuilder s = new StringBuilder();
Universitas Sumatera Utara
xxix
int n = st.Length;
for (int i = 0; i < n; i++)
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] Countvreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] vreq = new int[n];
for (int i = 0; i < n; i++)
vreq[i] = CountChar(st, charset[i]);
return vreq;
}
private static void InsertionSort(int[] vreq, String charset)
{
vr = vreq;
ass = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
Universitas Sumatera Utara
xxx
if (vr[i] > vr[j])
{
int vrtemp = vr[i];
vr[i] = vr[j];
vr[j] = vrtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
ass = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
Universitas Sumatera Utara
xxxi
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
private static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
Universitas Sumatera Utara
xxxii
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static String Encode(String stb)
{
StringBuilder code = new StringBuilder();
for (int i = 0; i < stb.Length; i += 8)
{
String x = stb.Substring(i, 8);
char y = (char)(BinToDec(x));
code.Append(y);
}
return code.ToString();
}
private static String Decode(String code)
{
StringBuilder sb = new StringBuilder();
int t = code.Length;
for (int i = 0; i < t; i++)
{
char x = code[i];
StringBuilder y = new StringBuilder(DecToBin((int)x));
if (y.Length < 8)
{
int z = 8 - y.Length % 8;
for (int j = 0; j < z; j++)
y.Insert(0, "0");
}
Universitas Sumatera Utara
xxxiii
sb.Append(y);
}
return sb.ToString();
}
private static String Decompress(String stb, String c, String[] s)
{
StringBuilder st = new StringBuilder();
StringBuilder bit = new StringBuilder();
StringBuilder stb2;
int t = stb.Length;
int pad = BinToDec(stb.Substring(t - 8, 8));
stb2 = new StringBuilder(stb.Substring(0, t - pad - 8));
for (int i = 0; i < stb2.Length; i++)
{
bit.Append(stb2[i]);
List list = new List(s);
if (list.Contains(bit.ToString()))
{
int x = list.IndexOf(bit.ToString());
st.Append(c[x]);
bit = new StringBuilder();
}
}
return st.ToString();
}
private static StringBuilder Pad(String bin, int bit)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bit - bin.Length; i++)
sb.Append("0");
sb.Append(bin);
Universitas Sumatera Utara
xxxiv
return sb;
}
private static void Capocelli(String st, int r)
{
int MAX = 20;
String charset = GetCharSet(st);
int t = charset.Length;
StringBuilder suffix = new StringBuilder();
suffix.Append("0");
for (int i = 0; i < r; i++)
suffix.Append("1");
String[] cc = new String[(int)Math.Pow(2, MAX) + 1];
cc[0] = suffix.ToString();
int prefix_length = 1;
int count = 1;
while (true)
{
for (int i = 0; i < (int)Math.Pow(2, prefix_length); i++)
{
StringBuilder prefix = Pad(DecToBin(i), prefix_length);
if ((prefix.ToString()).Contains(suffix.ToString()) == false)
cc[count++] = (prefix.Append(suffix)).ToString();
}
prefix_length += 1;
if (count > t)
break;
}
vc = new String[t];
for (int i = 0; i < t; i++)
vc[i] = cc[i];
}
Universitas Sumatera Utara
xxxv
public static void RunDecompressionCpl(String target,string target1, string
folder, string nama)
{
string content = Form2.readingfiledoc(target);
string content1 = Form2.readingfiledoc(target1);
int[] vreq = Countvreq(content);
String charset = GetCharSet(content);
InsertionSort(vreq, charset);
int r = 3;
Capocelli(content1, r);
String stb = StringToStb(content, ass, vc);
String code = Encode(stb);
String dc = Decode(code);
String ds = Decompress(dc, ass, vc);
kalimat = "Decompressed_String sama dengan String asli? " + (ds ==
content) + "\n"+"Decompressi Success 100%";
klm = content1;
string path = folder + "/CplDecompressi.doc";
System.IO.File.WriteAllText(path, klm);
}
}
public class IegDecompression
{
private static int[] fr;
private static String cs;
private static String[] egc;
Universitas Sumatera Utara
xxxvi
private static String[] iegc;
public static String klm;
public static String kalimat;
private static String GetCharSet(String st)
{
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] CountFreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] freq = new int[n];
for (int i = 0; i < n; i++)
freq[i] = CountChar(st, charset[i]);
return freq;
}
private static void InsertionSort(int[] freq, String charset)
{
fr = freq;
cs = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (fr[i] > fr[j])
Universitas Sumatera Utara
xxxvii
{
int frtemp = fr[i];
fr[i] = fr[j];
fr[j] = frtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
cs = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
Universitas Sumatera Utara
xxxviii
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
private static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
Universitas Sumatera Utara
xxxix
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static String Encode(String stb)
{
StringBuilder code = new StringBuilder();
for (int i = 0; i < stb.Length; i += 8)
{
String x = stb.Substring(i, 8);
char y = (char)(BinToDec(x));
code.Append(y);
}
return code.ToString();
}
private static String Decode(String code)
{
StringBuilder sb = new StringBuilder();
int t = code.Length;
for (int i = 0; i < t; i++)
{
char x = code[i];
StringBuilder y = new StringBuilder(DecToBin((int)x));
if (y.Length < 8)
{
int z = 8 - y.Length % 8;
for (int j = 0; j < z; j++)
y.Insert(0, "0");
}
sb.Append(y);
Universitas Sumatera Utara
xl
}
return sb.ToString();
}
private static String Decompress(String stb, String c, String[] s)
{
StringBuilder st = new StringBuilder();
StringBuilder bit = new StringBuilder();
StringBuilder stb2;
int t = stb.Length;
int pad = BinToDec(stb.Substring(t - 8, 8));
stb2 = new StringBuilder(stb.Substring(0, t - pad - 8));
for (int i = 0; i < stb2.Length; i++)
{
bit.Append(stb2[i]);
List list = new List(s);
if (list.Contains(bit.ToString()))
{
int x = list.IndexOf(bit.ToString());
st.Append(c[x]);
bit = new StringBuilder();
}
}
return st.ToString();
}
private static StringBuilder MultiAppend(StringBuilder sb, String s, int t)
{
for (int i = 0; i < t; i++)
sb.Append(s);
return sb;
}
Universitas Sumatera Utara
xli
private static void InvertedEliasGamma(String st)
{
int[] freq = CountFreq(st);
String charset = GetCharSet(st);
int t = charset.Length;
InsertionSort(freq, charset);
freq = fr;
charset = cs;
egc = new String[t];
iegc = new String[t];
int c = 0;
int ic = 0;
for (int n = 1; n < t + 1; n++)
{
String b = DecToBin(n);
int M = b.Length;
StringBuilder u = new StringBuilder();
u = MultiAppend(u, "0", M - 1);
u.Append("1");
StringBuilder s1 = new StringBuilder();
for (int j = 0; j < M; j++)
{
s1.Append(b[j]);
s1.Append(u[j]);
}
String s2 = s1.ToString();
String s = s2.Substring(1, s2.Length - 1);
egc[c] = s;
c++;
StringBuilder s3 = new StringBuilder();
for (int j = 0; j < s.Length; j++)
{
if (s[j] == '0')
Universitas Sumatera Utara
xlii
s3.Append("1");
else
s3.Append("0");
}
iegc[ic] = s3.ToString();
ic++;
}
}
public static void RunDecompressionIeg(String target, string target1, string
folder, string nama)
{
string content = Form2.readingfiledoc(target);
string content1 = Form2.readingfiledoc(target1);
int[] vreq = CountFreq(content);
String charset = GetCharSet(content);
InsertionSort(vreq, charset);
InvertedEliasGamma(content);
String stb = StringToStb(content, cs, iegc);
String code = Encode(stb);
String dc = Decode(code);
String ds = Decompress(dc, cs, iegc);
kalimat = "Decompressed_String sama dengan String asli? " + (ds ==
content) + "\n" + "Decompressi Success 100%";
klm = content1;
string path = folder + "/IegDecompressi.doc";
System.IO.File.WriteAllText(path, klm);
}
}
}
Universitas Sumatera Utara
LAMPIRAN LISTING PROGRAM
1. Form Home
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void compressToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Form2 newMDIChild = new Form2();
Universitas Sumatera Utara
ix
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 newMDIChild = new Form5();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void decompressToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 newMDIChild = new Form3();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 newMDIChild = new Form4();
newMDIChild.MdiParent = this;
newMDIChild.Show();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
Universitas Sumatera Utara
x
}
}
2. Form Kompresi
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string kalimat;
public string nwkalimat;
public Form2()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
Universitas Sumatera Utara
xi
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
textBox1.Text = file;
}
}
public static String readingfiledoc(String kode)
{
Microsoft.Office.Interop.Word.Application word = new
Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref
path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = "";
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
}
string text = totaltext;
return text;
Universitas Sumatera Utara
xii
docs.Close();
word.Quit();
}
private void button2_Click(object sender, EventArgs e)
{
string target = textBox1.Text;
string extension = Path.GetExtension(target);
if (extension == ".docx" | extension == ".txt")
{
string namafolder=Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
//calling Run inverted elias gamma function on inverted elias gamma clas
invertedeliasgamma.runInvertedEliasGamma(target, namafolder, namafile);
//attach process result to inverted elias gamma text
richTextBox1.Text =invertedeliasgamma.kalimatieg;
//calling Run Capocelli function on capocelli class
capocelli.RunCapocelliCompression(target, namafolder, namafile);
//attach process result to capocelli text
nwkalimat = capocelli.kalimatcpl;
richTextBox3.Text = nwkalimat;
}
else {
string msg = "Your file extension are not txt or docx !!!";
kotakpesan(msg);
}
Universitas Sumatera Utara
xiii
}
private void kotakpesan(string msg)
{
string message = msg;
string caption = "Warning";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.OK)
{
// Closes the parent form.
// this.Close();
}
}
}
public class invertedeliasgamma{
private static int[] fr;
private static String cs;
public static String kalimatieg;
private static String[] egc;
private static String[] iegc;
public static String GetCharSet(String st)
{
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
Universitas Sumatera Utara
xiv
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
public static int[] CountFreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] freq = new int[n];
for (int i = 0; i < n; i++)
freq[i] = CountChar(st, charset[i]);
return freq;
}
public static void InsertionSort(int[] freq, String charset)
{
fr = freq;
cs = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (fr[i] > fr[j])
{
char sbtemp = sb[i];
sb[i] = sb[j];
Universitas Sumatera Utara
xv
sb[j] = sbtemp;
}
cs = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
public static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
public static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
Universitas Sumatera Utara
xvi
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
// converts strings into stringbits
public static String StringToStb(String st, String c, String[] s)
{
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static StringBuilder MultiAppend(StringBuilder sb, String s, int t)
Universitas Sumatera Utara
xvii
{
for (int i = 0; i < t; i++)
sb.Append(s);
return sb;
}
private static void InvertedEliasGamma(String st)
{
int[] freq = CountFreq(st);
String charset = GetCharSet(st);
int t = charset.Length;
InsertionSort(freq, charset);
freq = fr;
charset = cs;
egc = new String[t];
iegc = new String[t];
int c = 0;
int ic = 0;
for (int n = 1; n < t + 1; n++)
{
String b = DecToBin(n);
int M = b.Length;
StringBuilder u = new StringBuilder();
u = MultiAppend(u, "0", M - 1);
u.Append("1");
StringBuilder s1 = new StringBuilder();
for (int j = 0; j < M; j++)
{
s1.Append(b[j]);
s1.Append(u[j]);
}
String s2 = s1.ToString();
String s = s2.Substring(1, s2.Length - 1);
Universitas Sumatera Utara
xviii
egc[c] = s;
c++;
StringBuilder s3 = new StringBuilder();
for (int j = 0; j < s.Length; j++)
{
if (s[j] == '0')
s3.Append("1");
else
s3.Append("0");
}
iegc[ic] = s3.ToString();
ic++;
}
}
public static void runInvertedEliasGamma(string target, string folder, string
nama){
string content=Form2.readingfiledoc(target);
InvertedEliasGamma(content);
String stb = StringToStb(content, cs, iegc);
int uncompressed_bits = target.Length * 8;
int compressed_bits = stb.Length;
double CR = (uncompressed_bits * 1.0) / compressed_bits;
double SS = (1.0 - 1.0 / CR) * 100;
kalimatieg = "String = " + content.Length + "\n" + "Char = " + cs + "\n" +
"Compressed_bits = " + compressed_bits + "\n" + "Uncompressed_bits = " +
uncompressed_bits + "\n" + "Compression Ratio = " + CR + " : 1" + "\n" + "Space
Savings = " + SS + " %" + "\n Compressi Success 100%";
string text = stb;
string path = folder + "/"+nama+".ieg";
System.IO.File.WriteAllText(path, text);
Universitas Sumatera Utara
xix
}
}
//end inverted elias gamma class
public class capocelli {
private static int[] vr;
private static String ass;
public static String kalimatcpl;
private static String[] vc;
private static String GetCharSet(String st)
{
StringBuilder s = new StringBuilder();
int n = st.Length;
for (int i = 0; i < n; i++)
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] Countvreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] vreq = new int[n];
Universitas Sumatera Utara
xx
for (int i = 0; i < n; i++)
vreq[i] = CountChar(st, charset[i]);
return vreq;
}
private static void InsertionSort(int[] vreq, String charset)
{
vr = vreq;
ass = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (vr[i] > vr[j])
{
int vrtemp = vr[i];
vr[i] = vr[j];
vr[j] = vrtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
ass = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
Universitas Sumatera Utara
xxi
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
public static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
Universitas Sumatera Utara
xxii
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static StringBuilder Pad(String bin, int bit)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bit - bin.Length; i++)
sb.Append("0");
sb.Append(bin);
return sb;
}
private static void Capocelli(String st, int r)
{
Universitas Sumatera Utara
xxiii
int MAX = 20;
String charset = GetCharSet(st);
int t = charset.Length;
StringBuilder suffix = new StringBuilder();
suffix.Append("0");
for (int i = 0; i < r; i++)
suffix.Append("1");
String[] cc = new String[(int)Math.Pow(2, MAX) + 1];
cc[0] = suffix.ToString();
int prefix_length = 1;
int count = 1;
while (true)
{
for (int i = 0; i < (int)Math.Pow(2, prefix_length); i++)
{
StringBuilder prefix = Pad(DecToBin(i), prefix_length);
if ((prefix.ToString()).Contains(suffix.ToString()) == false)
cc[count++] = (prefix.Append(suffix)).ToString();
}
prefix_length += 1;
if (count > t)
break;
}
vc = new String[t];
for (int i = 0; i < t; i++)
vc[i] = cc[i];
}
public static void RunCapocelliCompression(string target, string folder, string
nama)
{
int r = 3;
Universitas Sumatera Utara
xxiv
string content=Form2.readingfiledoc(target);
int[] vreq =Countvreq(content);
String charset = GetCharSet(content);
int t = charset.Length;
InsertionSort(vreq, charset);
Capocelli(content, r);
String stb = StringToStb(content, ass, vc);
int uncompressed_bits = content.Length * 8;
int compressed_bits = stb.Length;
double CR = (uncompressed_bits * 1.0) / compressed_bits;
double SS = (1.0 - 1.0 / CR) * 100;
kalimatcpl = "String = " + content.Length + "\n" + "Char = " + ass + "\n" + "R
=" + r + "\n" + "Compressed_bits = " + compressed_bits + "\n" +
"Uncompressed_bits = " + uncompressed_bits + "\n" + "Compression Ratio = " + CR
+ " : 1" + "\n" + "Space Savings = " + SS + " %" + "\n Compressi Success 100%";
string text = stb;
string path = folder + "/"+ nama +".cpl";
System.IO.File.WriteAllText(path, text);
}
}
}
3. Form Uncompresi
using System;
using System.IO;
using System.Collections.Generic;
Universitas Sumatera Utara
xxv
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
textBox1.Text = file;
Universitas Sumatera Utara
xxvi
}
}
public static String readingfiledoc(String kode)
{
Microsoft.Office.Interop.Word.Application word = new
Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = kode;
object readOnly = true;
object visible = false;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref
path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
string totaltext = "";
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
}
string text = totaltext;
return text;
docs.Close();
word.Quit();
}
private void button2_Click(object sender, EventArgs e)
{
string target = textBox1.Text;
string extension = Path.GetExtension(target);
Universitas Sumatera Utara
xxvii
if (extension == ".cpl")
{
string namafolder = Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
string target1= namafolder+"/"+namafile+".docx";
//calling Run inverted elias gamma function on inverted elias gamma clas
CapocelliDecompression.RunDecompressionCpl(target,target1, namafolder,
namafile);
//attach process result to inverted elias gamma text
richTextBox2.Text = CapocelliDecompression.kalimat;
richTextBox1.Text = CapocelliDecompression.klm;
}
else if (extension == ".ieg")
{
string namafolder = Path.GetDirectoryName(target);
string namafile = Path.GetFileNameWithoutExtension(target);
string target1 = namafolder + "/" + namafile + ".docx";
//calling Run inverted elias gamma function on inverted elias gamma clas
IegDecompression.RunDecompressionIeg(target, target1, namafolder,
namafile);
//attach process result to inverted elias gamma text
richTextBox2.Text = IegDecompression.kalimat;
richTextBox1.Text = IegDecompression.klm;
}
else
{
string msg = "Your file extension are not ieg or cpl !!!";
kotakpesan(msg);
}
}
Universitas Sumatera Utara
xxviii
private void kotakpesan(string msg)
{
string message = msg;
string caption = "Warning";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.OK)
{
// Closes the parent form.
// this.Close();
}
}
}
public class CapocelliDecompression
{
private static int[] vr;
private static String ass;
public static String klm;
public static String kalimat;
private static String[] vc;
private static String GetCharSet(String st)
{
StringBuilder s = new StringBuilder();
Universitas Sumatera Utara
xxix
int n = st.Length;
for (int i = 0; i < n; i++)
{
char c = st[i];
if (s.ToString().IndexOf(c) == -1)
s.Append(c);
}
return s.ToString();
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] Countvreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] vreq = new int[n];
for (int i = 0; i < n; i++)
vreq[i] = CountChar(st, charset[i]);
return vreq;
}
private static void InsertionSort(int[] vreq, String charset)
{
vr = vreq;
ass = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
Universitas Sumatera Utara
xxx
if (vr[i] > vr[j])
{
int vrtemp = vr[i];
vr[i] = vr[j];
vr[j] = vrtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
ass = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
Universitas Sumatera Utara
xxxi
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
private static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
Universitas Sumatera Utara
xxxii
int y = 8 - d.Length;
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static String Encode(String stb)
{
StringBuilder code = new StringBuilder();
for (int i = 0; i < stb.Length; i += 8)
{
String x = stb.Substring(i, 8);
char y = (char)(BinToDec(x));
code.Append(y);
}
return code.ToString();
}
private static String Decode(String code)
{
StringBuilder sb = new StringBuilder();
int t = code.Length;
for (int i = 0; i < t; i++)
{
char x = code[i];
StringBuilder y = new StringBuilder(DecToBin((int)x));
if (y.Length < 8)
{
int z = 8 - y.Length % 8;
for (int j = 0; j < z; j++)
y.Insert(0, "0");
}
Universitas Sumatera Utara
xxxiii
sb.Append(y);
}
return sb.ToString();
}
private static String Decompress(String stb, String c, String[] s)
{
StringBuilder st = new StringBuilder();
StringBuilder bit = new StringBuilder();
StringBuilder stb2;
int t = stb.Length;
int pad = BinToDec(stb.Substring(t - 8, 8));
stb2 = new StringBuilder(stb.Substring(0, t - pad - 8));
for (int i = 0; i < stb2.Length; i++)
{
bit.Append(stb2[i]);
List list = new List(s);
if (list.Contains(bit.ToString()))
{
int x = list.IndexOf(bit.ToString());
st.Append(c[x]);
bit = new StringBuilder();
}
}
return st.ToString();
}
private static StringBuilder Pad(String bin, int bit)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bit - bin.Length; i++)
sb.Append("0");
sb.Append(bin);
Universitas Sumatera Utara
xxxiv
return sb;
}
private static void Capocelli(String st, int r)
{
int MAX = 20;
String charset = GetCharSet(st);
int t = charset.Length;
StringBuilder suffix = new StringBuilder();
suffix.Append("0");
for (int i = 0; i < r; i++)
suffix.Append("1");
String[] cc = new String[(int)Math.Pow(2, MAX) + 1];
cc[0] = suffix.ToString();
int prefix_length = 1;
int count = 1;
while (true)
{
for (int i = 0; i < (int)Math.Pow(2, prefix_length); i++)
{
StringBuilder prefix = Pad(DecToBin(i), prefix_length);
if ((prefix.ToString()).Contains(suffix.ToString()) == false)
cc[count++] = (prefix.Append(suffix)).ToString();
}
prefix_length += 1;
if (count > t)
break;
}
vc = new String[t];
for (int i = 0; i < t; i++)
vc[i] = cc[i];
}
Universitas Sumatera Utara
xxxv
public static void RunDecompressionCpl(String target,string target1, string
folder, string nama)
{
string content = Form2.readingfiledoc(target);
string content1 = Form2.readingfiledoc(target1);
int[] vreq = Countvreq(content);
String charset = GetCharSet(content);
InsertionSort(vreq, charset);
int r = 3;
Capocelli(content1, r);
String stb = StringToStb(content, ass, vc);
String code = Encode(stb);
String dc = Decode(code);
String ds = Decompress(dc, ass, vc);
kalimat = "Decompressed_String sama dengan String asli? " + (ds ==
content) + "\n"+"Decompressi Success 100%";
klm = content1;
string path = folder + "/CplDecompressi.doc";
System.IO.File.WriteAllText(path, klm);
}
}
public class IegDecompression
{
private static int[] fr;
private static String cs;
private static String[] egc;
Universitas Sumatera Utara
xxxvi
private static String[] iegc;
public static String klm;
public static String kalimat;
private static String GetCharSet(String st)
{
}
private static int CountChar(String s, char ch)
{
return s.Split(ch).Length - 1;
}
private static int[] CountFreq(String st)
{
String charset = GetCharSet(st);
int n = charset.Length;
int[] freq = new int[n];
for (int i = 0; i < n; i++)
freq[i] = CountChar(st, charset[i]);
return freq;
}
private static void InsertionSort(int[] freq, String charset)
{
fr = freq;
cs = charset;
int n = charset.Length;
StringBuilder sb = new StringBuilder(charset);
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (fr[i] > fr[j])
Universitas Sumatera Utara
xxxvii
{
int frtemp = fr[i];
fr[i] = fr[j];
fr[j] = frtemp;
char sbtemp = sb[i];
sb[i] = sb[j];
sb[j] = sbtemp;
}
cs = sb.ToString();
}
public static String Reverse(String sb)
{
char[] charArray = sb.ToCharArray();
Array.Reverse(charArray);
return new String(charArray);
}
private static String DecToBin(int dec)
{
StringBuilder bin = new StringBuilder();
int r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
while (dec != 0)
{
r = dec % 2;
bin.Insert(0, r);
dec = dec / 2;
}
return bin.ToString();
}
Universitas Sumatera Utara
xxxviii
private static int BinToDec(String bin)
{
StringBuilder sb = new StringBuilder(Reverse(bin));
int n = 1;
int dec = 0;
for (int i = 0; i < sb.Length; i++)
{
dec = dec + (int)(Char.GetNumericValue(sb[i])) * n;
n = n * 2;
}
return dec;
}
// converts strings into stringbits
private static String StringToStb(String st, String c, String[] s)
{
StringBuilder stb = new StringBuilder();
for (int i = 0; i < st.Length; i++)
{
String z = Char.ToString(st[i]);
int k = c.IndexOf(z);
stb.Append(s[k]);
}
int x = stb.Length % 8;
int pad = 0;
if (x != 0)
{
pad = 8 - x;
for (int i = 0; i < pad; i++)
stb.Append("0");
}
String d = DecToBin(pad);
int y = 8 - d.Length;
Universitas Sumatera Utara
xxxix
for (int i = 0; i < y; i++)
stb.Append("0");
stb.Append(d);
return stb.ToString();
}
private static String Encode(String stb)
{
StringBuilder code = new StringBuilder();
for (int i = 0; i < stb.Length; i += 8)
{
String x = stb.Substring(i, 8);
char y = (char)(BinToDec(x));
code.Append(y);
}
return code.ToString();
}
private static String Decode(String code)
{
StringBuilder sb = new StringBuilder();
int t = code.Length;
for (int i = 0; i < t; i++)
{
char x = code[i];
StringBuilder y = new StringBuilder(DecToBin((int)x));
if (y.Length < 8)
{
int z = 8 - y.Length % 8;
for (int j = 0; j < z; j++)
y.Insert(0, "0");
}
sb.Append(y);
Universitas Sumatera Utara
xl
}
return sb.ToString();
}
private static String Decompress(String stb, String c, String[] s)
{
StringBuilder st = new StringBuilder();
StringBuilder bit = new StringBuilder();
StringBuilder stb2;
int t = stb.Length;
int pad = BinToDec(stb.Substring(t - 8, 8));
stb2 = new StringBuilder(stb.Substring(0, t - pad - 8));
for (int i = 0; i < stb2.Length; i++)
{
bit.Append(stb2[i]);
List list = new List(s);
if (list.Contains(bit.ToString()))
{
int x = list.IndexOf(bit.ToString());
st.Append(c[x]);
bit = new StringBuilder();
}
}
return st.ToString();
}
private static StringBuilder MultiAppend(StringBuilder sb, String s, int t)
{
for (int i = 0; i < t; i++)
sb.Append(s);
return sb;
}
Universitas Sumatera Utara
xli
private static void InvertedEliasGamma(String st)
{
int[] freq = CountFreq(st);
String charset = GetCharSet(st);
int t = charset.Length;
InsertionSort(freq, charset);
freq = fr;
charset = cs;
egc = new String[t];
iegc = new String[t];
int c = 0;
int ic = 0;
for (int n = 1; n < t + 1; n++)
{
String b = DecToBin(n);
int M = b.Length;
StringBuilder u = new StringBuilder();
u = MultiAppend(u, "0", M - 1);
u.Append("1");
StringBuilder s1 = new StringBuilder();
for (int j = 0; j < M; j++)
{
s1.Append(b[j]);
s1.Append(u[j]);
}
String s2 = s1.ToString();
String s = s2.Substring(1, s2.Length - 1);
egc[c] = s;
c++;
StringBuilder s3 = new StringBuilder();
for (int j = 0; j < s.Length; j++)
{
if (s[j] == '0')
Universitas Sumatera Utara
xlii
s3.Append("1");
else
s3.Append("0");
}
iegc[ic] = s3.ToString();
ic++;
}
}
public static void RunDecompressionIeg(String target, string target1, string
folder, string nama)
{
string content = Form2.readingfiledoc(target);
string content1 = Form2.readingfiledoc(target1);
int[] vreq = CountFreq(content);
String charset = GetCharSet(content);
InsertionSort(vreq, charset);
InvertedEliasGamma(content);
String stb = StringToStb(content, cs, iegc);
String code = Encode(stb);
String dc = Decode(code);
String ds = Decompress(dc, cs, iegc);
kalimat = "Decompressed_String sama dengan String asli? " + (ds ==
content) + "\n" + "Decompressi Success 100%";
klm = content1;
string path = folder + "/IegDecompressi.doc";
System.IO.File.WriteAllText(path, klm);
}
}
}
Universitas Sumatera Utara