private static FindStatus fileneededTest(RvFile tFile) { // first check to see if we already have it in the file table bool inFileDB = FindInFiles.Execute(tFile); // returns true if found in File table if (inFileDB) return FindStatus.FoundFileInArchive; // now check if needed in any ROMs return FindInROMs.Execute(tFile) ? FindStatus.FileNeededInArchive : FindStatus.FileUnknown; }
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.ReadGZip(f.FullName, true); gZipTest.sha1Hash = VarFix.CleanMD5SHA1(Path.GetFileNameWithoutExtension(f.Name), 40); if (errorcode != ZipReturn.ZipGood) { _bgw.ReportProgress(0,new bgwShowError(f.FullName,"gz File corrupt")); continue; } RvFile tFile = new RvFile(); tFile.CRC = gZipTest.crc; tFile.MD5 = gZipTest.md5Hash; tFile.SHA1 = gZipTest.sha1Hash; tFile.Size = gZipTest.uncompressedSize; tFile.CompressedSize = gZipTest.compressedSize; FindStatus res = fileneededTest(tFile); if (res != FindStatus.FoundFileInArchive) tFile.DBWrite(); } if (_bgw.CancellationPending) return; } DirectoryInfo[] childdi = di.GetDirectories(); foreach (DirectoryInfo d in childdi) { if (_bgw.CancellationPending) return; ScanRomRoot(d.FullName); } }
private static FindStatus fileneededTest(RvFile tFile) { // first check to see if we already have it in the file table bool inFileDB = FindInFiles.Execute(tFile); // returns true if found in File table return inFileDB ? FindStatus.FoundFileInArchive : FindStatus.FileNeededInArchive; }
public static RvFile CheckSumRead(Stream ds, int offset) { ds.Position = 0; RvFile file=new RvFile(); CRC32Hash crc32 = new CRC32Hash(); MD5 md5 = MD5.Create(); SHA1 sha1 = SHA1.Create(); CRC32Hash altCrc32 = null; MD5 altMd5 = null; SHA1 altSha1 = null; if (offset > 0) { altCrc32 = new CRC32Hash(); altMd5 = MD5.Create(); altSha1 = SHA1.Create(); } file.Size = (ulong)ds.Length; long sizetogo = ds.Length; // just read header into main Hash if (offset > 0) { int sizenow = sizetogo > offset ? offset : (int)sizetogo; ds.Read(Buffer, 0, sizenow); crc32.TransformBlock(Buffer, 0, sizenow, null, 0); md5.TransformBlock(Buffer, 0, sizenow, null, 0); sha1.TransformBlock(Buffer, 0, sizenow, null, 0); sizetogo -= sizenow; } while (sizetogo > 0) { int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo; ds.Read(Buffer, 0, sizenow); crc32.TransformBlock(Buffer, 0, sizenow, null, 0); md5.TransformBlock(Buffer, 0, sizenow, null, 0); sha1.TransformBlock(Buffer, 0, sizenow, null, 0); if (offset > 0) { altCrc32.TransformBlock(Buffer, 0, sizenow, null, 0); altMd5.TransformBlock(Buffer, 0, sizenow, null, 0); altSha1.TransformBlock(Buffer, 0, sizenow, null, 0); } sizetogo -= sizenow; } crc32.TransformFinalBlock(Buffer, 0, 0); md5.TransformFinalBlock(Buffer, 0, 0); sha1.TransformFinalBlock(Buffer, 0, 0); file.CRC = crc32.Hash; file.MD5 = md5.Hash; file.SHA1 = sha1.Hash; if (offset > 0) { altCrc32.TransformFinalBlock(Buffer, 0, 0); altMd5.TransformFinalBlock(Buffer, 0, 0); altSha1.TransformFinalBlock(Buffer, 0, 0); file.AltSize = (ulong?)(ds.Length - offset); file.AltCRC = altCrc32.Hash; file.AltMD5 = altMd5.Hash; file.AltSHA1 = altSha1.Hash; } else { file.AltSize = null; file.AltCRC = null; file.AltSHA1 = null; file.AltMD5 = null; } ds.Close(); return file; }
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); }