Ejemplo n.º 1
0
        public FileManager()
        {
            var        superBlock = new SuperBlock(1, Directory.GetCurrentDirectory() + "/dir");
            var        hash       = Encoding.UTF8.GetBytes("testtesttesttesttest");
            BlockIndex index      = new BlockIndex(0, Encoding.UTF8.GetBytes("test"), hash, 3, 2, 1);
            var        index2     = new BlockIndex(index.ToHeaderBytes());
            Block      block      = new Block(index.ToHeaderBytes(), "app", 4);

            Block block2 = new Block(new MemoryStream(block.ToBytes(hash)));

            this.superBlocks.Add(1, superBlock);
        }
Ejemplo n.º 2
0
        public void AddFile(string dir, string fileName, Stream stream)
        {
            var path = string.Join("/", dir.Split('\\', '/').Skip(1).ToArray());

            //得到文件的数据
            byte[] dataBytes = GetCompressedData(stream);
            //得到文件的名称
            var filePath = Encoding.UTF8.GetBytes(path + "/" + fileName);
            //得到文件的哈希
            var hash = sha(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)(dataFileWriter.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.ToHeaderBytes(), "", 0);

            dataBytes = block.ToBytes(dataBytes);
            dataFileWriter.Write(dataBytes, 0, dataBytes.Length);
            dataFileWriter.Flush();
        }