Advertisement
Guest User

Just compress

a guest
Oct 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. //using System.IO.FileStream;
  9.  
  10. namespace C_NET_ARCHIVE
  11. {
  12. /*Code byte (N) Instructions
  13. 0 ? N ? 127 Interpret the next N+1 bytes literally.
  14. N = 128 Reserved
  15. 129 ? N ? 255 Repeat the next byte 257?N times. */
  16.  
  17. class File
  18. {
  19. System.IO.FileStream file_to_pack, dest_file;//, temp_file;
  20. public void Open(string file_to_path, string file_de_path)
  21. {
  22. file_to_pack = new System.IO.FileStream(file_to_path, FileMode.Open); //SDELAT ISKL
  23. dest_file = new System.IO.FileStream(file_de_path, FileMode.Create);
  24.  
  25. encode(file_to_pack, dest_file);
  26.  
  27. dest_file.Close();
  28. file_to_pack.Close();
  29. }
  30.  
  31. //ИСПРАВЛЕНО ПОЛНОСТЬЮ
  32. public void encode(System.IO.FileStream file_to_pack, System.IO.FileStream dest_file)
  33. {
  34. BinaryWriter file = new BinaryWriter(dest_file);
  35.  
  36. file.Write((byte)128);
  37. bool do_compress = false;
  38.  
  39. #region surprise
  40. string[] message = { " @\n</|\n)\\|\n\n", " @\n</\\\n)\\ \\\n", " @\n</ \\\n)\\ |", " @\n// \\\n/ )|", "_ @\n /|\n/ )|", "_ @\n /|\n)\\|" };/*|||||||*/ //@"|<-\\--\'", @"\<-\\--\'", @"-<-\\--\'", @"/<-\\--\'" }; //4kadra
  41. int index = 0;
  42. #endregion
  43.  
  44. for (;file_to_pack.Position < file_to_pack.Length;) //i = 1 tk perviy simvol budet govorit o KOMPR
  45. {
  46. #region surprise in action
  47. if (file_to_pack.Position%400 == 100 && index < message.Length)
  48. {
  49. Console.Clear();
  50. Console.BackgroundColor = ConsoleColor.Blue;
  51. Console.ForegroundColor = ConsoleColor.White;
  52. Console.Write(message[index]);
  53.  
  54. index++;
  55. }
  56. else if (index == message.Length)
  57. index = 0;
  58.  
  59. #endregion
  60. if (file_to_pack.Position != 0)
  61. file_to_pack.Seek(file_to_pack.Position - 1, SeekOrigin.Begin);
  62.  
  63. byte count_comp = 0;
  64. byte count_exact = 0;
  65.  
  66. long offset = file_to_pack.Position;
  67. byte temp = (byte)file_to_pack.ReadByte(); //position переводится на следующую
  68.  
  69.  
  70. while (temp == (byte)file_to_pack.ReadByte() && file_to_pack.Position - offset < 128 && file_to_pack.Position < file_to_pack.Length) ;
  71.  
  72. count_comp = (byte)(file_to_pack.Position - offset);
  73.  
  74. do_compress = (count_comp > 1) ? true : false;
  75.  
  76. if (do_compress == true)
  77. {
  78. if (257 - count_comp > 128)
  79. {
  80. byte count = 2;
  81. count -= count_comp;// count += 1;
  82. file.Write(count);
  83. file.Write(temp);
  84. file.Write((byte)128);
  85. }
  86. else
  87. {
  88. byte count = 2;
  89. count_comp -= 129; ;// count += 1;
  90. file.Write((byte)129);
  91. file.Write(temp);
  92. file.Write((byte)128);
  93. //----------------------
  94. count_comp = (byte)(count - count_comp);
  95. file.Write(count_comp);
  96. file.Write(temp);
  97. file.Write((byte)128);
  98. }
  99. }
  100. else
  101. {
  102. long offset_back = file_to_pack.Position;
  103. while (file_to_pack.Position < file_to_pack.Length && temp != (byte)file_to_pack.ReadByte())
  104. {
  105. if (!(++count_exact < 126)) //ошибка переполнения?
  106. break;
  107. }
  108. file.Write(++count_exact);
  109.  
  110. file_to_pack.Seek(offset_back, SeekOrigin.Begin);
  111.  
  112. while (file_to_pack.Position < file_to_pack.Length)
  113. file.Write((byte)file_to_pack.ReadByte());
  114. file.Write((byte)128);
  115. }
  116. }
  117.  
  118. }
  119.  
  120. }
  121. class Unpack
  122. {
  123. System.IO.FileStream file_to_unpack, dest_file;
  124.  
  125. public void Open(string file_to_path, string file_de_path)
  126. {
  127. file_to_unpack = new System.IO.FileStream(file_to_path, FileMode.Open); //SDELAT ISKL
  128. dest_file = new System.IO.FileStream(file_de_path, FileMode.Create);
  129. decode(file_to_unpack, dest_file);
  130. }
  131.  
  132. //ИСПРАВЛЕНО ПОЛНОСТЬЮ
  133. public void decode(System.IO.FileStream file_to_unpack, System.IO.FileStream dest_file)
  134. {
  135. /*Если в начале группы из 255 байт стоит 1, то блок сжат, если 0, то нет*/
  136.  
  137. #region surprise
  138. string[] message = { " @\n</|\n)\\|\n\n", " @\n</\\\n)\\ \\\n", " @\n</ \\\n)\\ |", " @\n// \\\n/ )|", "_ @\n /|\n/ )|", "_ @\n /|\n)\\|" };/*|||||||*/ //@"|<-\\--\'", @"\<-\\--\'", @"-<-\\--\'", @"/<-\\--\'" }; //4kadra
  139. int index = 0;
  140. #endregion
  141.  
  142.  
  143.  
  144. byte last_byte = 0;
  145. for (;file_to_unpack.Position < file_to_unpack.Length-1;)
  146. {
  147. #region surprise in action
  148. if (file_to_unpack.Position % 400 == 100 && index < message.Length)
  149. {
  150. Console.Clear();
  151. Console.BackgroundColor = ConsoleColor.DarkMagenta;
  152. Console.ForegroundColor = ConsoleColor.White;
  153. Console.Write(message[index]);
  154.  
  155. index++;
  156. }
  157. else if (index == message.Length)
  158. index = 0;
  159.  
  160. #endregion
  161.  
  162. byte desi = (byte)file_to_unpack.ReadByte();
  163. byte count = (byte)file_to_unpack.ReadByte(); //409-я позиция
  164. if (count == (byte)0)
  165. count = 1;
  166. int wtf = 0;
  167. if (desi == 128) //decompress
  168. {
  169. if (count < 128) //literal bytes
  170. {
  171. for (int j = 0; j < count; j++)
  172. dest_file.WriteByte((byte)file_to_unpack.ReadByte());
  173. }
  174. else //compressed bytes
  175. {
  176. byte temp = (byte)file_to_unpack.ReadByte();
  177. last_byte = temp;
  178. for (int j = 0; j < 257 - count; j++)
  179. dest_file.WriteByte(temp);
  180. }
  181. }
  182.  
  183. }
  184. dest_file.WriteByte(last_byte);
  185.  
  186.  
  187.  
  188. }
  189. }
  190.  
  191. class Program
  192. {
  193. static public void makefile(string filename, int weight)
  194. {
  195. System.IO.FileStream file_to_make = new System.IO.FileStream(filename, FileMode.Create);
  196. Random j = new Random();
  197.  
  198. BinaryWriter file = new BinaryWriter(file_to_make);
  199.  
  200. while(weight != 0)
  201. {
  202. for (int i = 0, r = j.Next(32, 123); i < r && weight != 0; i++, weight--)
  203. file.Write((byte)r);
  204.  
  205. //file.Write((byte)j.Next(32, 123));
  206. //weight--;
  207. }
  208.  
  209.  
  210. file_to_make.Close();
  211. }
  212. static void Main(string[] args)
  213. {
  214.  
  215. if(args[0] == "-make")
  216. {
  217. makefile(args[1],int.Parse(args[2]));
  218. }
  219. else if(args[0] != "-unpack")
  220. {
  221. File file = new File();
  222. file.Open(args[1],args[2]);
  223.  
  224. }
  225. else
  226. {
  227. Unpack file_un = new Unpack();
  228. file_un.Open(args[1],args[2]);
  229. }
  230.  
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement