using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using SabreTools.Helper.Data; using SabreTools.Helper.Dats; using Ionic.Zlib; using ROMVault2.SupportedFiles.Zip; using SharpCompress.Archives; using SharpCompress.Archives.SevenZip; using SharpCompress.Archives.Tar; using SharpCompress.Common; using SharpCompress.Readers; using SharpCompress.Writers; namespace SabreTools.Helper.Tools { public static class ArchiveTools { private const int _bufferSize = 4096 * 128; #region Archive-to-Archive Handling /// /// Attempt to copy a file between archives /// /// Source archive name /// Destination archive name /// Input entry name /// Output entry name /// Logger object for file and console output /// True if the copy was a success, false otherwise public static bool CopyFileBetweenArchives(string inputArchive, string outDir, string sourceEntryName, Rom destEntry, Logger logger) { string temp = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); string realName = ExtractItem(inputArchive, sourceEntryName, temp, logger); bool success = WriteTorrentZip(realName, outDir, destEntry, logger); Directory.Delete(temp, true); return success; } #endregion #region Extraction /// /// Attempt to extract a file as an archive /// /// Name of the file to be extracted /// Temporary directory for archive extraction /// ArchiveScanLevel representing the archive handling levels /// Logger object for file and console output /// True if the extraction was a success, false otherwise public static bool ExtractArchive(string input, string tempDir, ArchiveScanLevel archiveScanLevel, Logger logger) { bool encounteredErrors = true; // First get the archive type ArchiveType? at = GetCurrentArchiveType(input, logger); // If we got back null, then it's not an archive, so we we return if (at == null) { return encounteredErrors; } try { if (at == ArchiveType.SevenZip && (archiveScanLevel & ArchiveScanLevel.SevenZipInternal) != 0) { logger.Verbose("Found archive of type: " + at); // Create the temp directory Directory.CreateDirectory(tempDir); // Extract all files to the temp directory SevenZipArchive sza = SevenZipArchive.Open(File.OpenRead(input)); foreach (IArchiveEntry iae in sza.Entries) { iae.WriteToDirectory(tempDir, new ExtractionOptions{ PreserveFileTime = true, ExtractFullPath = true, Overwrite = true }); } encounteredErrors = false; sza.Dispose(); } else if (at == ArchiveType.GZip && (archiveScanLevel & ArchiveScanLevel.GZipInternal) != 0) { logger.Verbose("Found archive of type: " + at); // Create the temp directory Directory.CreateDirectory(tempDir); // Decompress the input stream FileStream outstream = File.Create(Path.Combine(tempDir, Path.GetFileNameWithoutExtension(input))); GZipStream gzstream = new GZipStream(File.OpenRead(input), CompressionMode.Decompress); gzstream.CopyTo(outstream); // Dispose of the streams outstream.Dispose(); gzstream.Dispose(); encounteredErrors = false; } else if (at == ArchiveType.Zip && (archiveScanLevel & ArchiveScanLevel.ZipInternal) != 0) { logger.Verbose("Found archive of type: " + at); // Create the temp directory Directory.CreateDirectory(tempDir); // Extract all files to the temp directory ZipFile zf = new ZipFile(); ZipReturn zr = zf.Open(input, new FileInfo(input).LastWriteTime.Ticks, true); if (zr != ZipReturn.ZipGood) { throw new Exception(ZipFile.ZipErrorMessageText(zr)); } for (int i = 0; i < zf.EntriesCount && zr == ZipReturn.ZipGood; i++) { // Set defaults before writing out Stream readStream; ulong streamsize = 0; CompressionMethod cm = CompressionMethod.Stored; uint lastMod = 0; zr = zf.OpenReadStream(i, false, out readStream, out streamsize, out cm, out lastMod); // Create the rest of the path, if needed if (!String.IsNullOrEmpty(Path.GetDirectoryName(zf.Entries[i].FileName))) { Directory.CreateDirectory(Path.Combine(tempDir, Path.GetDirectoryName(zf.Entries[i].FileName))); } // If the entry ends with a directory separator, continue to the next item, if any if (zf.Entries[i].FileName.EndsWith(Path.DirectorySeparatorChar.ToString()) || zf.Entries[i].FileName.EndsWith(Path.AltDirectorySeparatorChar.ToString()) || zf.Entries[i].FileName.EndsWith(Path.PathSeparator.ToString())) { continue; } FileStream writeStream = File.OpenWrite(Path.Combine(tempDir, zf.Entries[i].FileName)); byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = readStream.Read(ibuffer, 0, _bufferSize)) > 0) { writeStream.Write(ibuffer, 0, ilen); writeStream.Flush(); } zr = zf.CloseReadStream(); writeStream.Dispose(); } encounteredErrors = false; } else if (at == ArchiveType.Rar && (archiveScanLevel & ArchiveScanLevel.RarInternal) != 0) { logger.Verbose("Found archive of type: " + at); // Create the temp directory Directory.CreateDirectory(tempDir); // Extract all files to the temp directory IReader reader = ReaderFactory.Open(File.OpenRead(input)); bool succeeded = reader.MoveToNextEntry(); while (succeeded) { reader.WriteEntryToDirectory(tempDir, new ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true }); succeeded = reader.MoveToNextEntry(); } encounteredErrors = false; reader.Dispose(); } } catch (EndOfStreamException) { // Catch this but don't count it as an error because SharpCompress is unsafe } catch (InvalidOperationException) { encounteredErrors = true; } catch (Exception) { // Don't log file open errors encounteredErrors = true; } return encounteredErrors; } /// /// Attempt to extract a file from an archive /// /// Name of the archive to be extracted /// Name of the entry to be extracted /// Temporary directory for archive extraction /// Logger object for file and console output /// Name of the extracted file, null on error public static string ExtractItem(string input, string entryName, string tempDir, Logger logger) { string realEntry = ""; Stream ms = ExtractStream(input, entryName, out realEntry, logger); realEntry = Path.Combine(Path.GetFullPath(tempDir), realEntry); if (!Directory.Exists(Path.GetDirectoryName(realEntry))) { Directory.CreateDirectory(Path.GetDirectoryName(realEntry)); } FileStream fs = File.Open(realEntry, FileMode.Create, FileAccess.Write); byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = ms.Read(ibuffer, 0, _bufferSize)) > 0) { fs.Write(ibuffer, 0, ilen); fs.Flush(); } // Dispose of the streams ms.Dispose(); fs.Dispose(); return realEntry; } /// /// Attempt to extract a stream from an archive /// /// Name of the archive to be extracted /// Name of the entry to be extracted /// Logger object for file and console output /// Name of the extracted file, null on error public static Stream ExtractStream(string input, string entryName, out string realEntry, Logger logger) { // Set the real entry name realEntry = ""; // Get a writable stream to return Stream st = new MemoryStream(); // First get the archive type ArchiveType? at = GetCurrentArchiveType(input, logger); // If we got back null, then it's not an archive, so we we return if (at == null) { return st; } IReader reader = null; try { if (at == ArchiveType.Zip) { ZipFile zf = new ZipFile(); ZipReturn zr = zf.Open(input, new FileInfo(input).LastWriteTime.Ticks, true); if (zr != ZipReturn.ZipGood) { throw new Exception(ZipFile.ZipErrorMessageText(zr)); } for (int i = 0; i < zf.EntriesCount && zr == ZipReturn.ZipGood; i++) { logger.Verbose("Current entry name: '" + zf.Entries[i].FileName + "'"); if (zf.Entries[i].FileName.Contains(entryName)) { realEntry = zf.Entries[i].FileName; // Set defaults before writing out Stream readStream; ulong streamsize = 0; CompressionMethod cm = CompressionMethod.Stored; uint lastMod = 0; zr = zf.OpenReadStream(i, false, out readStream, out streamsize, out cm, out lastMod); byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = readStream.Read(ibuffer, 0, _bufferSize)) > 0) { st.Write(ibuffer, 0, ilen); st.Flush(); } zr = zf.CloseReadStream(); } } } else if (at == ArchiveType.SevenZip || at == ArchiveType.Rar) { reader = ReaderFactory.Open(File.OpenRead(input)); while (reader.MoveToNextEntry()) { logger.Verbose("Current entry name: '" + reader.Entry.Key + "'"); if (reader.Entry != null && reader.Entry.Key.Contains(entryName)) { realEntry = reader.Entry.Key; reader.WriteEntryTo(st); } } } else if (at == ArchiveType.GZip) { // Decompress the input stream realEntry = Path.GetFileNameWithoutExtension(input); GZipStream gzstream = new GZipStream(File.OpenRead(input), CompressionMode.Decompress); gzstream.CopyTo(st); // Dispose of the stream gzstream.Dispose(); } } catch (Exception ex) { logger.Error(ex.ToString()); st = null; } finally { reader?.Dispose(); } st.Position = 0; return st; } #endregion #region Information /// /// Generate a list of RomData objects from the header values in an archive /// /// Input file to get data from /// Logger object for file and console output /// List of RomData objects representing the found data public static List GetArchiveFileInfo(string input, Logger logger) { List roms = new List(); string gamename = Path.GetFileNameWithoutExtension(input); // First get the archive type ArchiveType? at = GetCurrentArchiveType(input, logger); // If we got back null, then it's not an archive, so we we return if (at == null) { return roms; } // If we got back GZip, try to get TGZ info first else if (at == ArchiveType.GZip) { Rom possibleTgz = GetTorrentGZFileInfo(input, logger); // If it was, then add it to the outputs and continue if (possibleTgz.Name != null) { roms.Add(possibleTgz); return roms; } } IReader reader = null; try { logger.Verbose("Found archive of type: " + at); long size = 0; string crc = ""; // If we have a gzip file, get the crc directly if (at == ArchiveType.GZip) { // Get the CRC and size from the file BinaryReader br = new BinaryReader(File.OpenRead(input)); br.BaseStream.Seek(-8, SeekOrigin.End); byte[] headercrc = br.ReadBytes(4); crc = BitConverter.ToString(headercrc.Reverse().ToArray()).Replace("-", string.Empty).ToLowerInvariant(); byte[] headersize = br.ReadBytes(4); size = BitConverter.ToInt32(headersize.Reverse().ToArray(), 0); br.Dispose(); } else if (at == ArchiveType.Zip) { ZipFile zf = new ZipFile(); ZipReturn zr = zf.Open(input, new FileInfo(input).LastWriteTime.Ticks, true); if (zr != ZipReturn.ZipGood) { throw new Exception(ZipFile.ZipErrorMessageText(zr)); } for (int i = 0; i < zf.EntriesCount && zr == ZipReturn.ZipGood; i++) { string newname = zf.Entries[i].FileName; long newsize = (size == 0 ? (long)zf.Entries[i].UncompressedSize : size); string newcrc = BitConverter.ToString(zf.Entries[i].CRC.Reverse().ToArray(), 0, zf.Entries[i].CRC.Length).Replace("-", string.Empty).ToLowerInvariant(); logger.Verbose("Entry found: '" + newname + "': " + newsize + ", " + newcrc); roms.Add(new Rom { Type = ItemType.Rom, Name = newname, Size = newsize, CRC = newcrc, Machine = new Machine { Name = gamename, }, }); } } else if (at != ArchiveType.Tar) { reader = ReaderFactory.Open(File.OpenRead(input)); while (reader.MoveToNextEntry()) { if (reader.Entry != null && !reader.Entry.IsDirectory) { logger.Verbose("Entry found: '" + reader.Entry.Key + "': " + (size == 0 ? reader.Entry.Size : size) + ", " + (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc)); roms.Add(new Rom { Type = ItemType.Rom, Name = reader.Entry.Key, Size = (size == 0 ? reader.Entry.Size : size), CRC = (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc), Machine = new Machine { Name = gamename, }, }); } } } } catch (Exception ex) { logger.Error(ex.ToString()); } finally { reader?.Dispose(); } return roms; } /// /// Retrieve file information for a single torrent GZ file /// /// Filename to get information from /// Logger object for file and console output /// Populated RomData object if success, empty one on error public static Rom GetTorrentGZFileInfo(string input, Logger logger) { string datum = Path.GetFileName(input).ToLowerInvariant(); long filesize = new FileInfo(input).Length; // Check if the name is the right length if (!Regex.IsMatch(datum, @"^[0-9a-f]{40}\.gz")) { logger.Warning("Non SHA-1 filename found, skipping: '" + datum + "'"); return null; } // Check if the file is at least the minimum length if (filesize < 40 /* bytes */) { logger.Warning("Possibly corrupt file '" + input + "' with size " + Style.GetBytesReadable(filesize)); return null; } // Get the Romba-specific header data byte[] header; // Get preamble header for checking byte[] headermd5; // MD5 byte[] headercrc; // CRC ulong headersz; // Int64 size BinaryReader br = new BinaryReader(File.OpenRead(input)); header = br.ReadBytes(12); headermd5 = br.ReadBytes(16); headercrc = br.ReadBytes(4); headersz = br.ReadUInt64(); br.Dispose(); // If the header is not correct, return a blank rom bool correct = true; for (int i = 0; i < header.Length; i++) { // This is a temp fix to ignore the modification time and OS until romba can be fixed if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9) { continue; } correct &= (header[i] == Constants.TorrentGZHeader[i]); } if (!correct) { return null; } // Now convert the data and get the right position string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty); string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty); long extractedsize = (long)headersz; Rom rom = new Rom { Type = ItemType.Rom, Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), Size = extractedsize, CRC = gzcrc.ToLowerInvariant(), MD5 = gzmd5.ToLowerInvariant(), SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), Machine = new Machine { Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), }, }; return rom; } /// /// Returns the archive type of an input file /// /// Input file to check /// Logger object for file and console output /// ArchiveType of inputted file (null on error) public static ArchiveType? GetCurrentArchiveType(string input, Logger logger) { ArchiveType? outtype = null; // First line of defense is going to be the extension, for better or worse string ext = Path.GetExtension(input).ToLowerInvariant(); if (ext.StartsWith(".")) { ext = ext.Substring(1); } if (ext != "7z" && ext != "gz" && ext != "lzma" && ext != "rar" && ext != "rev" && ext != "r00" && ext != "r01" && ext != "tar" && ext != "tgz" && ext != "tlz" && ext != "zip" && ext != "zipx") { return outtype; } // Read the first bytes of the file and get the magic number try { byte[] magic = new byte[8]; BinaryReader br = new BinaryReader(File.OpenRead(input)); magic = br.ReadBytes(8); br.Dispose(); // Convert it to an uppercase string string mstr = string.Empty; for (int i = 0; i < magic.Length; i++) { mstr += BitConverter.ToString(new byte[] { magic[i] }); } mstr = mstr.ToUpperInvariant(); // Now try to match it to a known signature if (mstr.StartsWith(Constants.SevenZipSig)) { outtype = ArchiveType.SevenZip; } else if (mstr.StartsWith(Constants.GzSig)) { outtype = ArchiveType.GZip; } else if (mstr.StartsWith(Constants.RarSig) || mstr.StartsWith(Constants.RarFiveSig)) { outtype = ArchiveType.Rar; } else if (mstr.StartsWith(Constants.TarSig) || mstr.StartsWith(Constants.TarZeroSig)) { outtype = ArchiveType.Tar; } else if (mstr.StartsWith(Constants.ZipSig) || mstr.StartsWith(Constants.ZipSigEmpty) || mstr.StartsWith(Constants.ZipSigSpanned)) { outtype = ArchiveType.Zip; } } catch (Exception) { // Don't log file open errors } return outtype; } /// /// Get if the current file should be scanned internally and externally /// /// Name of the input file to check /// ArchiveScanLevel representing the archive handling levels /// Logger object for file and console output /// Output parameter determining if file should be processed externally /// Output parameter determining if file should be processed internally public static void GetInternalExternalProcess(string input, ArchiveScanLevel archiveScanLevel, Logger logger, out bool shouldExternalProcess, out bool shouldInternalProcess) { shouldExternalProcess = true; shouldInternalProcess = true; ArchiveType? archiveType = GetCurrentArchiveType(input, logger); switch (archiveType) { case null: shouldExternalProcess = true; shouldInternalProcess = false; break; case ArchiveType.GZip: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.GZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.GZipInternal) != 0); break; case ArchiveType.Rar: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.RarExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.RarInternal) != 0); break; case ArchiveType.SevenZip: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.SevenZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.SevenZipInternal) != 0); break; case ArchiveType.Zip: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.ZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.ZipInternal) != 0); break; } } /// /// Get the archive scan level based on the inputs /// /// User-defined scan level for 7z archives /// User-defined scan level for GZ archives /// User-defined scan level for RAR archives /// User-defined scan level for Zip archives /// ArchiveScanLevel representing the levels public static ArchiveScanLevel GetArchiveScanLevelFromNumbers(int sevenzip, int gzip, int rar, int zip) { ArchiveScanLevel archiveScanLevel = 0x0000; // 7z sevenzip = (sevenzip < 0 || sevenzip > 2 ? 0 : sevenzip); switch (sevenzip) { case 0: archiveScanLevel |= ArchiveScanLevel.SevenZipBoth; break; case 1: archiveScanLevel |= ArchiveScanLevel.SevenZipInternal; break; case 2: archiveScanLevel |= ArchiveScanLevel.SevenZipExternal; break; } // GZip gzip = (gzip < 0 || gzip > 2 ? 0 : gzip); switch (gzip) { case 0: archiveScanLevel |= ArchiveScanLevel.GZipBoth; break; case 1: archiveScanLevel |= ArchiveScanLevel.GZipInternal; break; case 2: archiveScanLevel |= ArchiveScanLevel.GZipExternal; break; } // RAR rar = (rar < 0 || rar > 2 ? 0 : rar); switch (rar) { case 0: archiveScanLevel |= ArchiveScanLevel.RarBoth; break; case 1: archiveScanLevel |= ArchiveScanLevel.RarInternal; break; case 2: archiveScanLevel |= ArchiveScanLevel.RarExternal; break; } // Zip zip = (zip < 0 || zip > 2 ? 0 : zip); switch (zip) { case 0: archiveScanLevel |= ArchiveScanLevel.ZipBoth; break; case 1: archiveScanLevel |= ArchiveScanLevel.ZipInternal; break; case 2: archiveScanLevel |= ArchiveScanLevel.ZipExternal; break; } return archiveScanLevel; } /// /// (UNIMPLEMENTED) Read the information from an input 7z file /// /// Name of the input file to check /// Logger object for file and console output /// /// http://cpansearch.perl.org/src/BJOERN/Compress-Deflate7-1.0/7zip/DOC/7zFormat.txt public static void GetSevenZipFileInfo(string input, Logger logger) { BinaryReader br = new BinaryReader(File.OpenRead(input)); br.ReadBytes(6); // BYTE kSignature[6] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; logger.User("ArchiveVersion (Major.Minor): " + br.ReadByte() + "." + br.ReadByte()); logger.User("StartHeaderCRC: " + br.ReadUInt32()); logger.User("StartHeader (NextHeaderOffset, NextHeaderSize, NextHeaderCRC)" + br.ReadUInt64() + ", " + br.ReadUInt64() + ", " + br.ReadUInt32()); } #endregion #region Writing /// /// Write an input file to a tape archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTAR(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTAR(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a tape archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTAR(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { bool success = false; // If the number of inputs is less than the number of available roms, return if (inputFiles.Count < roms.Count) { return success; } // If one of the files doesn't exist, return foreach (string file in inputFiles) { if (!File.Exists(file)) { return success; } } // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + (roms[0].Machine.Name.EndsWith(".tar") ? "" : ".tar")); // Set internal variables Stream tarstream = new MemoryStream(); TarArchive tarchive = TarArchive.Create(); try { // If the full output path doesn't exist, create it if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); } // We can only overwrite at this point, sorry tarstream = File.Open(archiveFileName, FileMode.Create, FileAccess.Write); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); for (int i = 0; i < inputFiles.Count; i++) { inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i); } // Sort the keys in TZIP order List keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TorrentZipStringCompare); // Now add each of the files foreach (string key in keys) { int index = inputIndexMap[key]; tarchive.AddEntry(key, inputFiles[index]); } tarchive.SaveTo(tarstream, new WriterOptions(CompressionType.None)); success = true; } catch (Exception ex) { success = false; logger.Error(ex.ToString()); } finally { tarstream?.Dispose(); tarchive?.Dispose(); } return success; } /// /// Write an input file to a torrent7z archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrent7Zip(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTorrent7Zip(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a torrent7z archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrent7Zip(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { return false; } /// /// Write an input file to a torrent GZ file /// /// File to write from /// Directory to write archive to /// True if files should be output in Romba depot folders, false otherwise /// Logger object for file and console output /// True if the write was a success, false otherwise /// This works for now, but it can be sped up by using Ionic.Zip or another zlib wrapper that allows for header values built-in. See edc's code. public static bool WriteTorrentGZ(string input, string outDir, bool romba, Logger logger) { // Check that the input file exists if (!File.Exists(input)) { logger.Warning("File " + input + " does not exist!"); return false; } input = Path.GetFullPath(input); // Make sure the output directory exists if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } outDir = Path.GetFullPath(outDir); // Now get the Rom info for the file so we have hashes and size Rom rom = FileTools.GetFileInfo(input, logger); // Get the output file name string outfile = Path.Combine(outDir, rom.SHA1 + ".gz"); // If the output file exists, don't try to write again if (!File.Exists(outfile)) { // Compress the input stream FileStream inputStream = File.OpenRead(input); FileStream outputStream = File.Open(outfile, FileMode.Create, FileAccess.Write); // Open the output file for writing BinaryWriter sw = new BinaryWriter(outputStream); // Write standard header and TGZ info byte[] data = Constants.TorrentGZHeader .Concat(Style.StringToByteArray(rom.MD5)) // MD5 .Concat(Style.StringToByteArray(rom.CRC)) // CRC .ToArray(); sw.Write(data); sw.Write((ulong)rom.Size); // Long size (Unsigned, Mirrored) // Now create a deflatestream from the input file DeflateStream ds = new DeflateStream(outputStream, CompressionMode.Compress, CompressionLevel.BestCompression, true); // Copy the input stream to the output byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = inputStream.Read(ibuffer, 0, _bufferSize)) > 0) { ds.Write(ibuffer, 0, ilen); ds.Flush(); } ds.Dispose(); // Now write the standard footer sw.Write(Style.StringToByteArray(rom.CRC).Reverse().ToArray()); sw.Write((uint)rom.Size); // Dispose of everything sw.Dispose(); outputStream.Dispose(); inputStream.Dispose(); } // If we're in romba mode, create the subfolder and move the file if (romba) { MoveToRombaFolder(rom, outDir, outfile, logger); } return true; } /// /// Write an input file to a torrentlrzip archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentLRZ(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTorrentLRZ(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a torrentlrzip archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentLRZ(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { return false; } /// /// Write an input file to a torrentrar archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentRAR(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTorrentRAR(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a torrentrar archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentRAR(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { return false; } /// /// Write an input file to a torrentxz archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentXZ(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTorrentXZ(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a torrentxz archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentXZ(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { return false; } /// /// Write an input file to a torrentzip archive /// /// Input filename to be moved /// Output directory to build to /// RomData representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentZip(string inputFile, string outDir, Rom rom, Logger logger, bool date = false) { // Wrap the individual inputs into lists List inputFiles = new List(); inputFiles.Add(inputFile); List roms = new List(); roms.Add(rom); return WriteTorrentZip(inputFiles, outDir, roms, logger, date: date); } /// /// Write a set of input files to a torrentzip archive (assuming the same output archive name) /// /// Input filenames to be moved /// Output directory to build to /// List of Rom representing the new information /// Logger object for file and console output /// True if the date from the DAT should be used if available, false otherwise (default) /// True if the archive was written properly, false otherwise public static bool WriteTorrentZip(List inputFiles, string outDir, List roms, Logger logger, bool date = false) { bool success = false; string tempFile = Path.GetTempFileName(); // If the number of inputs is less than the number of available roms, return if (inputFiles.Count < roms.Count) { return success; } // If one of the files doesn't exist, return foreach (string file in inputFiles) { if (!File.Exists(file)) { return success; } } // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + (roms[0].Machine.Name.EndsWith(".zip") ? "" : ".zip")); // Set internal variables Stream writeStream = null; ZipFile oldZipFile = new ZipFile(); ZipFile zipFile = new ZipFile(); ZipReturn zipReturn = ZipReturn.ZipGood; try { // If the full output path doesn't exist, create it if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); } // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { zipReturn = zipFile.Create(tempFile); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); for (int i = 0; i < inputFiles.Count; i++) { inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i); } // Sort the keys in TZIP order List keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TorrentZipStringCompare); // Now add all of the files in order foreach (string key in keys) { // Get the index mapped to the key int index = inputIndexMap[key]; // Open the input file for reading Stream freadStream = File.Open(inputFiles[index], FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ulong istreamSize = (ulong)(new FileInfo(inputFiles[index]).Length); DateTime dt = DateTime.Now; if (date && !String.IsNullOrEmpty(roms[index].Date) && DateTime.TryParse(roms[index].Date.Replace('\\', '/'), out dt)) { uint msDosDateTime = Style.ConvertDateTimeToMsDosTimeFormat(dt); zipFile.OpenWriteStream(false, false, roms[index].Name.Replace('\\', '/'), istreamSize, CompressionMethod.Deflated, out writeStream, lastMod: msDosDateTime); } else { zipFile.OpenWriteStream(false, true, roms[index].Name.Replace('\\', '/'), istreamSize, CompressionMethod.Deflated, out writeStream); } // Copy the input stream to the output byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = freadStream.Read(ibuffer, 0, _bufferSize)) > 0) { writeStream.Write(ibuffer, 0, ilen); writeStream.Flush(); } freadStream.Dispose(); zipFile.CloseWriteStream(Convert.ToUInt32(roms[index].CRC, 16)); } } // Otherwise, sort the input files and write out in the correct order else { // Open the old archive for reading oldZipFile.Open(archiveFileName, new FileInfo(archiveFileName).LastWriteTime.Ticks, true); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); for (int i = 0; i < inputFiles.Count; i++) { // If the old one contains the new file, then just skip out if (oldZipFile.Contains(roms[i].Name.Replace('\\', '/'))) { continue; } inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1)); } // Then add all of the old entries to it too for (int i = 0; i < oldZipFile.EntriesCount; i++) { inputIndexMap.Add(oldZipFile.Filename(i), i); } // If the number of entries is the same as the old archive, skip out if (inputIndexMap.Keys.Count <= oldZipFile.EntriesCount) { success = true; return success; } // Otherwise, process the old zipfile zipFile.Create(tempFile); // Get the order for the entries with the new file List keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TorrentZipStringCompare); // Copy over all files to the new archive foreach (string key in keys) { // Get the index mapped to the key int index = inputIndexMap[key]; // If we have the input file, add it now if (index < 0) { // Open the input file for reading Stream freadStream = File.Open(inputFiles[-index - 1], FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ulong istreamSize = (ulong)(new FileInfo(inputFiles[-index - 1]).Length); DateTime dt = DateTime.Now; if (date && !String.IsNullOrEmpty(roms[-index - 1].Date) && DateTime.TryParse(roms[-index - 1].Date.Replace('\\', '/'), out dt)) { uint msDosDateTime = Style.ConvertDateTimeToMsDosTimeFormat(dt); zipFile.OpenWriteStream(false, false, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, CompressionMethod.Deflated, out writeStream, lastMod: msDosDateTime); } else { zipFile.OpenWriteStream(false, true, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, CompressionMethod.Deflated, out writeStream); } // Copy the input stream to the output byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = freadStream.Read(ibuffer, 0, _bufferSize)) > 0) { writeStream.Write(ibuffer, 0, ilen); writeStream.Flush(); } freadStream.Dispose(); zipFile.CloseWriteStream(Convert.ToUInt32(roms[-index - 1].CRC, 16)); } // Otherwise, copy the file from the old archive else { // Instantiate the streams CompressionMethod icompressionMethod = CompressionMethod.Stored; uint lastMod = 0; ulong istreamSize = 0; Stream zreadStream; oldZipFile.OpenReadStream(index, false, out zreadStream, out istreamSize, out icompressionMethod, out lastMod); zipFile.OpenWriteStream(false, lastMod == Constants.TorrentZipFileDateTime, oldZipFile.Filename(index), istreamSize, CompressionMethod.Deflated, out writeStream, lastMod: lastMod); // Copy the input stream to the output byte[] ibuffer = new byte[_bufferSize]; int ilen; while ((ilen = zreadStream.Read(ibuffer, 0, _bufferSize)) > 0) { writeStream.Write(ibuffer, 0, ilen); writeStream.Flush(); } zipFile.CloseWriteStream(BitConverter.ToUInt32(oldZipFile.CRC32(index), 0)); } } } // Close the output zip file zipFile.Close(); success = true; } catch (Exception ex) { Console.WriteLine(ex); success = false; } finally { zipFile.Dispose(); oldZipFile.Dispose(); } // If the old file exists, delete it and replace if (File.Exists(archiveFileName)) { File.Delete(archiveFileName); } File.Move(tempFile, archiveFileName); return true; } /// /// Get the romba path for a file based on the rom's SHA-1 /// /// Rom to get the sha1 from /// Base output folder /// Formatted path string to use public static string GetRombaPath(Rom rom, string baseOutDir) { string subfolder = Path.Combine(rom.SHA1.Substring(0, 2), rom.SHA1.Substring(2, 2), rom.SHA1.Substring(4, 2), rom.SHA1.Substring(6, 2)); return Path.Combine(baseOutDir, subfolder); } /// /// Move a file to a named, Romba-style subdirectory /// /// Rom to get the sha1 from /// Base output folder /// Name of the file to be moved /// Logger object for file and console output public static void MoveToRombaFolder(Rom rom, string baseOutDir, string filename, Logger logger) { string outDir = GetRombaPath(rom, baseOutDir); if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } try { File.Move(filename, Path.Combine(outDir, Path.GetFileName(filename))); } catch (Exception ex) { logger.Warning(ex.ToString()); File.Delete(filename); } } #endregion } }