예제 #1
0
        public void AddFile(string fileName, Stream stream, long length, string contentType, string path)
        {
            //得到文件的数据
            byte[] dataBytes = GetCompressedData(stream);
            //得到文件的名称
            var filePath = path + "/" + fileName;
            //得到文件的哈希
            var hash = Cryptor.EncryptSHA1(dataBytes);

            //判断文件内容是否已经存在
            if (fileDataIndex.ContainsKey(hash))
            {
                //判断文件名称是否已经存在
                if (!fileNameIndex.ContainsKey(filePath))
                {
                    fileNameIndex.Add(filePath, fileDataIndex[hash]);
                }
                else
                {
                    fileNameIndex[filePath] = fileDataIndex[hash];
                }
                return;
            }
            var blockIndex = new BlockIndex((uint)(dataFileStream.Position / 8), filePath, hash);

            this.fileBlocks.Add(blockIndex);
            var fileIndex = this.fileBlocks.Count - 1;

            fileDataIndex.Add(hash, fileIndex);
            //判断文件名称是否已经存在
            if (!fileNameIndex.ContainsKey(filePath))
            {
                fileNameIndex.Add(filePath, fileDataIndex[hash]);
            }
            else
            {
                fileNameIndex[filePath] = fileDataIndex[hash];
            }
            Block block = new Block(blockIndex.Offset, blockIndex.FileName, blockIndex.Hash, contentType, 0, dataBytes, blockIndex.Cookie);

            dataBytes = block.ToBytes();
            dataFileStream.Write(dataBytes, 0, dataBytes.Length);
            dataFileStream.Flush();
        }
예제 #2
0
        private void BuildIndex()
        {
            using (var indexFileReader = File.Open(this.indexFile, FileMode.OpenOrCreate, FileAccess.Read))
            {
                while (true)
                {
                    var size = ReadInt(indexFileReader);
                    if (size <= 0)
                    {
                        return;
                    }
                    var buffer = new byte[size];
                    indexFileReader.Read(buffer, 0, size);

                    var index = BlockIndex.ReadBlockIndex(buffer);
                    //判断文件内容是否已经存在
                    if (fileDataIndex.ContainsKey(index.Hash))
                    {
                        //判断文件名称是否已经存在
                        if (!fileNameIndex.ContainsKey(index.FileName))
                        {
                            fileNameIndex.Add(index.FileName, fileDataIndex[index.Hash]);
                        }
                        else
                        {
                            fileNameIndex[index.FileName] = fileDataIndex[index.Hash];
                        }
                        continue;
                    }
                    this.fileBlocks.Add(index);
                    var fileIndex = this.fileBlocks.Count - 1;
                    fileDataIndex.Add(index.Hash, fileIndex);
                    fileNameIndex.Add(index.FileName, fileIndex);
                }
            }
        }