示例#1
0
文件: Archive.cs 项目: mihan1991/GAF
 // Convert an object to a byte array
 private byte[] VolumesToByteArray(Dict obj)
 {
     if (obj == null)
         return null;
     BinaryFormatter bf = new BinaryFormatter();
     MemoryStream ms = new MemoryStream();
     bf.Serialize(ms, obj);
     return ms.ToArray();
 }
示例#2
0
文件: Archive.cs 项目: mihan1991/GAF
        //сериализация словаря
        private void Compress(Dict dict)
        {
            Console.WriteLine("Проход 2. Записываю архив.\n");
            byte[] b;
            //string tmp="";
            FileStream fs = new FileStream(root + "/" + "archive.bin", FileMode.OpenOrCreate);

            //пишем сигнатуры

            //заголовок
            b = GetBytes(HEADER);
            //tmp += HEADER;
            fs.Write(b, 0, b.Length);

            //заголовок таблицы
            b = GetBytes(TAB_IN);
            //tmp += TAB_IN;
            fs.Write(b, 0, b.Length);

            //пишем длину семпла
            Sample_Length = dict.GetSampleLength();
            b = BitConverter.GetBytes(Sample_Length);
            //tmp += Sample_Length;
            fs.Write(b, 0, b.Length);

            //пишет таблицу
            Dictionary<int, string> d = dict.GetTable();
            for (int i = 0; i < d.Keys.Count; i++)
            {
                int q = d.First(a => a.Value == d[i]).Key;
                b = BitConverter.GetBytes(q);
                //tmp += q;
                fs.Write(b, 0, b.Length);

                b = GetBytes(d[i]);
                //tmp += d[i];
                fs.Write(b, 0, b.Length);

                if (i < d.Keys.Count - 1)
                {
                    b = GetBytes(BORDER_NORM);
                    //tmp += BORDER_NORM;
                    fs.Write(b, 0, b.Length);
                }
            }
            //пишем завершение таблицы
            b = GetBytes(TAB_OUT);
            //tmp += TAB_OUT;
            fs.Write(b, 0, b.Length);

            //бекапим содержимое 1 файла
            string new_data = data[0];

            //загоняем образцы
            while (new_data.Length > Sample_Length)
            {
                //формируем образец
                string sample = new_data.Remove(Sample_Length);

                //сверяем со словарем
                //если есть совпадение
                int comp = dict.CompareSample(sample);
                if (comp != -1)
                {
                    //пишем сигнатуру замены
                    b = GetBytes(BORDER_KEY);
                    //tmp += BORDER_KEY;
                    fs.Write(b, 0, b.Length);

                    //и ключ
                    b = BitConverter.GetBytes(comp);
                    //tmp += comp;
                    fs.Write(b, 0, b.Length);
                }

                //если же не нашли
                else
                {
                    //пишем сигнатуру обычного куска
                    b = GetBytes(BORDER_NORM);
                    //tmp += BORDER_NORM;
                    fs.Write(b, 0, b.Length);

                    //и сам кусок
                    b = GetBytes(sample);
                    //tmp += sample;
                    fs.Write(b, 0, b.Length);
                }

                //отсекаем его от остатков
                new_data = new_data.Substring(Sample_Length);
            }
            //b = GetBytes(tmp);
            //fs.Write(b, 0, b.Length);
            fs.Flush();
            fs.Close();
            Console.WriteLine("Запись завершена.");
        }