namespace sabercompressor { class treenode { public int repeat; public int byt; public treenode left = null, right = null; } class connector : functions { public void connect(byte[] bt, int[] b, int[] r, string paths) { connector saber = new connector(); saber.path = paths; saber.mainfunction(bt, b, r); } } class functions { int c = 0, len = 0; string[] tree = new string[1000000]; public string path; public void getandsort(byte[] bt, int[] b, int[] r) { for (int i = 0; i < bt.Length; i++) { r[Convert.ToInt16(bt[i].ToString())]++; } for (int i = 0; i < 256; i++) { b[i] = i; } int temp; for (int i = 0; i < r.Length; i++) { for (int j = i; j < r.Length; j++) { if (r[i] < r[j]) { temp = r[i]; r[i] = r[j]; r[j] = temp; temp = b[i]; b[i] = b[j]; b[j] = temp; } } } } protected void mainfunction(byte[] bt, int[] b, int[] r) { treenode p = treemaker(r, b); string[] s = new string[256]; for (int i = 0; i < s.Length; i++) { s[i] = "-1"; } string str = ""; s = codefinder(p, s, str); writecodetofile(s); //str = codedfilesstring(bt, s); //System.IO.File.WriteAllText(@"E:\Hofman\s.txt", str); //hofman.Form1 saber = new hofman.Form1(); //saber.richtextboxwriter(str); //compressedsaver(compressor(str)); bytecompressorsaver(bt ,s); treeindex(p, 1); treesaver(); } treenode treemaker(int[] r, int[] b) { treenode p; for (int i = 0; i < r.Length; i++) { if (r[i] != 0) { len++; } } treenode[] a = new treenode[len]; int templen = len; for (int i = 0; i < len; i++) { p = new treenode(); p.repeat = r[i]; p.byt = b[i]; a[i] = p; } treenode t = new treenode(); for (int i = len - 2; i >= 0; i--) { treenode temp = new treenode(); temp.repeat = a[i].repeat + a[i + 1].repeat; temp.left = a[i + 1]; temp.right = a[i]; t = a[i]; a[i] = temp; temp = t; len -= 1; sort(a, len); } len = templen; return a[0]; } void sort(treenode[] a, int len) { treenode temp; for (int i = 0; i < len; i++) { for (int j = i; j < len; j++) { if (a[i].repeat < a[j].repeat) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } string[] codefinder(treenode p, string[] s, string str) { if (p.left == null && p.right == null) { s[p.byt] = str; c++; } else { str = str.Insert(str.Length, "0"); codefinder(p.left, s, str); str = str.Remove(str.Length - 1); str = str.Insert(str.Length, "1"); codefinder(p.right, s, str); str = str.Remove(str.Length - 1); } return s; } void writecodetofile(string[] s) { System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(@"D:\Hofman"); string path = d.FullName + @"\.txt"; System.IO.File.WriteAllText(path, ""); for (int i = 0; i < 256; i++) { if (i != 255) { System.IO.File.AppendAllText(path, i.ToString() + "\r\n" + s[i].ToString() + "\r\n"); } else { System.IO.File.AppendAllText(path, i.ToString() + "\r\n" + s[i].ToString()); } } } string codedfilesstring(byte[] bt, string[] s) { string str = ""; string[] sa = new string[256]; for (int i = 0; i < s.Length; i++) { sa[i] = s[i]; } for (int i = 0; i < bt.Length; i++) { str += sa[Convert.ToInt16(bt[i])]; } return str; } byte[] compressor(string str) { int blen = str.Length / 8 + 1; byte[] bt = new byte[blen]; int[] byt = new int[blen]; int zero = blen * 8 - str.Length; for (int i = 0; i < zero; i++) { str = str.Insert(str.Length - 1, "0"); } for (int i = 0; i < str.Length / 8; i++) { for (int j = 0; j < 8; j++) { string s = Convert.ToString(str[i * 8 + j]); byt[i] += Convert.ToInt16(Convert.ToDouble(s) * (Math.Pow(2, (7 - j) % 8))); } } for (int i = 0; i < byt.Length; i++) { bt[i] = Convert.ToByte(byt[i]); } return bt; } void bytecompressorsaver(byte[] bt, string[] s) { int k=7,l=0,math=128,temp=0; byte[] byt = new byte[1000000000]; for (int i = 0; i < bt.Length; i++) { for (int j=0; j < s[Convert.ToInt16(bt[i])].Length; j++) { if (k > 0) { temp += math * (Convert.ToInt16(s[Convert.ToInt16(bt[i])][j])-48); math /= 2; k--; } else { temp += math * (Convert.ToInt16(s[Convert.ToInt16(bt[i])][j]) - 48); math = 128; k = 7; byt[l] = Convert.ToByte(temp); temp = 0; l++; } } } byte[] bytee = new byte[l]; for (int i = 0; i < l; i++) { bytee[i] = byt[i]; } System.IO.File.WriteAllBytes(path, bytee); } void compressedsaver(byte[] bt) { System.IO.File.WriteAllBytes(path, bt); } void treeindex(treenode p, int index) { if (p.left != null || p.right != null) { tree[index] = "300"; if (p.left != null) { treeindex(p.left, 2 * index); } if (p.right != null) { treeindex(p.right, 2 * index + 1); } } else { tree[index] = Convert.ToString(p.byt); } } void treesaver() { System.IO.File.WriteAllLines(@"D:\Hofman\tree.txt", tree); } } }