Пример #1
0
 public int RemoveFileIndex(int index)
 {
     if (index > SubFileList.Count - 1)
     {
         return(-1);
     }
     CurrentID = index;
     SubFileList.RemoveAt(index);
     for (int i = index; i < SubFileList.Count; i++)
     {
         SubFileList[i].ID = i;
     }
     return(0);
 }
Пример #2
0
        public int RemoveFileID(int ID)
        {
            int index = -1;

            for (int i = 0; i < SubFileList.Count; i++)
            {
                if (SubFileList[i].ID == ID)
                {
                    index = i;
                    SubFileList.RemoveAt(i);
                    break;
                }
            }
            for (int i = index; i < SubFileList.Count; i++)
            {
                SubFileList[i].ID = i;
            }
            return(0);
        }
Пример #3
0
        public int AddFile(string fileName)
        {
            try
            {
                int         fileSize        = (int)(new FileInfo(fileName)).Length;
                FileStream  newFileStream   = new FileStream(fileName, FileMode.Open);
                List <byte> newFileByteList = new List <byte>();
                for (int i = 0; i < fileSize; i++)
                {
                    newFileByteList.Add((byte)newFileStream.ReadByte());
                }
                newFileStream.Close();

                newFileStream = new FileStream(fileName, FileMode.Open);
                byte[] file = new byte[fileSize];
                newFileStream.Read(file, 0, fileSize);
                newFileStream.Close();

                string         fileNameWithoutDirectory = Path.GetFileName(fileName);
                VirtualSubFile subFile = new VirtualSubFile(CurrentID, fileNameWithoutDirectory);
                subFile.Data = newFileByteList.ToArray();
                SubFileList.Add(subFile);
                CurrentID++;
                return(subFile.ID);
            }
            catch (FileNotFoundException)
            {
                return(-1);
            }
            catch (FileLoadException)
            {
                return(-2);
            }
            catch (UnauthorizedAccessException)
            {
                return(-5);
            }
        }
Пример #4
0
        public int Build(string fileName)
        {
            try
            {
                List <byte> virtualFileDataList = new List <byte>();
                byte        nFiles             = (byte)SubFileList.Count();
                int         totalIndexFileSize = BYTESPERFILEINDEX * nFiles;
                int         totalManifestSize  = totalIndexFileSize + 1;
                byte[]      bytes = new byte[totalManifestSize];
                bytes[0] = nFiles;
                long currentStartPosition = totalManifestSize;
                for (int i = 0; i < SubFileList.Count; i++)
                {
                    long fileSize = SubFileList[i].Size;
                    currentStartPosition += fileSize;
                    int offset = 1;

                    //bytes[BYTESPERFILEINDEX * i + 0 + offset] = (byte)(currentStartPosition >> 56);
                    //bytes[BYTESPERFILEINDEX * i + 1 + offset] = (byte)(currentStartPosition >> 48);
                    //bytes[BYTESPERFILEINDEX * i + 2 + offset] = (byte)(currentStartPosition >> 40);
                    //bytes[BYTESPERFILEINDEX * i + 3 + offset] = (byte)(currentStartPosition >> 32);
                    //bytes[BYTESPERFILEINDEX * i + 4 + offset] = (byte)(currentStartPosition >> 24);
                    //bytes[BYTESPERFILEINDEX * i + 5 + offset] = (byte)(currentStartPosition >> 16);
                    //bytes[BYTESPERFILEINDEX * i + 6 + offset] = (byte)(currentStartPosition >> 8);
                    //bytes[BYTESPERFILEINDEX * i + 7 + offset] = (byte)(currentStartPosition);

                    //for (int a = 0; a < FILEINDEXBYTES; a++) bytes[BYTESPERFILEINDEX * i + a + offset] = (byte)(currentStartPosition >> ((FILEINDEXBYTES - 1) * 8) - (8 * a));

                    //bytes[BYTESPERFILEINDEX * i + 8 + offset] = (byte)(fileSize >> 56);
                    //bytes[BYTESPERFILEINDEX * i + 9 + offset] = (byte)(fileSize >> 48);
                    //bytes[BYTESPERFILEINDEX * i + 10 + offset] = (byte)(fileSize >> 40);
                    //bytes[BYTESPERFILEINDEX * i + 11 + offset] = (byte)(fileSize >> 32);
                    //bytes[BYTESPERFILEINDEX * i + 12 + offset] = (byte)(fileSize >> 24);
                    //bytes[BYTESPERFILEINDEX * i + 13 + offset] = (byte)(fileSize >> 16);
                    //bytes[BYTESPERFILEINDEX * i + 14 + offset] = (byte)(fileSize >> 8);
                    //bytes[BYTESPERFILEINDEX * i + 15 + offset] = (byte)(fileSize);

                    for (int a = 0; a < FILESIZEBYTES; a++)
                    {
                        bytes[BYTESPERFILEINDEX * i + a + FILEINDEXBYTES + offset] = (byte)(fileSize >> ((FILESIZEBYTES - 1) * 8) - (8 * a));
                    }

                    offset = 17;

                    for (int a = 0; a < SubFileList[i].Name.Length; a++)
                    {
                        bytes[BYTESPERFILEINDEX * i + a + offset] = (byte)(char)SubFileList[i].Name[a];
                    }

                    offset += FILENAMEBYTES;

                    for (int a = 0; a < SubFileList[i].MD5Hash.Length; a++)
                    {
                        bytes[BYTESPERFILEINDEX * i + a + offset] = SubFileList[i].MD5Hash[a];
                    }
                }
                foreach (byte currentByte in bytes)
                {
                    virtualFileDataList.Add(currentByte);
                }
                foreach (VirtualSubFile subFile in SubFileList)
                {
                    foreach (byte currentByte in subFile.Data)
                    {
                        virtualFileDataList.Add(currentByte);
                    }
                }
                byte[]     virtualFileData = virtualFileDataList.ToArray();
                FileStream fileStream      = new FileStream(fileName, FileMode.Create);
                foreach (byte currentByte in virtualFileData)
                {
                    fileStream.WriteByte(currentByte);
                }
                fileStream.Close();
                return(0);
            }
            catch (FileNotFoundException)
            {
                return(-1);
            }
            catch (FileLoadException)
            {
                return(-2);
            }
            catch (IOException)
            {
                return(-3);
            }
        }