private static void AddFile(FileInfo f, DatDir thisDir) { Compress.File.File zf1 = new Compress.File.File(); zf1.ZipFileOpen(f.FullName, -1, true); FileScan fs = new FileScan(); List <FileScan.FileResults> fr = fs.Scan(zf1, true, true); DatFile df = new DatFile(DatFileType.File) { Name = f.Name, Size = fr[0].Size, CRC = fr[0].CRC, SHA1 = fr[0].SHA1 }; thisDir.ChildAdd(df); zf1.ZipFileClose(); }
private TrrntZipStatus OpenZip(FileInfo fi, out ICompress zipFile) { string ext = Path.GetExtension(fi.Name); switch (ext) { case ".7z": zipFile = new SevenZ(); break; case ".zip": zipFile = new Zip(); break; default: zipFile = new Compress.File.File(); break; } ZipReturn zr = zipFile.ZipFileOpen(fi.FullName, fi.LastWriteTime); if (zr != ZipReturn.ZipGood) { return(TrrntZipStatus.CorruptZip); } TrrntZipStatus tzStatus = TrrntZipStatus.Unknown; // first check if the file is a trrntip files if (zipFile.ZipStatus == ZipStatus.TrrntZip) { tzStatus |= TrrntZipStatus.ValidTrrntzip; } return(tzStatus); }
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); }