private static bool ReadXMLDat(string fullname, ReportError ErrorReport, out DatHeader rvDat) { rvDat = null; int errorCode = FileStream.OpenFileRead(fullname, out Stream fs); if (errorCode != 0) { ErrorReport?.Invoke(fullname, errorCode + ": " + new Win32Exception(errorCode).Message); return(false); } bool retVal = false; try { retVal = ReadXMLDatFromStream(fs, fullname, ErrorReport, out rvDat); } catch (Exception e) { fs.Close(); fs.Dispose(); ErrorReport?.Invoke(fullname, $"Error Occured Reading Dat:\r\n{e.Message}\r\n"); return(false); } fs.Close(); fs.Dispose(); return(retVal); }
private static bool ReadXMLDat(ref RvDir tDat, string strFilename) { int errorCode = FileStream.OpenFileRead(strFilename, out Stream fs); if (errorCode != 0) { _bgw.ReportProgress(0, new bgwShowError(strFilename, errorCode + ": " + new Win32Exception(errorCode).Message)); return(false); } XmlDocument doc = new XmlDocument { XmlResolver = null }; try { doc.Load(fs); } catch (Exception e) { fs.Close(); fs.Dispose(); _bgw.ReportProgress(0, new bgwShowError(strFilename, string.Format("Error Occured Reading Dat:\r\n{0}\r\n", e.Message))); return(false); } fs.Close(); fs.Dispose(); if (doc.DocumentElement == null) { return(false); } XmlNode mame = doc.SelectSingleNode("mame"); if (mame != null) { return(DatXmlReader.ReadMameDat(ref tDat, doc)); } if (doc.DocumentElement != null) { XmlNode head = doc.DocumentElement.SelectSingleNode("header"); if (head != null) { return(DatXmlReader.ReadDat(ref tDat, doc)); } } XmlNodeList headList = doc.SelectNodes("softwarelist"); if (headList != null) { return(DatMessXmlReader.ReadDat(ref tDat, doc)); } return(false); }
// if we are fixing a zip/7z file then we use zipFileOut to open an output compressed stream // if we are just making a file then we use filenameOut to open an output filestream private static ReturnCode OpenOutputStream(RvFile fileOut, RvFile fileIn, ICompress zipFileOut, string filenameOut, ushort compressionMethod, bool rawCopy, bool sourceTrrntzip, long?dateTime, out Stream writeStream, out string error) { writeStream = null; if (fileOut.FileType == FileType.ZipFile || fileOut.FileType == FileType.SevenZipFile) { // if ZipFileOut == null then we have not open the output zip yet so open it from writing. if (zipFileOut == null) { error = "zipFileOut cannot be null"; return(ReturnCode.FileSystemError); } if (zipFileOut.ZipOpen != ZipOpenType.OpenWrite) { error = "Output Zip File is not set to OpenWrite, Logic Error."; return(ReturnCode.LogicError); } if (fileIn.Size == null) { error = "Null File Size found in Fixing File :" + fileIn.FullName; return(ReturnCode.LogicError); } TimeStamps ts = null; if (dateTime != null) { ts = new TimeStamps { ModTime = dateTime }; } ZipReturn zr = zipFileOut.ZipFileOpenWriteStream(rawCopy, sourceTrrntzip, fileOut.Name, (ulong)fileIn.Size, compressionMethod, out writeStream, ts); if (zr != ZipReturn.ZipGood) { error = "Error Opening Write Stream " + zr; return(ReturnCode.FileSystemError); } } else { if (File.Exists(filenameOut) && fileOut.GotStatus != GotStatus.Corrupt) { error = "Rescan needed, File Changed :" + filenameOut; return(ReturnCode.RescanNeeded); } int errorCode = FileStream.OpenFileWrite(filenameOut, out writeStream); if (errorCode != 0) { error = new Win32Exception(errorCode).Message + ". " + filenameOut; return(ReturnCode.FileSystemError); } } error = ""; return(ReturnCode.Good); }
public static bool ReadDat(string fullname, BackgroundWorker bgw, out RvDat rvDat) { _bgw = bgw; rvDat = null; Console.WriteLine("Reading " + fullname); int errorCode = FileStream.OpenFileRead(fullname, out Stream fs); if (errorCode != 0) { _bgw.ReportProgress(0, new bgwShowError(fullname, errorCode + ": " + new Win32Exception(errorCode).Message)); return(false); } StreamReader myfile = new StreamReader(fs, Program.Enc); string strLine = myfile.ReadLine(); myfile.Close(); fs.Close(); fs.Dispose(); if (strLine == null) { return(false); } if (strLine.ToLower().IndexOf("xml", StringComparison.Ordinal) >= 0) { if (!ReadXMLDat(fullname, out rvDat)) { return(false); } } else if ((strLine.ToLower().IndexOf("clrmamepro", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("romvault", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("game", StringComparison.Ordinal) >= 0)) { if (!DatCmpReader.ReadDat(fullname, out rvDat)) { return(false); } } else if (strLine.ToLower().IndexOf("doscenter", StringComparison.Ordinal) >= 0) { // if (!DatDOSReader.ReadDat(datFullName)) // return; } else { _bgw.ReportProgress(0, new bgwShowError(fullname, "Invalid DAT File")); return(false); } return(true); }
private static bool ReadXMLDat(string fullname, ReportError ErrorReport, out DatHeader rvDat) { rvDat = null; int errorCode = FileStream.OpenFileRead(fullname, out Stream fs); if (errorCode != 0) { ErrorReport?.Invoke(fullname, errorCode + ": " + new Win32Exception(errorCode).Message); return(false); } XmlDocument doc = new XmlDocument { XmlResolver = null }; try { doc.Load(fs); } catch (Exception e) { fs.Close(); fs.Dispose(); ErrorReport?.Invoke(fullname, $"Error Occured Reading Dat:\r\n{e.Message}\r\n"); return(false); } fs.Close(); fs.Dispose(); if (doc.DocumentElement == null) { return(false); } XmlNode mame = doc.SelectSingleNode("mame"); if (mame != null) { return(DatXmlReader.ReadMameDat(doc, fullname, out rvDat)); } XmlNode head = doc.DocumentElement?.SelectSingleNode("header"); if (head != null) { return(DatXmlReader.ReadDat(doc, fullname, out rvDat)); } XmlNodeList headList = doc.SelectNodes("softwarelist"); if (headList != null) { return(DatMessXmlReader.ReadDat(doc, fullname, out rvDat)); } return(false); }
public ZipReturn ZipFileOpen(string newFilename, long timestamp, bool readHeaders) { ZipFileClose(); ZipStatus = ZipStatus.None; _zip64 = false; _centralDirStart = 0; _centralDirSize = 0; _zipFileInfo = null; try { if (!RVIO.File.Exists(newFilename)) { ZipFileClose(); return(ZipReturn.ZipErrorFileNotFound); } _zipFileInfo = new FileInfo(newFilename); if (timestamp != -1 && _zipFileInfo.LastWriteTime != timestamp) { ZipFileClose(); return(ZipReturn.ZipErrorTimeStamp); } int errorCode = FileStream.OpenFileRead(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); if (errorCode == 32) { return(ZipReturn.ZipFileLocked); } return(ZipReturn.ZipErrorOpeningFile); } } catch (PathTooLongException) { ZipFileClose(); return(ZipReturn.ZipFileNameToLong); } catch (IOException) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenRead; if (!readHeaders) { return(ZipReturn.ZipGood); } return(ZipFileReadHeaders()); }
public static int LoadDat(string strFilename) { Filename = strFilename; _streamReader = null; int errorCode = FileStream.OpenFileRead(strFilename, out _fileStream); if (errorCode != 0) { return(errorCode); } _streamReader = new StreamReader(_fileStream, Program.Enc); return(0); }
public ZipReturn ZipFileCreate(string newFilename, bool compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64) { if (ZipOpen != ZipOpenType.Closed) { return(ZipReturn.ZipFileAlreadyOpen); } DirUtil.CreateDirForFile(newFilename); _zipFileInfo = new FileInfo(newFilename); int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenWrite; _signatureHeader = new SignatureHeader(); _header = new Header(); using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) { _signatureHeader.Write(bw); } _baseOffset = _zipFs.Position; _compressed = compressOutput; _unpackedStreamSize = 0; if (_compressed) { LzmaEncoderProperties ep = new LzmaEncoderProperties(true, dictionarySize, numFastBytes); LzmaStream lzs = new LzmaStream(ep, false, _zipFs); _codeMSbytes = lzs.Properties; _lzmaStream = lzs; /* * ZstandardStream zss = new ZstandardStream(_zipFs, 22, true); * _codeMSbytes = new byte[] { 1, 4, 18, 0, 0 }; * _lzmaStream = zss; */ _packStreamStart = (ulong)_zipFs.Position; } return(ZipReturn.ZipGood); }
public ZipReturn ZipFileOpen(string filename, long timestamp, bool readHeaders) { ZipFileClose(); _memoryZipFile = null; Debug.WriteLine(filename); #region open file stream try { if (!RVIO.File.Exists(filename)) { ZipFileClose(); return(ZipReturn.ZipErrorFileNotFound); } _zipFileInfo = new FileInfo(filename); if ((timestamp != -1) && (_zipFileInfo.LastWriteTime != timestamp)) { ZipFileClose(); return(ZipReturn.ZipErrorTimeStamp); } int errorCode = FileStream.OpenFileRead(filename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } } catch (PathTooLongException) { ZipFileClose(); return(ZipReturn.ZipFileNameToLong); } catch (IOException) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } #endregion ZipOpen = ZipOpenType.OpenRead; ZipStatus = ZipStatus.None; return(ZipFileReadHeaders()); }
public ZipReturn ZipFileCreate(string newFilename) { if (ZipOpen != ZipOpenType.Closed) { return(ZipReturn.ZipFileAlreadyOpen); } CompressUtils.CreateDirForFile(newFilename); _fileInfo = new FileInfo(newFilename); int errorCode = FileStream.OpenFileWrite(newFilename, out _inStream); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenWrite; return(ZipReturn.ZipGood); }
private Stream CloneStream(Stream s) { switch (s) { case System.IO.FileStream _: int errorCode = FileStream.OpenFileRead(ZipFilename, out Stream streamOut); return(errorCode != 0 ? null : streamOut); case MemoryStream memStream: long pos = memStream.Position; memStream.Position = 0; byte[] newStream = new byte[memStream.Length]; memStream.Read(newStream, 0, (int)memStream.Length); MemoryStream ret = new MemoryStream(newStream, false); memStream.Position = pos; return(ret); } return(null); }
public ZipReturn ReadGZip(string filename, bool deepScan) { _filename = ""; if (!File.Exists(filename)) { return(ZipReturn.ZipErrorFileNotFound); } _filename = filename; int errorCode = FileStream.OpenFileRead(filename, out _zipFs); if (errorCode != 0) { if (errorCode == 32) { return(ZipReturn.ZipFileLocked); } return(ZipReturn.ZipErrorOpeningFile); } return(ReadBody(deepScan)); }
public ZipReturn ZipFileCreate(string newFilename, bool compressOutput) { if (ZipOpen != ZipOpenType.Closed) { return(ZipReturn.ZipFileAlreadyOpen); } DirUtil.CreateDirForFile(newFilename); _zipFileInfo = new FileInfo(newFilename); int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenWrite; _signatureHeader = new SignatureHeader(); _header = new Header(); BinaryWriter bw = new BinaryWriter(_zipFs); _signatureHeader.Write(bw); _compressed = compressOutput; _unpackedStreamSize = 0; if (_compressed) { LzmaEncoderProperties ep = new LzmaEncoderProperties(true, 1 << 24, 64); _lzmaStream = new LzmaStream(ep, false, _zipFs); _codeMSbytes = _lzmaStream.Properties; _packStreamStart = (ulong)_zipFs.Position; } return(ZipReturn.ZipGood); }
public ZipReturn GetRawStream(out Stream st) { st = null; if (!RVIO.File.Exists(_zipFileInfo.FullName)) { return(ZipReturn.ZipErrorFileNotFound); } int errorCode = FileStream.OpenFileRead(_zipFileInfo.FullName, out st); if (errorCode != 0) { if (errorCode == 32) { return(ZipReturn.ZipFileLocked); } return(ZipReturn.ZipErrorOpeningFile); } st.Position = dataStartPos; return(ZipReturn.ZipGood); }
public ZipReturn ZipFileCreate(string newFilename, SevenZipCompressType compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64) { if (ZipOpen != ZipOpenType.Closed) { return(ZipReturn.ZipFileAlreadyOpen); } CompressUtils.CreateDirForFile(newFilename); _zipFileInfo = new FileInfo(newFilename); int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenWrite; _signatureHeader = new SignatureHeader(); _header = new Header(); using (BinaryWriter bw = new(_zipFs, Encoding.UTF8, true)) { _signatureHeader.Write(bw); } _baseOffset = _zipFs.Position; _packedOutStreams = new List <outStreams>(); _compType = compressOutput; #if solid outStreams newStream = new() { packedStart = (ulong)_zipFs.Position, compType = compressOutput, packedSize = 0, unpackedStreams = new List <UnpackedStreamInfo>() }; switch (compressOutput) { case SevenZipCompressType.lzma: LzmaEncoderProperties ep = new(true, dictionarySize, numFastBytes); LzmaStream lzs = new(ep, false, _zipFs); newStream.Method = new byte[] { 3, 1, 1 }; newStream.Properties = lzs.Properties; _compressStream = lzs; break; case SevenZipCompressType.zstd: ZstdSharp.CompressionStream zss = new(_zipFs, 19); newStream.Method = new byte[] { 4, 247, 17, 1 }; newStream.Properties = new byte[] { 1, 5, 19, 0, 0 }; _compressStream = zss; break; case SevenZipCompressType.uncompressed: newStream.Method = new byte[] { 0 }; newStream.Properties = null; _compressStream = _zipFs; break; } _packedOutStreams.Add(newStream); #endif return(ZipReturn.ZipGood); }
public ZipReturn WriteGZip(string filename, Stream sInput, bool isCompressedStream) { CreateDirForFile(filename); Stream _zipFs; FileStream.OpenFileWrite(filename, out _zipFs); using (BinaryWriter zipBw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) { zipBw.Write((byte)0x1f); // ID1 = 0x1f zipBw.Write((byte)0x8b); // ID2 = 0x8b zipBw.Write((byte)0x08); // CM = 0x08 zipBw.Write((byte)0x04); // FLG = 0x04 zipBw.Write((uint)0); // MTime = 0 zipBw.Write((byte)0x00); // XFL = 0x00 zipBw.Write((byte)0xff); // OS = 0x00 // writing FEXTRA if (FileHeaderReader.FileHeaderReader.AltHeaderFile(altType)) { zipBw.Write((short)77); // XLEN 16+4+8+1+16+20+4+8 } else { zipBw.Write((short)28); // XLEN 16+4+8 } zipBw.Write(md5Hash); // 16 bytes zipBw.Write(crc); // 4 bytes zipBw.Write(uncompressedSize); // 8 bytes if (FileHeaderReader.FileHeaderReader.AltHeaderFile(altType)) { zipBw.Write((byte)altType); // 1 zipBw.Write(altmd5Hash); // 16 zipBw.Write(altsha1Hash); // 20 zipBw.Write(altcrc); // 4 zipBw.Write((ulong)uncompressedAltSize); // 8 } if (Buffer0 == null) { Buffer0 = new byte[Buffersize]; } ulong dataStartPos = (ulong)zipBw.BaseStream.Position; if (isCompressedStream) { ulong sizetogo = compressedSize; while (sizetogo > 0) { int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo; sInput.Read(Buffer0, 0, sizenow); _zipFs.Write(Buffer0, 0, sizenow); sizetogo -= (ulong)sizenow; } } else { if (uncompressedSize == 0) { _zipFs.WriteByte(03); _zipFs.WriteByte(00); } else { ulong sizetogo = uncompressedSize; Stream writeStream = new ZlibBaseStream(_zipFs, CompressionMode.Compress, CompressionLevel.BestCompression, ZlibStreamFlavor.DEFLATE, true); while (sizetogo > 0) { int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo; sInput.Read(Buffer0, 0, sizenow); writeStream.Write(Buffer0, 0, sizenow); sizetogo -= (ulong)sizenow; } writeStream.Flush(); writeStream.Close(); writeStream.Dispose(); } } compressedSize = (ulong)zipBw.BaseStream.Position - dataStartPos; zipBw.Write(crc[3]); zipBw.Write(crc[2]); zipBw.Write(crc[1]); zipBw.Write(crc[0]); zipBw.Write((uint)uncompressedSize); zipBw.Flush(); zipBw.Close(); } _zipFs.Close(); return(ZipReturn.ZipGood); }
public static ReturnCode DecompressSource7ZipFile(RvFile zZipFileIn, bool includeGood, out string error) { byte[] buffer = new byte[BufferSize]; RvFile cacheDir = DB.RvFileCache(); string fileNameIn = zZipFileIn.FullName; SevenZ zipFileIn = new SevenZ(); ZipReturn zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.TimeStamp, true); if (zr1 != ZipReturn.ZipGood) { error = "Error opening 7zip file for caching"; return(ReturnCode.RescanNeeded); } RvFile outDir = new RvFile(FileType.Dir) { Name = zZipFileIn.Name + ".cache", Parent = cacheDir, DatStatus = DatStatus.InToSort, GotStatus = GotStatus.Got }; int nameDirIndex = 0; while (cacheDir.ChildNameSearch(outDir, out int index) == 0) { nameDirIndex++; outDir.Name = zZipFileIn.Name + ".cache (" + nameDirIndex + ")"; } cacheDir.ChildAdd(outDir); Directory.CreateDirectory(outDir.FullName); for (int i = 0; i < zipFileIn.LocalFilesCount(); i++) { if (zZipFileIn.Child(i).IsDir) { continue; } RvFile thisFile = null; for (int j = 0; j < zZipFileIn.ChildCount; j++) { if (zZipFileIn.Child(j).ZipFileIndex != i) { continue; } thisFile = zZipFileIn.Child(j); break; } if (thisFile == null) { error = "Error opening 7zip file for caching"; return(ReturnCode.RescanNeeded); } bool extract = true; // first check to see if we have a file version of this compressed file somewhere else. foreach (RvFile f in thisFile.FileGroup.Files) { if (f.FileType == FileType.File && f.GotStatus == GotStatus.Got) { extract = false; } } if (!extract) { continue; } extract = false; if (includeGood) { // if this is the file we are fixing then pull out the correct files. if (thisFile.RepStatus == RepStatus.Correct) { extract = true; } } // next check to see if we need this extracted to fix another file foreach (RvFile f in thisFile.FileGroup.Files) { if (f.RepStatus == RepStatus.CanBeFixed) { extract = true; break; } } if (!extract) { continue; } string cleanedName = thisFile.Name; cleanedName = cleanedName.Replace("/", "-"); cleanedName = cleanedName.Replace("\\", "-"); RvFile outFile = new RvFile(FileType.File) { Name = cleanedName, Size = thisFile.Size, CRC = thisFile.CRC, SHA1 = thisFile.SHA1, MD5 = thisFile.MD5, HeaderFileType = thisFile.HeaderFileType, AltSize = thisFile.AltSize, AltCRC = thisFile.AltCRC, AltSHA1 = thisFile.AltSHA1, AltMD5 = thisFile.AltMD5, FileGroup = thisFile.FileGroup }; outFile.SetStatus(DatStatus.InToSort, GotStatus.Got); outFile.FileStatusSet( FileStatus.HeaderFileTypeFromHeader | FileStatus.SizeFromHeader | FileStatus.SizeVerified | FileStatus.CRCFromHeader | FileStatus.CRCVerified | FileStatus.SHA1FromHeader | FileStatus.SHA1Verified | FileStatus.MD5FromHeader | FileStatus.MD5Verified | FileStatus.AltSizeFromHeader | FileStatus.AltSizeVerified | FileStatus.AltCRCFromHeader | FileStatus.AltCRCVerified | FileStatus.AltSHA1FromHeader | FileStatus.AltSHA1Verified | FileStatus.AltMD5FromHeader | FileStatus.AltMD5Verified , thisFile); outFile.RepStatus = RepStatus.NeededForFix; zipFileIn.ZipFileOpenReadStream(i, out Stream readStream, out ulong unCompressedSize); string filenameOut = Path.Combine(outDir.FullName, outFile.Name); ThreadMD5 tmd5 = null; ThreadSHA1 tsha1 = null; ThreadCRC tcrc32 = new ThreadCRC(); if (Settings.rvSettings.FixLevel != EFixLevel.Level1 && Settings.rvSettings.FixLevel != EFixLevel.TrrntZipLevel1) { tmd5 = new ThreadMD5(); tsha1 = new ThreadSHA1(); } int errorCode = FileStream.OpenFileWrite(filenameOut, out Stream writeStream); ulong sizetogo = unCompressedSize; while (sizetogo > 0) { int sizenow = sizetogo > BufferSize ? BufferSize : (int)sizetogo; try { readStream.Read(buffer, 0, sizenow); } catch (Exception ex) { if (ex is ZlibException || ex is DataErrorException) { ZipReturn zr = zipFileIn.ZipFileCloseReadStream(); if (zr != ZipReturn.ZipGood) { error = "Error Closing " + zr + " Stream :" + zipFileIn.ZipFilename; return(ReturnCode.FileSystemError); } zipFileIn.ZipFileClose(); writeStream.Flush(); writeStream.Close(); if (filenameOut != null) { File.Delete(filenameOut); } thisFile.GotStatus = GotStatus.Corrupt; error = "Unexpected corrupt archive file found:\n" + zZipFileIn.FullName + "\nRun Find Fixes, and Fix to continue fixing correctly."; return(ReturnCode.SourceDataStreamCorrupt); } error = "Error reading Source File " + ex.Message; return(ReturnCode.FileSystemError); } tcrc32.Trigger(buffer, sizenow); tmd5?.Trigger(buffer, sizenow); tsha1?.Trigger(buffer, sizenow); tcrc32.Wait(); tmd5?.Wait(); tsha1?.Wait(); try { writeStream.Write(buffer, 0, sizenow); } catch (Exception e) { error = "Error writing out file. " + Environment.NewLine + e.Message; return(ReturnCode.FileSystemError); } sizetogo = sizetogo - (ulong)sizenow; } writeStream.Flush(); writeStream.Close(); writeStream.Dispose(); tcrc32.Finish(); tmd5?.Finish(); tsha1?.Finish(); byte[] bCRC = tcrc32.Hash; byte[] bMD5 = tmd5?.Hash; byte[] bSHA1 = tsha1?.Hash; tcrc32.Dispose(); tmd5?.Dispose(); tsha1?.Dispose(); FileInfo fi = new FileInfo(filenameOut); outFile.TimeStamp = fi.LastWriteTime; if (bCRC != null && thisFile.CRC != null && !ArrByte.BCompare(bCRC, thisFile.CRC)) { // error in file. } if (bMD5 != null && thisFile.MD5 != null && !ArrByte.BCompare(bMD5, thisFile.MD5)) { // error in file. } if (bSHA1 != null && thisFile.SHA1 != null && !ArrByte.BCompare(bSHA1, thisFile.SHA1)) { // error in file. } thisFile.FileGroup.Files.Add(outFile); outDir.ChildAdd(outFile); } zipFileIn.ZipFileClose(); error = ""; return(ReturnCode.Good); }
private static ReturnCode OpenInputStream(RvFile fileIn, bool rawCopy, out ICompress zipFileIn, out bool sourceTrrntzip, out Stream readStream, out ulong streamSize, out ushort compressionMethod, out string error) { zipFileIn = null; sourceTrrntzip = false; readStream = null; streamSize = 0; compressionMethod = 0; if (fileIn.FileType == FileType.ZipFile || fileIn.FileType == FileType.SevenZipFile) // Input is a ZipFile { RvFile zZipFileIn = fileIn.Parent; if (zZipFileIn.FileType != DBTypeGet.DirFromFile(fileIn.FileType)) { error = "File Open but Source File is not correct type, Logic Error."; return(ReturnCode.LogicError); } string fileNameIn = zZipFileIn.FullNameCase; ZipReturn zr1; if (zZipFileIn.FileType == FileType.SevenZip) { sourceTrrntzip = false; zipFileIn = new SevenZ(); zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.FileModTimeStamp); } else { sourceTrrntzip = (zZipFileIn.ZipStatus & ZipStatus.TrrntZip) == ZipStatus.TrrntZip; zipFileIn = new Zip(); zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.FileModTimeStamp, fileIn.ZipFileHeaderPosition == null); } switch (zr1) { case ZipReturn.ZipGood: break; case ZipReturn.ZipErrorFileNotFound: error = "File not found, Rescan required before fixing " + fileIn.Name; return(ReturnCode.RescanNeeded); case ZipReturn.ZipErrorTimeStamp: error = "File has changed, Rescan required before fixing " + fileIn.Name; return(ReturnCode.RescanNeeded); default: error = "Error Open Zip" + zr1 + ", with File " + fileIn.DatTreeFullName; return(ReturnCode.FileSystemError); } if (fileIn.FileType == FileType.SevenZipFile) { ((SevenZ)zipFileIn).ZipFileOpenReadStream(fileIn.ZipFileIndex, out readStream, out streamSize); } else { if (fileIn.ZipFileHeaderPosition != null) { ((Zip)zipFileIn).ZipFileOpenReadStreamQuick((ulong)fileIn.ZipFileHeaderPosition, rawCopy, out readStream, out streamSize, out compressionMethod); } else { ((Zip)zipFileIn).ZipFileOpenReadStream(fileIn.ZipFileIndex, rawCopy, out readStream, out streamSize, out compressionMethod); } } } else // Input is a regular file { string fileNameIn = fileIn.FullName; if (!File.Exists(fileNameIn)) { error = "Rescan needed, File Not Found :" + fileNameIn; return(ReturnCode.RescanNeeded); } FileInfo fileInInfo = new FileInfo(fileNameIn); if (fileInInfo.LastWriteTime != fileIn.FileModTimeStamp) { error = "Rescan needed, File Changed :" + fileNameIn; return(ReturnCode.RescanNeeded); } int errorCode = FileStream.OpenFileRead(fileNameIn, out readStream); if (errorCode != 0) { error = new Win32Exception(errorCode).Message + ". " + fileNameIn; return(ReturnCode.FileSystemError); } if (fileIn.Size == null) { error = "Null File Size found in Fixing File :" + fileNameIn; return(ReturnCode.LogicError); } streamSize = (ulong)fileIn.Size; if ((ulong)readStream.Length != streamSize) { error = "Rescan needed, File Length Changed :" + fileNameIn; return(ReturnCode.RescanNeeded); } } error = ""; return(ReturnCode.Good); }
public static RvDir ReadInDatFile(RvDat datFile, BackgroundWorker bgw) { _bgw = bgw; string datFullName = datFile.GetData(RvDat.DatData.DatFullName); /* * DatRead dr = new DatRead * { * ErrorReport = ReadError * }; * DatHeader dh; * dr.ReadDat(datFullName, out dh); */ RvDir newDir = new RvDir(FileType.Dir); int errorCode = FileStream.OpenFileRead(datFullName, out Stream fs); if (errorCode != 0) { _bgw.ReportProgress(0, new bgwShowError(datFullName, errorCode + ": " + new Win32Exception(errorCode).Message)); return(null); } StreamReader myfile = new StreamReader(fs, Program.Enc); string strLine = myfile.ReadLine(); myfile.Close(); fs.Close(); fs.Dispose(); if (strLine == null) { return(null); } if (strLine.ToLower().IndexOf("xml", StringComparison.Ordinal) >= 0) { if (!ReadXMLDat(ref newDir, datFullName)) { return(null); } } else if ((strLine.ToLower().IndexOf("clrmamepro", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("romvault", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("game", StringComparison.Ordinal) >= 0)) { if (!DatCmpReader.ReadDat(ref newDir, datFullName)) { return(null); } } else if (strLine.ToLower().IndexOf("doscenter", StringComparison.Ordinal) >= 0) { if (!DatDOSReader.ReadDat(ref newDir, datFullName)) { return(null); } } else { _bgw.ReportProgress(0, new bgwShowError(datFullName, "Invalid DAT File")); return(null); } if (newDir.Dat == null) { _bgw.ReportProgress(0, new bgwShowError(datFullName, "Invalid Header")); return(null); } /* * DatCleaner.DatSetRemoveUnneededDirs(dh.BaseDir); * DatCleaner.DatSetCheckParentSets(dh.BaseDir); * DatCleaner.DatSetRemoveUnneededDirs(dh.BaseDir); * if (dh.MergeType=="full") * DatCleaner.DatSetMergeSets(dh.BaseDir); * * DatCleaner.DatSetCheckCollect(dh.BaseDir); */ newDir.Dat.AddData(RvDat.DatData.DatFullName, datFullName); newDir.Dat.TimeStamp = datFile.TimeStamp; newDir.Dat.Status = DatUpdateStatus.Correct; DatSetRemoveUnneededDirs(newDir); DatSetCheckParentSets(newDir); DatSetRenameAndRemoveDups(newDir); if (newDir.Dat.GetData(RvDat.DatData.MergeType).ToLower() == "full") { DatSetMergeSets(newDir); } DatSetCheckCollect(newDir); DatSetCreateSubDirs(newDir); if (newDir.Dat.GetData(RvDat.DatData.DirSetup).ToLower() == "nogame") { DatSetRemoveGameDir(newDir); } return(newDir); }
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); }
public static void extract(string dirName, string outPath) { if (CommandFindRomsInGame == null) { CommandFindRomsInGame = new SQLiteCommand( @"SELECT ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", DBSqlite.db.Connection); } CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId")); Debug.WriteLine(dirName); SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", DBSqlite.db.Connection); DbDataReader reader = getfiles.ExecuteReader(); while (reader.Read()) { string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip"; outputFile = outputFile.Substring(dirName.Length); outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\"); Debug.WriteLine(outputFile); int GameId = Convert.ToInt32(reader["GameId"]); string GameName = reader["name"].ToString(); Debug.WriteLine("Game " + GameId + " Name: " + GameName); Zip memZip = new Zip(); memZip.ZipCreateFake(); ulong fileOffset = 0; Stream zipFs = null; int romCount = 0; using (DbDataReader drRom = ZipSetGetRomsInGame(GameId)) { while (drRom.Read()) { int RomId = Convert.ToInt32(drRom["RomId"]); string RomName = drRom["name"].ToString(); ulong size = Convert.ToUInt64(drRom["size"]); ulong compressedSize = Convert.ToUInt64(drRom["compressedsize"]); byte[] CRC = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8); byte[] sha1 = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32); Debug.WriteLine(" Rom " + RomId + " Name: " + RomName + " Size: " + size + " Compressed: " + compressedSize + " CRC: " + VarFix.ToString(CRC)); byte[] localHeader; memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader); //ZipSetLocalFileHeader(RomId, localHeader, fileOffset); if (romCount == 0) { CompressUtils.CreateDirForFile(outputFile); int errorCode = FileStream.OpenFileWrite(outputFile, out zipFs); } zipFs.Write(localHeader, 0, localHeader.Length); gZip GZip = new gZip(); string strFilename = RomRootDir.Getfilename(sha1); GZip.ZipFileOpen(strFilename, -1, true); GZip.ZipFileOpenReadStream(0, true, out Stream oStr, out ulong _); StreamCopier.StreamCopy(oStr, zipFs, compressedSize); GZip.ZipFileCloseReadStream(); GZip.ZipFileClose(); fileOffset += (ulong)localHeader.Length + compressedSize; zipFs.Position = (long)fileOffset; romCount += 1; } } memZip.ZipFileCloseFake(fileOffset, out byte[] centeralDir); if (romCount > 0) { zipFs.Write(centeralDir, 0, centeralDir.Length); zipFs.Flush(); zipFs.Close(); zipFs.Dispose(); } } }
public static bool ReadDat(string fullname, BackgroundWorker bgw, out RvDat rvDat) { _bgw = bgw; rvDat = null; Console.WriteLine("Reading " + fullname); int errorCode = FileStream.OpenFileRead(fullname, out Stream fs); if (errorCode != 0) { _bgw.ReportProgress(0, new bgwShowError(fullname, errorCode + ": " + new Win32Exception(errorCode).Message)); return(false); } // If the file could be read, read the first line StreamReader myfile = new StreamReader(fs, Program.Enc); string strLine = myfile.ReadLine(); myfile.Close(); fs.Close(); fs.Dispose(); // If there's no first line, we don't have a readable file if (strLine == null) { return(false); } // XML-based DATs if (strLine.IndexOf("xml", StringComparison.OrdinalIgnoreCase) >= 0) { return(ReadXMLDat(fullname, out rvDat)); } // ClrMamePro DATs else if (strLine.IndexOf("clrmamepro", StringComparison.OrdinalIgnoreCase) >= 0 || strLine.IndexOf("romvault", StringComparison.OrdinalIgnoreCase) >= 0 || strLine.IndexOf("game", StringComparison.OrdinalIgnoreCase) >= 0) { return(DatCmpReader.ReadDat(fullname, out rvDat)); } // DOSCenter DATs else if (strLine.IndexOf("doscenter", StringComparison.OrdinalIgnoreCase) >= 0) { return(DatDOSReader.ReadDat(fullname, out rvDat)); } // RomCenter DATs else if (strLine.IndexOf("[", StringComparison.OrdinalIgnoreCase) >= 0 && strLine.IndexOf("]", StringComparison.OrdinalIgnoreCase) >= 0) { return(DatRcReader.ReadDat(fullname, out rvDat)); } // Unknown file / DAT type else { _bgw.ReportProgress(0, new bgwShowError(fullname, "Invalid DAT File")); return(false); } }
public bool FullExtract(string filename, string outDir) { MessageCallBack?.Invoke($"Processing file: {filename}"); if (!string.IsNullOrEmpty(outDir)) { MessageCallBack?.Invoke($"Output dir: {outDir}"); } string ext = Path.GetExtension(filename); ICompress z = null; switch (ext.ToLower()) { case ".zip": z = new Zip(); break; case ".7z": z = new SevenZ(); break; } if (z == null) { MessageCallBack?.Invoke($"Unknown file type {ext}"); return(false); } ZipReturn zRet = z.ZipFileOpen(filename); if (zRet != ZipReturn.ZipGood) { MessageCallBack?.Invoke($"Error opening archive {zRet}"); return(false); } ulong buflen = 409600; byte[] buffer = new byte[buflen]; for (int i = 0; i < z.LocalFilesCount(); i++) { LocalFile lf = z.GetLocalFile(i); byte[] cread = null; string filenameOut = lf.Filename; if (lf.IsDirectory) { string outFullDir = Path.Combine(outDir, filenameOut.Substring(0, filenameOut.Length - 1).Replace('/', '\\')); Directory.CreateDirectory(outFullDir); continue; } else { MessageCallBack?.Invoke($"Extracting {filenameOut}"); string fOut = Path.Combine(outDir, filenameOut.Replace('/', '\\')); string dOut = Path.GetDirectoryName(fOut); if (!string.IsNullOrWhiteSpace(dOut) && !Directory.Exists(dOut)) { Directory.CreateDirectory(dOut); } int errorCode = FileStream.OpenFileWrite(fOut, out Stream sWrite); if (errorCode != 0) { MessageCallBack?.Invoke($"Error opening outputfile {fOut}"); } z.ZipFileOpenReadStream(i, out Stream sRead, out _); CRC crc = new(); ulong sizeToGo = lf.UncompressedSize; while (sizeToGo > 0) { ulong sizeNow = sizeToGo > buflen ? buflen : sizeToGo; int sizeRead = sRead.Read(buffer, 0, (int)sizeNow); crc.SlurpBlock(buffer, 0, sizeRead); sWrite.Write(buffer, 0, sizeRead); sizeToGo -= (ulong)sizeRead; } sWrite.Close(); sWrite.Dispose(); cread = crc.Crc32ResultB; } byte[] fread = lf.CRC; if (cread[0] != fread[0] || cread[1] != fread[1] || cread[2] != fread[2] || cread[3] != fread[3]) { MessageCallBack?.Invoke($"CRC error. Expected {fread.ToHex()} found {cread.ToHex()}"); return(false); } } return(true); }
public static hdErr CheckFile(string file, string directory, bool isLinux, ref bool deepCheck, out uint?chdVersion, out byte[] chdSHA1, out byte[] chdMD5, ref bool fileErrorAbort) { chdSHA1 = null; chdMD5 = null; chdVersion = null; string filename = Path.Combine(directory, file); fileProcess?.Invoke(filename); //string ext = Path.GetExtension(filename).ToLower(); //if (ext != ".chd") //{ // return hdErr.HDERR_INVALID_FILE; //} if (!File.Exists(filename)) { fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened."); fileErrorAbort = true; return(hdErr.HDERR_CANNOT_OPEN_FILE); } Stream s; int retval = FileStream.OpenFileRead(filename, out s); if (retval != 0) { fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened."); fileErrorAbort = true; return(hdErr.HDERR_CANNOT_OPEN_FILE); } if (s == null) { fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened."); fileErrorAbort = true; return(hdErr.HDERR_CANNOT_OPEN_FILE); } if (s.Length < MaxHeader) { s.Close(); s.Dispose(); return(hdErr.HDERR_INVALID_FILE); } hard_disk_info hdi = new hard_disk_info(); hdErr res = ReadCHDHeader(s, ref hdi); if (res != hdErr.HDERR_NONE) { return(res); } chdVersion = hdi.version; chdMD5 = hdi.md5; chdSHA1 = hdi.sha1; if (!deepCheck) { s.Close(); s.Dispose(); return(res); } string error = null; if (hdi.version < 4 && hdi.compression < 3) { hdi.file = s; CHDLocalCheck clc = new CHDLocalCheck(); res = clc.ChdCheck(fileProgress, hdi, out error); s.Close(); s.Dispose(); } else { s.Close(); s.Dispose(); CHDManCheck cmc = new CHDManCheck(); res = cmc.ChdCheck(fileProgress, isLinux, filename, out error); } switch (res) { case hdErr.HDERR_NONE: break; case hdErr.HDERR_CHDMAN_NOT_FOUND: deepCheck = false; res = hdErr.HDERR_NONE; break; case hdErr.HDERR_DECOMPRESSION_ERROR: fileError?.Invoke(filename, error); break; case hdErr.HDERR_FILE_NOT_FOUND: fileSystemError?.Invoke("File: " + filename + " Error: Not Found scan Aborted."); fileErrorAbort = true; break; default: generalError?.Invoke(res + " " + error); break; } return(res); }
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); }
private static ReturnCode OpenOutputStream(RvFile fileOut, RvFile fileIn, ref ICompress zipFileOut, string zipFilenameOut, ushort compressionMethod, bool rawCopy, bool sourceTrrntzip, out Stream writeStream, out string error) { writeStream = null; if ((fileOut.FileType == FileType.ZipFile) || (fileOut.FileType == FileType.SevenZipFile)) { // if ZipFileOut == null then we have not open the output zip yet so open it from writing. if (zipFileOut == null) { if (Path.GetFileName(zipFilenameOut) == "__RomVault.tmp") { if (File.Exists(zipFilenameOut)) { File.Delete(zipFilenameOut); } } else if (File.Exists(zipFilenameOut)) { error = "Rescan needed, File Changed :" + zipFilenameOut; return(ReturnCode.RescanNeeded); } if (fileOut.FileType == FileType.ZipFile) { zipFileOut = new ZipFile(); } else { zipFileOut = new SevenZ(); } ZipReturn zrf = zipFileOut.ZipFileCreate(zipFilenameOut); if (zrf != ZipReturn.ZipGood) { error = "Error Opening Write Stream " + zrf; return(ReturnCode.FileSystemError); } } else { if (zipFileOut.ZipOpen != ZipOpenType.OpenWrite) { error = "Output Zip File is not set to OpenWrite, Logic Error."; return(ReturnCode.LogicError); } if (zipFileOut.ZipFilename != new FileInfo(zipFilenameOut).FullName) { error = "Output Zip file has changed name from " + zipFileOut.ZipFilename + " to " + zipFilenameOut + ". Logic Error"; return(ReturnCode.LogicError); } } if (fileIn.Size == null) { error = "Null File Size found in Fixing File :" + fileIn.FullName; return(ReturnCode.LogicError); } ZipReturn zr = zipFileOut.ZipFileOpenWriteStream(rawCopy, sourceTrrntzip, fileOut.Name, (ulong)fileIn.Size, compressionMethod, out writeStream); if (zr != ZipReturn.ZipGood) { error = "Error Opening Write Stream " + zr; return(ReturnCode.FileSystemError); } } else { if (File.Exists(zipFilenameOut) && (fileOut.GotStatus != GotStatus.Corrupt)) { error = "Rescan needed, File Changed :" + zipFilenameOut; return(ReturnCode.RescanNeeded); } int errorCode = FileStream.OpenFileWrite(zipFilenameOut, out writeStream); if (errorCode != 0) { error = new Win32Exception(errorCode).Message + ". " + zipFilenameOut; return(ReturnCode.FileSystemError); } } error = ""; return(ReturnCode.Good); }
public static void extract(string dirName) { FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); DialogResult result = folderBrowserDialog1.ShowDialog(); if (result != DialogResult.OK) { return; } string outPath = folderBrowserDialog1.SelectedPath; if (CommandFindRomsInGame == null) { CommandFindRomsInGame = new SQLiteCommand( @"SELECT ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", Program.db.Connection); } CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId")); byte[] buff = new byte[1024]; Debug.WriteLine(dirName); SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", Program.db.Connection); DbDataReader reader = getfiles.ExecuteReader(); while (reader.Read()) { string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip"; outputFile = outputFile.Substring(dirName.Length); outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\"); Debug.WriteLine(outputFile); int GameId = Convert.ToInt32(reader["GameId"]); string GameName = reader["name"].ToString(); Debug.WriteLine("Game " + GameId + " Name: " + GameName); ZipFile memZip = new ZipFile(); memZip.ZipCreateFake(); ulong fileOffset = 0; Stream _zipFs = null; int romCount = 0; using (DbDataReader drRom = ZipSetGetRomsInGame(GameId)) { while (drRom.Read()) { int RomId = Convert.ToInt32(drRom["RomId"]); string RomName = drRom["name"].ToString(); ulong size = Convert.ToUInt64(drRom["size"]); ulong compressedSize = Convert.ToUInt64(drRom["compressedsize"]); byte[] CRC = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8); byte[] sha1 = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32); Debug.WriteLine(" Rom " + RomId + " Name: " + RomName + " Size: " + size + " Compressed: " + compressedSize + " CRC: " + VarFix.ToString(CRC)); byte[] localHeader; memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader); //ZipSetLocalFileHeader(RomId, localHeader, fileOffset); if (romCount == 0) { ZipFile.CreateDirForFile(outputFile); int errorCode = FileStream.OpenFileWrite(outputFile, out _zipFs); } _zipFs.Write(localHeader, 0, localHeader.Length); GZip GZip = new GZip(); string strFilename = RomRootDir.GetFilename(sha1, true); GZip.ReadGZip(strFilename, false); Stream oStr; GZip.GetRawStream(out oStr); ulong sizetogo = compressedSize; while (sizetogo > 0) { ulong sizenow = sizetogo > 1024 ? 1024 : sizetogo; oStr.Read(buff, 0, (int)sizenow); _zipFs.Write(buff, 0, (int)sizenow); sizetogo -= sizenow; } oStr.Dispose(); GZip.Close(); fileOffset += (ulong)localHeader.Length + compressedSize; _zipFs.Position = (long)fileOffset; romCount += 1; } } byte[] centeralDir; memZip.ZipFileCloseFake(fileOffset, out centeralDir); if (romCount > 0) { _zipFs.Write(centeralDir, 0, centeralDir.Length); _zipFs.Flush(); _zipFs.Close(); _zipFs.Dispose(); } } }
public static int CheckSumRead(string filename, bool testDeep, out byte[] crc, out byte[] bMD5, out byte[] bSHA1) { bMD5 = null; bSHA1 = null; crc = null; Stream ds = null; ThreadLoadBuffer lbuffer = null; ThreadCRC tcrc32 = null; ThreadMD5 tmd5 = null; ThreadSHA1 tsha1 = null; try { int errorCode = FileStream.OpenFileRead(filename, out ds); if (errorCode != 0) { return(errorCode); } lbuffer = new ThreadLoadBuffer(ds); tcrc32 = new ThreadCRC(); if (testDeep) { tmd5 = new ThreadMD5(); tsha1 = new ThreadSHA1(); } long sizetogo = ds.Length; // Pre load the first buffer0 int sizeNext = sizetogo > Buffersize ? Buffersize : (int)sizetogo; ds.Read(Buffer0, 0, sizeNext); int sizebuffer = sizeNext; sizetogo -= sizeNext; bool whichBuffer = true; while ((sizebuffer > 0) && !lbuffer.errorState) { sizeNext = sizetogo > Buffersize ? Buffersize : (int)sizetogo; if (sizeNext > 0) { lbuffer.Trigger(whichBuffer ? Buffer1 : Buffer0, sizeNext); } byte[] buffer = whichBuffer ? Buffer0 : Buffer1; tcrc32.Trigger(buffer, sizebuffer); tmd5?.Trigger(buffer, sizebuffer); tsha1?.Trigger(buffer, sizebuffer); if (sizeNext > 0) { lbuffer.Wait(); } tcrc32.Wait(); tmd5?.Wait(); tsha1?.Wait(); sizebuffer = sizeNext; sizetogo -= sizeNext; whichBuffer = !whichBuffer; } lbuffer.Finish(); tcrc32.Finish(); tmd5?.Finish(); tsha1?.Finish(); ds.Close(); } catch { ds?.Close(); lbuffer?.Dispose(); tcrc32?.Dispose(); tmd5?.Dispose(); tsha1?.Dispose(); return(0x17); // need to remember what this number is for } if (lbuffer.errorState) { ds?.Close(); lbuffer?.Dispose(); tcrc32?.Dispose(); tmd5?.Dispose(); tsha1?.Dispose(); return(0x17); // need to remember what this number is for } crc = tcrc32.Hash; if (testDeep) { bMD5 = tmd5.Hash; bSHA1 = tsha1.Hash; } lbuffer.Dispose(); tcrc32.Dispose(); tmd5?.Dispose(); tsha1?.Dispose(); return(0); }