private static void ScanRomRoot(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() == ".gz") { gZip gZipTest = new gZip(); ZipReturn errorcode = gZipTest.ZipFileOpen(f.FullName); if (errorcode != ZipReturn.ZipGood) { _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz File corrupt")); if (!Directory.Exists("corrupt")) { Directory.CreateDirectory("corrupt"); } File.Move(f.FullName, Path.Combine("corrupt", f.Name)); continue; } RvFile tFile = RvFile.fromGZip(f.FullName, gZipTest.ExtraData, gZipTest.CompressedSize); gZipTest.ZipFileClose(); FindStatus res = fileneededTest(tFile); if (res != FindStatus.FoundFileInArchive) { if (deep) { gZipTest = new gZip(); try { errorcode = gZipTest.ZipFileOpen(f.FullName); if (errorcode == ZipReturn.ZipGood) { FileScan fs = new FileScan(); List <FileScan.FileResults> gRes = fs.Scan(gZipTest, true, true); errorcode = gRes[0].FileStatus; gZipTest.ZipFileClose(); } } catch { gZipTest.ZipFileClose(); _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz Crashed Compression")); if (!Directory.Exists("corrupt")) { Directory.CreateDirectory("corrupt"); } File.Move(f.FullName, Path.Combine("corrupt", f.Name)); continue; } if (errorcode != ZipReturn.ZipGood) { _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz File corrupt")); if (!Directory.Exists("corrupt")) { Directory.CreateDirectory("corrupt"); } File.Move(f.FullName, Path.Combine("corrupt", f.Name)); continue; } } tFile.DBWrite(); } } if (_bgw.CancellationPending) { return; } } DirectoryInfo[] childdi = di.GetDirectories(); foreach (DirectoryInfo d in childdi) { if (_bgw.CancellationPending) { return; } ScanRomRoot(d.FullName); } }
public static RvFile FromAZipFile(RvFile dbDir, EScanLevel eScanLevel, ThreadWorker thWrk) { RvFile fileDir = new RvFile(dbDir.FileType); DatStatus chechingDatStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat; string filename = dbDir.FullNameCase; ICompress checkZ = dbDir.FileType == FileType.Zip ? new Zip() : (ICompress) new SevenZ(); ZipReturn zr = checkZ.ZipFileOpen(filename, dbDir.FileModTimeStamp); if (zr == ZipReturn.ZipGood) { dbDir.ZipStatus = checkZ.ZipStatus; // to be Scanning a ZIP file means it is either new or has changed. // as the code below only calls back here if that is true. // // Level1: Only use header CRC's // Just get the CRC for the ZIP headers. // // Level2: Fully checksum changed only files // We know this file has been changed to do a full checksum scan. // // Level3: Fully checksum everything // So do a full checksum scan. if (_fs == null) { _fs = new FileScan(); } List <FileScan.FileResults> fr = _fs.Scan(checkZ, false, eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3); // add all of the file information from the zip file into scanDir for (int i = 0; i < checkZ.LocalFilesCount(); i++) { LocalFile lf = checkZ.GetLocalFile(i); RvFile tFile = new RvFile(DBTypeGet.FileFromDir(dbDir.FileType)) { Name = lf.Filename, ZipFileIndex = i, ZipFileHeaderPosition = lf.LocalHead, Size = lf.UncompressedSize, CRC = lf.CRC, FileModTimeStamp = lf.LastModified }; // all levels read the CRC from the ZIP header tFile.SetStatus(chechingDatStatus, GotStatus.Got); tFile.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader); if (fr[i].FileStatus != ZipReturn.ZipGood) { thWrk.Report(new bgwShowCorrupt(fr[i].FileStatus, filename + " : " + lf.Filename)); tFile.GotStatus = GotStatus.Corrupt; } else { tFile.HeaderFileType = fr[i].HeaderFileType; tFile.SHA1 = fr[i].SHA1; tFile.MD5 = fr[i].MD5; tFile.AltSize = fr[i].AltSize; tFile.AltCRC = fr[i].AltCRC; tFile.AltSHA1 = fr[i].AltSHA1; tFile.AltMD5 = fr[i].AltMD5; if (fr[i].CRC != null) { tFile.FileStatusSet(FileStatus.CRCFromHeader); if (eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3) { tFile.FileStatusSet(FileStatus.CRCVerified); } } tFile.FileStatusSet( FileStatus.SizeVerified | (fr[i].HeaderFileType != HeaderFileType.Nothing ? FileStatus.HeaderFileTypeFromHeader : 0) | (fr[i].SHA1 != null ? FileStatus.SHA1Verified : 0) | (fr[i].MD5 != null ? FileStatus.MD5Verified : 0) | (fr[i].AltSize != null ? FileStatus.AltSizeVerified : 0) | (fr[i].AltCRC != null ? FileStatus.AltCRCVerified : 0) | (fr[i].AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) | (fr[i].AltMD5 != null ? FileStatus.AltMD5Verified : 0) ); } fileDir.ChildAdd(tFile); } } else if (zr == ZipReturn.ZipFileLocked) { thWrk.Report(new bgwShowError(filename, "Zip File Locked")); dbDir.FileModTimeStamp = 0; dbDir.GotStatus = GotStatus.FileLocked; } else { thWrk.Report(new bgwShowCorrupt(zr, filename)); dbDir.GotStatus = GotStatus.Corrupt; } checkZ.ZipFileClose(); return(fileDir); }
private static void AddZip(FileInfo f, DatDir thisDir) { Zip zf1 = new Zip(); ZipReturn result = zf1.ZipFileOpen(f.FullName, -1, true); if (result != ZipReturn.ZipGood) { return; } zCount += 1; if ((zf1.ZipStatus & ZipStatus.TrrntZip) == ZipStatus.TrrntZip) { tCount += 1; Console.WriteLine($"{zCount} {tCount} {cCount}"); } else if (zf1.Filecomment != null && zf1.Filecomment.Length > 0) { string comments = ZipUtils.GetString(zf1.Filecomment); if (comments.Length > 13 && comments.Substring(0, 13) == "TORRENTZIPPED") { tCount += 1; } else { cCount += 1; Console.WriteLine(f.FullName + " " + zCount); Console.WriteLine("------------------------"); Console.WriteLine(comments); } Console.WriteLine($"{zCount} {tCount} {cCount}"); } //zf1.ZipStatus = ZipStatus.TrrntZip; //DatDir ZipDir = new DatDir(zf1.ZipStatus == ZipStatus.TrrntZip ? DatFileType.DirTorrentZip : DatFileType.DirRVZip) DatDir ZipDir = new DatDir(DatFileType.UnSet) { Name = Path.GetFileNameWithoutExtension(f.Name), DGame = new DatGame() }; ZipDir.DGame.Description = ZipDir.Name; thisDir.ChildAdd(ZipDir); FileScan fs = new FileScan(); List <FileScan.FileResults> fr = fs.Scan(zf1, !quick, !quick); bool isTorrentZipDate = true; for (int i = 0; i < fr.Count; i++) { if (fr[i].FileStatus != ZipReturn.ZipGood) { Console.WriteLine("File Error :" + zf1.Filename(i) + " : " + fr[i].FileStatus); continue; } DatFile df = new DatFile(DatFileType.UnSet) { Name = zf1.Filename(i), Size = fr[i].Size, CRC = fr[i].CRC, SHA1 = fr[i].SHA1, DateModified = new DateTime(zf1.LastModified(i)).ToString("yyyy/MM/dd HH:mm:ss"), DateCreated = zf1.Created(i) == null ? null : new DateTime((long)zf1.Created(i)).ToString("yyyy/MM/dd HH:mm:ss"), DateAccessed = zf1.Accessed(i) == null ? null : new DateTime((long)zf1.Accessed(i)).ToString("yyyy/MM/dd HH:mm:ss") //df.MD5 = zf.MD5(i) }; if (zf1.LastModified(i) != 629870671200000000) { isTorrentZipDate = false; } ZipDir.ChildAdd(df); } zf1.ZipFileClose(); if (isTorrentZipDate && ZipDir.DatFileType == DatFileType.DirRVZip) { ZipDir.DatFileType = DatFileType.DirTorrentZip; } if (ZipDir.DatFileType == DatFileType.DirTorrentZip) { DatSetCompressionType.SetZip(ZipDir); DatClean.RemoveUnNeededDirectoriesFromZip(ZipDir); } }
private static bool ScanAFile(string realFilename, Stream memzip, string displayFilename) { Compress.File.File fStream = new Compress.File.File(); if (string.IsNullOrEmpty(realFilename) && memzip != null) { fStream.ZipFileOpen(memzip); } else { ZipReturn zRet = fStream.ZipFileOpen(realFilename, -1, true); if (zRet != ZipReturn.ZipGood) { return(false); } } bool ret = false; FileScan fScan = new FileScan(); List <FileScan.FileResults> resScan = fScan.Scan(fStream, true, true); HeaderFileType foundFileType = resScan[0].HeaderFileType; if (foundFileType == HeaderFileType.CHD) { // read altheader values from CHD file. } RvFile tFile = new RvFile { Size = resScan[0].Size, CRC = resScan[0].CRC, MD5 = resScan[0].MD5, SHA1 = resScan[0].SHA1, AltType = resScan[0].HeaderFileType, AltSize = resScan[0].AltSize, AltCRC = resScan[0].AltCRC, AltMD5 = resScan[0].AltMD5, AltSHA1 = resScan[0].AltSHA1 }; // 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); string outfile = RomRootDir.Getfilename(tFile.SHA1); gZip gz1 = new gZip(); gz1.ZipFileCreate(outfile); gz1.ExtraData = tFile.SetExtraData(); gz1.ZipFileOpenWriteStream(false, true, "", tFile.Size, 8, out Stream write, null); fStream.ZipFileOpenReadStream(0, out Stream s, out ulong _); // do copy StreamCopier.StreamCopy(s, write, tFile.Size); fStream.ZipFileCloseReadStream(); fStream.ZipFileClose(); gz1.ZipFileCloseWriteStream(tFile.CRC); tFile.CompressedSize = gz1.CompressedSize; gz1.ZipFileClose(); tFile.DBWrite(); ret = true; } else if (res == FindStatus.FoundFileInArchive) { ret = true; } fStream.ZipFileClose(); 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; } ZipReturn zp; if (string.IsNullOrEmpty(realFilename) && memzip != null) { memzip.Position = 0; zp = fz.ZipFileOpen(memzip); } else { zp = fz.ZipFileOpen(realFilename); } if (zp == ZipReturn.ZipGood) { bool allZipFound = true; for (int i = 0; i < fz.LocalFilesCount(); i++) { LocalFile lf = fz.GetLocalFile(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, lf.Filename); } } 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, lf.Filename); File.Delete(file); } //fz.ZipFileCloseReadStream(); } fz.ZipFileClose(); ret |= allZipFound; } else { ret = false; } } return(ret); }
private static void AddZip(FileInfo f, DatDir thisDir) { ZipFile zf1 = new ZipFile(); ZipReturn result = zf1.ZipFileOpen(f.FullName, -1, true); if (result != ZipReturn.ZipGood) { return; } //zf1.ZipStatus = ZipStatus.TrrntZip; DatDir ZipDir = new DatDir(zf1.ZipStatus == ZipStatus.TrrntZip ? DatFileType.DirTorrentZip : DatFileType.DirRVZip) { Name = Path.GetFileNameWithoutExtension(f.Name), DGame = new DatGame() }; ZipDir.DGame.Description = ZipDir.Name; thisDir.ChildAdd(ZipDir); FileScan fs = new FileScan(); List <FileScan.FileResults> fr = fs.Scan(zf1, true, true); bool isTorrentZipDate = true; for (int i = 0; i < fr.Count; i++) { if (fr[i].FileStatus != ZipReturn.ZipGood) { Console.WriteLine("File Error :" + zf1.Filename(i) + " : " + fr[i].FileStatus); continue; } DatFile df = new DatFile(DatFileType.FileTorrentZip) { Name = zf1.Filename(i), Size = fr[i].Size, CRC = fr[i].CRC, SHA1 = fr[i].SHA1, Date = zf1.LastModified(i).ToString("yyyy/MM/dd HH:mm:ss") //df.MD5 = zf.MD5(i) }; if (zf1.LastModified(i).Ticks != 629870671200000000) { isTorrentZipDate = false; } ZipDir.ChildAdd(df); } zf1.ZipFileClose(); if (isTorrentZipDate && ZipDir.DatFileType == DatFileType.DirRVZip) { ZipDir.DatFileType = DatFileType.DirTorrentZip; } if (ZipDir.DatFileType == DatFileType.DirTorrentZip) { DatSetCompressionType.SetZip(ZipDir); DatClean.RemoveUnNeededDirectoriesFromZip(ZipDir); } }