Beispiel #1
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);
            }
        }
Beispiel #2
0
        public int Load(string fileName)
        {
            try
            {
                FileStream fileStream = new FileStream(fileName, FileMode.Open);
                byte[]     nFiles     = new byte[1];
                fileStream.Read(nFiles, 0, 1);
                if (nFiles.Length > 0)
                {
                    SubFileList = new List <VirtualSubFile>();

                    for (int i = 0; i < (int)nFiles[0]; i++)
                    {
                        byte[] startIndexBytes = new byte[FILEINDEXBYTES];
                        fileStream.Read(startIndexBytes, 0, FILEINDEXBYTES);

                        long startPosition = (startIndexBytes[0] << 56) | (startIndexBytes[1] << 48) | (startIndexBytes[2] << 40) | (startIndexBytes[3] << 32) | (startIndexBytes[4] << 24) | (startIndexBytes[5] << 16) | (startIndexBytes[6] << 8) | (startIndexBytes[7]);

                        byte[] sizeBytes = new byte[FILESIZEBYTES];
                        fileStream.Read(sizeBytes, 0, FILESIZEBYTES);

                        long size = (sizeBytes[0] << 56) | (sizeBytes[1] << 48) | (sizeBytes[2] << 40) | (sizeBytes[3] << 32) | (sizeBytes[4] << 24) | (sizeBytes[5] << 16) | (sizeBytes[6] << 8) | (sizeBytes[7]);

                        byte[] fileNameBytes = new byte[FILENAMEBYTES];
                        fileStream.Read(fileNameBytes, 0, FILENAMEBYTES);
                        string currentFileName = "";
                        for (int a = 0; a < FILENAMEBYTES; a++)
                        {
                            if (fileNameBytes[a] != 0x00)
                            {
                                currentFileName += (char)fileNameBytes[a];
                            }
                        }

                        byte[] md5Hash = new byte[MD5HASHSIZE];
                        fileStream.Read(md5Hash, 0, MD5HASHSIZE);
                        VirtualSubFile subFile = new VirtualSubFile(i);
                        subFile.Data          = new byte[size];
                        subFile.MD5HashTarget = md5Hash;
                        subFile.Name          = currentFileName;
                        SubFileList.Add(subFile);
                    }

                    int successfulLoads = 0;
                    for (int i = 0; i < (int)nFiles[0]; i++)
                    {
                        byte[] fileData = new byte[SubFileList[i].Size];
                        fileStream.Read(fileData, 0, (int)SubFileList[i].Size);
                        SubFileList[i].Data = fileData;
                        if (SubFileList[i].MD5Pass)
                        {
                            Console.BackgroundColor = ConsoleColor.Green;
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.WriteLine("File '{0}' md5 pass...", SubFileList[i].Name);
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.ForegroundColor = ConsoleColor.White;
                            successfulLoads++;
                        }
                        else
                        {
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.WriteLine("File '{0}' md5 fail...", SubFileList[i].Name);
                            Console.BackgroundColor = ConsoleColor.Black;
                        }
                    }

                    fileStream.Close();
                    return(successfulLoads);
                }
                fileStream.Close();
                return(-4);
            }
            catch (FileNotFoundException)
            {
                return(-1);
            }
            catch (FileLoadException)
            {
                return(-2);
            }
        }