예제 #1
0
        private static void DeepScanFile(string directory, RvFile tFile)
        {
            string filename  = Path.Combine(directory, tFile.Name);
            int    errorCode = UnCompFiles.CheckSumRead(filename, true, out tFile.CRC, out tFile.MD5, out tFile.SHA1);

            if (errorCode == 32)
            {
                tFile.GotStatus = GotStatus.FileLocked;
                return;
            }
            if (errorCode != 0)
            {
                ReportError.Show("File: " + filename + " Error: " + new Win32Exception(errorCode).Message + ". Scan Aborted.");
                _fileErrorAbort = true;
                return;
            }
            tFile.FileStatusSet(FileStatus.SizeVerified | FileStatus.CRCVerified | FileStatus.SHA1Verified | FileStatus.MD5Verified);
        }
예제 #2
0
        private static void ScanADir(string directory)
        {
            _bgw.ReportProgress(0, new bgwText("Scanning Dir : " + directory));
            DirectoryInfo di = new DirectoryInfo(directory);

            FileInfo[] fi = di.GetFiles();

            _bgw.ReportProgress(0, new bgwRange2Visible(true));
            _bgw.ReportProgress(0, new bgwSetRange2(fi.Count()));

            for (int j = 0; j < fi.Count(); j++)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }

                FileInfo f = fi[j];
                _bgw.ReportProgress(0, new bgwValue2(j));
                _bgw.ReportProgress(0, new bgwText2(f.Name));
                string ext = Path.GetExtension(f.Name);

                if (ext.ToLower() == ".zip")
                {
                    ZipFile   fz = new ZipFile();
                    ZipReturn zr = fz.ZipFileOpen(f.FullName, f.LastWriteTime, true);
                    if (zr != ZipReturn.ZipGood)
                    {
                        continue;
                    }

                    fz.DeepScan();

                    int FileUsedCount = 0;

                    for (int i = 0; i < fz.LocalFilesCount(); i++)
                    {
                        Debug.WriteLine(fz.Filename(i));
                        RvFile tFile = new RvFile();
                        tFile.Size = fz.UncompressedSize(i);
                        tFile.CRC  = fz.CRC32(i);
                        tFile.MD5  = fz.MD5(i);
                        tFile.SHA1 = fz.SHA1(i);
                        Debug.WriteLine("CRC " + VarFix.ToString(tFile.CRC));
                        Debug.WriteLine("MD5 " + VarFix.ToString(tFile.MD5));
                        Debug.WriteLine("SHA1 " + VarFix.ToString(tFile.SHA1));


                        FindStatus res = fileneededTest(tFile);

                        if (res == FindStatus.FileUnknown)
                        {
                            continue;
                        }

                        FileUsedCount++;

                        if (res != FindStatus.FoundFileInArchive)
                        {
                            GZip gz = new GZip();
                            gz.crc              = tFile.CRC;
                            gz.md5Hash          = tFile.MD5;
                            gz.sha1Hash         = tFile.SHA1;
                            gz.uncompressedSize = tFile.Size;

                            bool   isZipTrrntzip = (fz.ZipStatus == ZipStatus.TrrntZip);
                            ulong  compressedSize;
                            ushort method;
                            Stream zds;

                            fz.ZipFileOpenReadStream(i, isZipTrrntzip, out zds, out compressedSize, out method);
                            gz.compressedSize = compressedSize;

                            string outfile = Getfilename(tFile.SHA1);
                            gz.WriteGZip(outfile, zds, isZipTrrntzip);
                            tFile.CompressedSize = gz.compressedSize;
                            fz.ZipFileCloseReadStream();

                            tFile.DBWrite();
                        }
                    }
                    fz.ZipFileClose();

                    if (delFiles && FileUsedCount == fz.LocalFilesCount())
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
                else if (ext.ToLower() == ".gz")
                {
                    GZip      gZipTest  = new GZip();
                    ZipReturn errorcode = gZipTest.ReadGZip(f.FullName, true);
                    if (errorcode != ZipReturn.ZipGood)
                    {
                        continue;
                    }
                    RvFile tFile = new RvFile();
                    tFile.CRC  = gZipTest.crc;
                    tFile.MD5  = gZipTest.md5Hash;
                    tFile.SHA1 = gZipTest.sha1Hash;
                    tFile.Size = gZipTest.uncompressedSize;

                    FindStatus res = fileneededTest(tFile);

                    if (res == FindStatus.FileUnknown)
                    {
                        continue;
                    }

                    if (res != FindStatus.FoundFileInArchive)
                    {
                        GZip gz = new GZip();
                        gz.crc              = tFile.CRC;
                        gz.md5Hash          = tFile.MD5;
                        gz.sha1Hash         = tFile.SHA1;
                        gz.uncompressedSize = tFile.Size;

                        Stream ds;
                        gZipTest.GetStream(out ds);
                        string outfile = Getfilename(tFile.SHA1);
                        gz.WriteGZip(outfile, ds, false);
                        ds.Close();
                        ds.Dispose();

                        gZipTest.Close();
                        tFile.CompressedSize = gz.compressedSize;
                        tFile.DBWrite();
                    }

                    if (delFiles)
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
                else
                {
                    RvFile tFile     = new RvFile();
                    int    errorcode = UnCompFiles.CheckSumRead(f.FullName, true, out tFile.CRC, out tFile.MD5, out tFile.SHA1, out tFile.Size);

                    if (errorcode != 0)
                    {
                        continue;
                    }

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

                    if (res == FindStatus.FileUnknown)
                    {
                        continue;
                    }

                    if (res != FindStatus.FoundFileInArchive)
                    {
                        GZip gz = new GZip();
                        gz.crc              = tFile.CRC;
                        gz.md5Hash          = tFile.MD5;
                        gz.sha1Hash         = tFile.SHA1;
                        gz.uncompressedSize = tFile.Size;

                        Stream ds;
                        int    errorCode = IO.FileStream.OpenFileRead(f.FullName, out ds);
                        string outfile   = Getfilename(tFile.SHA1);
                        gz.WriteGZip(outfile, ds, false);
                        ds.Close();
                        ds.Dispose();

                        tFile.CompressedSize = gz.compressedSize;
                        tFile.DBWrite();
                    }

                    if (delFiles)
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
            }

            DirectoryInfo[] childdi = di.GetDirectories();
            foreach (DirectoryInfo d in childdi)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }
                ScanADir(d.FullName);
            }

            if (directory != rootDir && IsDirectoryEmpty(directory))
            {
                Directory.Delete(directory);
            }
        }
