示例#1
0
        public void Differ_modifer_file(string Path, differ_info_json_list diff_json, byte[] filedata)
        {
            string Path_new = Path + "_new";

            long       block_size = 1024;
            FileStream file_old   = new FileStream(Path, FileMode.Open);
            FileStream file_new   = new FileStream(Path_new, FileMode.Create, FileAccess.Write);
            long       last_index = 0;

            for (int i = 0; i < diff_json.List.Count; i++)
            {
                long len = 0;
                switch (diff_json.List[i].Typ)
                {
                case 1:

                    len = diff_json.List[i].Len;
                    byte[] temp_data_1 = new byte[len];
                    Buffer.BlockCopy(filedata, (int)last_index, temp_data_1, 0, (int)len);
                    file_new.Write(temp_data_1, 0, temp_data_1.Length);
                    last_index += len;
                    break;

                case 2:
                    len = diff_json.List[i].Len;
                    long   Idx         = diff_json.List[i].Idx;
                    byte[] temp_data_2 = new byte[len];
                    // move pointer to Idx * block_size
                    file_old.Seek(Idx * block_size, SeekOrigin.Begin);
                    file_old.Read(temp_data_2, 0, (int)block_size);
                    file_new.Write(temp_data_2, 0, temp_data_2.Length);
                    break;
                }
            }

            file_old.Flush();
            file_new.Flush();
            file_old.Close();
            file_new.Close();

            File.Delete(Path);
            File.Move(Path_new, Path);
        }
示例#2
0
        public void Search_block_index(string Path, List <string> md5_list, out differ_info_json_list diff_json, out byte[] filedata, string filename)
        {
            diff_json = new differ_info_json_list();
            filedata  = null;
            FileStream file = new FileStream(Path, FileMode.Open);

            //differ_info_json_list diff_json = new differ_info_json_list();

            diff_json.List = new List <Differ_info_json>();
            diff_json.Name = filename;
            diff_json.Num  = 1;
            diff_json.Idx  = 0;

            // long block_size = 1024;
            int  current_index = 0;
            int  tmp_start     = 0;
            bool flag          = true;
            bool flag_ex       = false;
            int  matched       = 0;

            byte[] data = new byte[file.Length];
            file.Read(data, 0, data.Length);

            while (current_index + 1023 <= data.Length - 1)
            {
                byte[] temp = new byte[1024];

                Buffer.BlockCopy(data, current_index, temp, 0, 1024);

                string md5 = GetMD5Hash(temp);

                for (int n = 0; n < md5_list.Count; n++)
                {
                    if (md5_list[n] == md5)
                    {
                        flag_ex = true;
                        matched = n;
                        break;
                    }
                }

                if (flag_ex)
                {
                    if (!flag)
                    {
                        flag = true;

                        //
                        diff_json.List.Add(new Differ_info_json()
                        {
                            Idx = tmp_start,
                            Typ = 1,
                            Len = current_index - tmp_start
                        });
                        byte[] tmp = new byte[current_index - tmp_start];
                        Buffer.BlockCopy(data, tmp_start, tmp, 0, tmp.Length);
                        if (filedata != null)
                        {
                            filedata = filedata.Concat(tmp).Where(a => '1' == '1').ToArray();
                        }
                        else
                        {
                            filedata = tmp;
                        }
                    }
                    diff_json.List.Add(new Differ_info_json()
                    {
                        Idx = matched,
                        Typ = 2,
                        Len = 1024
                    });

                    current_index += 1024;
                    tmp_start      = current_index;
                }
                else
                {
                    if (flag)
                    {
                        tmp_start = current_index;
                        flag      = false;
                    }
                    current_index++;
                }
            }
            if (current_index + 1023 > data.Length - 1)
            {
                diff_json.List.Add(new Differ_info_json()
                {
                    Idx = tmp_start,
                    Typ = 1,
                    Len = data.Length - tmp_start
                });
                byte[] tmp = new byte[data.Length - tmp_start];
                Buffer.BlockCopy(data, tmp_start, tmp, 0, tmp.Length);
                if (filedata != null)
                {
                    filedata = filedata.Concat(tmp).Where(a => '1' == '1').ToArray();
                }
                else
                {
                    filedata = tmp;
                }
            }
        }
示例#3
0
        public void Upload_File_differ(byte[] file_id, string file_name)
        {
            SendMessage(BuildDataPackage_For_Pull(file_id, 0x22, Device_id));
            byte[] result    = ReceiveMessage();
            byte[] hash_json = new byte[300];
            Buffer.BlockCopy(result, 0, hash_json, 0, hash_json.Length);
            int index = hash_json.ToList().IndexOf(0);

            byte[] hash_json_new = new byte[index];
            Buffer.BlockCopy(hash_json, 0, hash_json_new, 0, hash_json_new.Length);


            string          str             = System.Text.Encoding.Default.GetString(hash_json_new);
            file_index_json File_index_json = JsonConvert.DeserializeObject <file_index_json>(str);

            string Path = PATH + File_index_json.Name;

            int           hash_list_num = (result.Length - 300) / 16;
            List <string> md5_list      = new List <string>();
            //
            bool flag = true;

            while (flag)
            {
                index = 0;
                if (hash_list_num != 0)
                {
                    byte[] md5 = new byte[16];
                    Buffer.BlockCopy(result, 300 + index * 16, md5, 0, md5.Length);


                    md5_list.Add(BitConverter.ToString(md5, 0).Replace("-", string.Empty).ToLower());
                    hash_list_num--;
                }
                else
                {
                    flag = false;
                }
            }
            differ_info_json_list diff_json = new differ_info_json_list();

            byte[] filedata = null;
            lib.Search_block_index(Path, md5_list, out diff_json, out filedata, file_name);

            byte[] Diff_json = new byte[1000];



            int filedata_lenth = filedata.Length;

            double num = Math.Ceiling((double)filedata_lenth / (double)(0xffff - 2 - 1000));

            if (filedata_lenth > (0xffff - 2 - 1000))
            {
                int idx = 0;
                while (true)
                {
                    if (filedata_lenth > 0)
                    {
                        diff_json.Idx = idx;
                        diff_json.Num = (int)num;
                        byte[] Diff_json_new = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(diff_json));

                        Buffer.BlockCopy(Diff_json_new, 0, Diff_json, 0, Diff_json_new.Length);
                        byte[] Msg = new byte[1000 + filedata_lenth];
                        Buffer.BlockCopy(Diff_json, 0, Msg, 0, 1000);
                        Buffer.BlockCopy(filedata, (0xffff - 2 - 1000) * idx, Msg, 0, filedata_lenth);
                        SendMessage(BuildDataPackage_For_Pull(Msg, 0x22, Device_id));
                    }
                    else
                    {
                        break;
                    }
                    filedata_lenth -= (0xffff - 1000 - 2);
                    idx++;
                }
            }
            else
            {
                byte[] Diff_json_new = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(diff_json));

                Buffer.BlockCopy(Diff_json_new, 0, Diff_json, 0, Diff_json_new.Length);
                byte[] Msg = new byte[1000 + filedata.Length];
                Buffer.BlockCopy(Diff_json, 0, Msg, 0, 1000);
                Buffer.BlockCopy(filedata, 0, Msg, 1000, filedata.Length);
                SendMessage(BuildDataPackage_For_Pull(Msg, 0x22, Device_id));
            }
        }