private static void ExtractGame(RvGame tGame, string outDir) { if (buffer == null) buffer = new byte[BufferSize]; ZipReturn zr; bool romGot = false; for (int rIndex = 0; rIndex < tGame.Roms.Count; rIndex++) { if (tGame.Roms[rIndex].FileId != null) { romGot = true; break; } } if (!romGot) return; // export the rom; ZipFile zipOut = new ZipFile(); string filename = Path.Combine(outDir, tGame.Name + ".zip"); filename = filename.Replace(@"/", @"\"); if (!Directory.Exists(filename)) { string dir = Path.GetDirectoryName(filename); Directory.CreateDirectory(dir); } zr = zipOut.ZipFileCreate(filename); if (zr != ZipReturn.ZipGood) { MessageBox.Show("Error creating " + Path.Combine(outDir, tGame.Name + ".zip") + " " + zr); return; } for (int rIndex = 0; rIndex < tGame.Roms.Count; rIndex++) { RvRom tRom = tGame.Roms[rIndex]; if (tRom.FileId != null) { GZip sourceGZip = new GZip(); string sha1 = Getfilename(GetFile.Execute((uint)tRom.FileId)); zr = sourceGZip.ReadGZip(sha1, false); if (zr != ZipReturn.ZipGood) { sourceGZip.Close(); continue; } Stream outStream; zipOut.ZipFileOpenWriteStream(true, true, tRom.Name, sourceGZip.uncompressedSize, 8, out outStream); Stream gZipStream; zr = sourceGZip.GetRawStream(out gZipStream); if (zr == ZipReturn.ZipGood) { // write the gzip stream to the zipstream ulong sizetogo = sourceGZip.compressedSize; while (sizetogo > 0) { int sizenow = sizetogo > BufferSize ? (int)BufferSize : (int)sizetogo; gZipStream.Read(buffer, 0, sizenow); outStream.Write(buffer, 0, sizenow); sizetogo = sizetogo - (ulong)sizenow; } } sourceGZip.Close(); zipOut.ZipFileCloseWriteStream(sourceGZip.crc); } } zipOut.ZipFileClose(); }
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); }
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 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) { } }