예제 #3
0
        private static void ScanAFile(FileInfo f)
        {
            Debug.WriteLine(f.FullName);

            Stream fStream;
            int    errorCode = IO.FileStream.OpenFileRead(f.FullName, out fStream);

            if (errorCode != 0)
            {
                return;
            }

            int      offset;
            FileType foundFileType = FileHeaderReader.GetType(fStream, out offset);

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

            tFile.AltType = foundFileType;

            if (foundFileType == FileType.CHD)
            {
                // need to validate check the CHD file
            }

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

            if (res == FindStatus.FileNeededInArchive)
            {
                Debug.WriteLine("Reading file as " + tFile.SHA1);
                GZip gz = new GZip();
                gz.crc              = tFile.CRC;
                gz.md5Hash          = tFile.MD5;
                gz.sha1Hash         = tFile.SHA1;
                gz.uncompressedSize = tFile.Size;

                Stream ds;
                IO.FileStream.OpenFileRead(f.FullName, out ds);
                string outfile = Getfilename(tFile.SHA1);
                gz.WriteGZip(outfile, ds, false);
                ds.Close();
                ds.Dispose();

                tFile.DBWrite();
            }

            if (foundFileType == FileType.ZIP)
            {
                ZipFile fz = new ZipFile();
                fz.ZipFileOpen(f.FullName, f.LastWriteTime, true);
                {
                    for (int i = 0; i < fz.LocalFilesCount(); i++)
                    {
                        // this needs to go back into the Zip library.
                        int    Buffersize = 1024 * 1024;
                        byte[] _buffer    = new byte[Buffersize];
                        Stream stream;
                        ulong  streamSize;
                        ushort compressionMethod;
                        fz.ZipFileOpenReadStream(i, false, out stream, out streamSize, out compressionMethod);
                        string file = @"C:\RomVaultX\" + Guid.NewGuid();
                        Stream Fs;
                        IO.FileStream.OpenFileWrite(file, out 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();
                        stream.Close();

                        FileInfo fi = new FileInfo(file);
                        ScanAFile(fi);
                        File.Delete(file);
                    }
                }
            }
            if (foundFileType == FileType.GZ)
            {
            }
        }
예제 #4
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);
        }