Example #1
0
        private static FindStatus fileneededTest(RvFile tFile)
        {
            // first check to see if we already have it in the file table
            bool inFileDB = RvRomFileMatchup.FindInFiles(tFile); // returns true if found in File table

            return(inFileDB ? FindStatus.FoundFileInArchive : FindStatus.FileNeededInArchive);
        }
Example #2
0
        private static bool ScanAFile(string realFilename, Stream memzip, string displayFilename)
        {
            Stream fStream;

            if (string.IsNullOrEmpty(realFilename) && memzip != null)
            {
                fStream = memzip;
            }
            else
            {
                int errorCode = FileStream.OpenFileRead(realFilename, out fStream);
                if (errorCode != 0)
                {
                    return(false);
                }
            }

            bool           ret           = false;
            HeaderFileType foundFileType = FileHeaderReader.FileHeaderReader.GetType(fStream, out int offset);

            fStream.Position = 0;
            RvFile tFile = UnCompFiles.CheckSumRead(fStream, offset);

            tFile.AltType = foundFileType;


            if (foundFileType == HeaderFileType.CHD)
            {
                // read altheader values from CHD file.
            }

            // test if needed.
            FindStatus res = RvRomFileMatchup.FileneededTest(tFile);

            if (res == FindStatus.FileNeededInArchive)
            {
                _bgw?.ReportProgress(0, new bgwShowError(displayFilename, "found"));
                Debug.WriteLine("Reading file as " + tFile.SHA1);
                GZip   gz      = new GZip(tFile);
                string outfile = RomRootDir.Getfilename(tFile.SHA1);
                fStream.Position = 0;
                gz.WriteGZip(outfile, fStream, false);

                tFile.CompressedSize = gz.compressedSize;
                tFile.DBWrite();
                ret = true;
            }
            else if (res == FindStatus.FoundFileInArchive)
            {
                ret = true;
            }

            if (foundFileType == HeaderFileType.ZIP || foundFileType == HeaderFileType.SevenZip || foundFileType == HeaderFileType.GZ)
            {
                ICompress fz;
                switch (foundFileType)
                {
                case HeaderFileType.SevenZip:
                    fz = new SevenZ();
                    break;

                case HeaderFileType.GZ:
                    fz = new gZip();
                    break;

                //case HeaderFileType.ZIP:
                default:
                    fz = new Zip();
                    break;
                }

                fStream.Position = 0;

                ZipReturn zp;

                if (string.IsNullOrEmpty(realFilename) && memzip != null)
                {
                    zp = fz.ZipFileOpen(memzip);
                }
                else
                {
                    zp = fz.ZipFileOpen(realFilename);
                }

                if (zp == ZipReturn.ZipGood)
                {
                    bool allZipFound = true;
                    for (int i = 0; i < fz.LocalFilesCount(); i++)
                    {
                        ZipReturn openFile = fz.ZipFileOpenReadStream(i, out Stream stream, out ulong streamSize);

                        if (streamSize <= _inMemorySize)
                        {
                            if (openFile == ZipReturn.ZipTryingToAccessADirectory)
                            {
                                continue;
                            }
                            byte[] tmpFile = new byte[streamSize];
                            stream.Read(tmpFile, 0, (int)streamSize);
                            using (Stream memStream = new MemoryStream(tmpFile, false))
                            {
                                allZipFound &= ScanAFile(null, memStream, fz.Filename(i));
                            }
                        }
                        else
                        {
                            string file = Path.Combine(_tmpDir, Guid.NewGuid().ToString());
                            FileStream.OpenFileWrite(file, out Stream fs);
                            ulong sizetogo = streamSize;
                            while (sizetogo > 0)
                            {
                                int sizenow = sizetogo > (ulong)Buffersize ? Buffersize : (int)sizetogo;
                                stream.Read(Buffer, 0, sizenow);
                                fs.Write(Buffer, 0, sizenow);
                                sizetogo -= (ulong)sizenow;
                            }
                            fs.Close();

                            allZipFound &= ScanAFile(file, null, fz.Filename(i));

                            File.Delete(file);
                        }
                        //fz.ZipFileCloseReadStream();
                    }
                    fz.ZipFileClose();
                    ret |= allZipFound;
                }
                else
                {
                    ret = false;
                }
            }

            if (!string.IsNullOrEmpty(realFilename) || memzip == null)
            {
                fStream.Close();
                fStream.Dispose();
            }


            return(ret);
        }