Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- //using System.IO.FileStream;
- namespace C_NET_ARCHIVE
- {
- /*Code byte (N) Instructions
- 0 ? N ? 127 Interpret the next N+1 bytes literally.
- N = 128 Reserved
- 129 ? N ? 255 Repeat the next byte 257?N times. */
- class File
- {
- System.IO.FileStream file_to_pack, dest_file;//, temp_file;
- public void Open(string file_to_path, string file_de_path)
- {
- file_to_pack = new System.IO.FileStream(file_to_path, FileMode.Open); //SDELAT ISKL
- dest_file = new System.IO.FileStream(file_de_path, FileMode.Create);
- encode(file_to_pack, dest_file);
- dest_file.Close();
- file_to_pack.Close();
- }
- //ИСПРАВЛЕНО ПОЛНОСТЬЮ
- public void encode(System.IO.FileStream file_to_pack, System.IO.FileStream dest_file)
- {
- BinaryWriter file = new BinaryWriter(dest_file);
- file.Write((byte)128);
- bool do_compress = false;
- #region surprise
- string[] message = { " @\n</|\n)\\|\n\n", " @\n</\\\n)\\ \\\n", " @\n</ \\\n)\\ |", " @\n// \\\n/ )|", "_ @\n /|\n/ )|", "_ @\n /|\n)\\|" };/*|||||||*/ //@"|<-\\--\'", @"\<-\\--\'", @"-<-\\--\'", @"/<-\\--\'" }; //4kadra
- int index = 0;
- #endregion
- for (;file_to_pack.Position < file_to_pack.Length;) //i = 1 tk perviy simvol budet govorit o KOMPR
- {
- #region surprise in action
- if (file_to_pack.Position%400 == 100 && index < message.Length)
- {
- Console.Clear();
- Console.BackgroundColor = ConsoleColor.Blue;
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(message[index]);
- index++;
- }
- else if (index == message.Length)
- index = 0;
- #endregion
- if (file_to_pack.Position != 0)
- file_to_pack.Seek(file_to_pack.Position - 1, SeekOrigin.Begin);
- byte count_comp = 0;
- byte count_exact = 0;
- long offset = file_to_pack.Position;
- byte temp = (byte)file_to_pack.ReadByte(); //position переводится на следующую
- while (temp == (byte)file_to_pack.ReadByte() && file_to_pack.Position - offset < 128 && file_to_pack.Position < file_to_pack.Length) ;
- count_comp = (byte)(file_to_pack.Position - offset);
- do_compress = (count_comp > 1) ? true : false;
- if (do_compress == true)
- {
- if (257 - count_comp > 128)
- {
- byte count = 2;
- count -= count_comp;// count += 1;
- file.Write(count);
- file.Write(temp);
- file.Write((byte)128);
- }
- else
- {
- byte count = 2;
- count_comp -= 129; ;// count += 1;
- file.Write((byte)129);
- file.Write(temp);
- file.Write((byte)128);
- //----------------------
- count_comp = (byte)(count - count_comp);
- file.Write(count_comp);
- file.Write(temp);
- file.Write((byte)128);
- }
- }
- else
- {
- long offset_back = file_to_pack.Position;
- while (file_to_pack.Position < file_to_pack.Length && temp != (byte)file_to_pack.ReadByte())
- {
- if (!(++count_exact < 126)) //ошибка переполнения?
- break;
- }
- file.Write(++count_exact);
- file_to_pack.Seek(offset_back, SeekOrigin.Begin);
- while (file_to_pack.Position < file_to_pack.Length)
- file.Write((byte)file_to_pack.ReadByte());
- file.Write((byte)128);
- }
- }
- }
- }
- class Unpack
- {
- System.IO.FileStream file_to_unpack, dest_file;
- public void Open(string file_to_path, string file_de_path)
- {
- file_to_unpack = new System.IO.FileStream(file_to_path, FileMode.Open); //SDELAT ISKL
- dest_file = new System.IO.FileStream(file_de_path, FileMode.Create);
- decode(file_to_unpack, dest_file);
- }
- //ИСПРАВЛЕНО ПОЛНОСТЬЮ
- public void decode(System.IO.FileStream file_to_unpack, System.IO.FileStream dest_file)
- {
- /*Если в начале группы из 255 байт стоит 1, то блок сжат, если 0, то нет*/
- #region surprise
- string[] message = { " @\n</|\n)\\|\n\n", " @\n</\\\n)\\ \\\n", " @\n</ \\\n)\\ |", " @\n// \\\n/ )|", "_ @\n /|\n/ )|", "_ @\n /|\n)\\|" };/*|||||||*/ //@"|<-\\--\'", @"\<-\\--\'", @"-<-\\--\'", @"/<-\\--\'" }; //4kadra
- int index = 0;
- #endregion
- byte last_byte = 0;
- for (;file_to_unpack.Position < file_to_unpack.Length-1;)
- {
- #region surprise in action
- if (file_to_unpack.Position % 400 == 100 && index < message.Length)
- {
- Console.Clear();
- Console.BackgroundColor = ConsoleColor.DarkMagenta;
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(message[index]);
- index++;
- }
- else if (index == message.Length)
- index = 0;
- #endregion
- byte desi = (byte)file_to_unpack.ReadByte();
- byte count = (byte)file_to_unpack.ReadByte(); //409-я позиция
- if (count == (byte)0)
- count = 1;
- int wtf = 0;
- if (desi == 128) //decompress
- {
- if (count < 128) //literal bytes
- {
- for (int j = 0; j < count; j++)
- dest_file.WriteByte((byte)file_to_unpack.ReadByte());
- }
- else //compressed bytes
- {
- byte temp = (byte)file_to_unpack.ReadByte();
- last_byte = temp;
- for (int j = 0; j < 257 - count; j++)
- dest_file.WriteByte(temp);
- }
- }
- }
- dest_file.WriteByte(last_byte);
- }
- }
- class Program
- {
- static public void makefile(string filename, int weight)
- {
- System.IO.FileStream file_to_make = new System.IO.FileStream(filename, FileMode.Create);
- Random j = new Random();
- BinaryWriter file = new BinaryWriter(file_to_make);
- while(weight != 0)
- {
- for (int i = 0, r = j.Next(32, 123); i < r && weight != 0; i++, weight--)
- file.Write((byte)r);
- //file.Write((byte)j.Next(32, 123));
- //weight--;
- }
- file_to_make.Close();
- }
- static void Main(string[] args)
- {
- if(args[0] == "-make")
- {
- makefile(args[1],int.Parse(args[2]));
- }
- else if(args[0] != "-unpack")
- {
- File file = new File();
- file.Open(args[1],args[2]);
- }
- else
- {
- Unpack file_un = new Unpack();
- file_un.Open(args[1],args[2]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement