private static byte[] GetData(string file, bool wasCompressed, bool useCompression) { if (useCompression && wasCompressed) { var uncompressedData = File.ReadAllBytes(file); var data = FalcomCompressor.Compress(uncompressedData); return(data); } else { return(File.ReadAllBytes(file)); } }
public static void Extract(string inputPath, string outputFolder) { var inputFolder = Path.GetDirectoryName(inputPath); var fileName = Path.GetFileNameWithoutExtension(inputPath); var dirFile = Path.Combine(inputFolder, $"{fileName}.dir"); if (!File.Exists(dirFile)) { throw new FileNotFoundException($"No se ha encontrado el archivo {dirFile}"); } Directory.CreateDirectory(outputFolder); var logFile = Path.Combine(outputFolder, "Extract_Data.tf"); using (var dirFs = new FileStream(dirFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var dirInput = new ExtendedBinaryReader(dirFs, Encoding.GetEncoding(1252))) using (var datFs = new FileStream(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var datInput = new ExtendedBinaryReader(datFs, Encoding.UTF8)) using (var log = new ExtendedBinaryWriter(new FileStream(logFile, FileMode.Create), Encoding.GetEncoding(1252))) { var dirHeader = dirInput.ReadBytes(8); var datHeader = datInput.ReadBytes(8); if (!dirHeader.SequenceEqual(DIR_HEADER) || !datHeader.SequenceEqual(DAT_HEADER)) { throw new InvalidDataException("El fichero no es válido"); } var maxNumFiles = dirInput.ReadInt64(); log.Write(maxNumFiles); var currentPos = dirInput.Position; var endPos = dirInput.Length; while (currentPos < endPos) { var name = dirInput.ReadString(); log.WriteString(name); dirInput.Skip(3); var compressedSize = dirInput.ReadInt32(); var maxSize = dirInput.ReadInt32(); var unknown2 = dirInput.ReadInt32(); var timestamp = dirInput.ReadInt32(); log.Write(compressedSize); log.Write(maxSize); log.Write(unknown2); log.Write(timestamp); var datOffset = dirInput.ReadInt32(); log.Write(datOffset); if (datOffset != 0 && compressedSize != 0) { var outputFile = Path.Combine(outputFolder, name); datInput.Seek(datOffset, SeekOrigin.Begin); var data = datInput.ReadBytes(compressedSize); try { var data2 = FalcomCompressor.Decompress(data); File.WriteAllBytes(outputFile, data2); log.Write(1); // Para saber si estaba comprimido o no } catch (Exception e) { File.WriteAllBytes(outputFile, data); log.Write(0); } } else { log.Write(0); } currentPos = dirInput.Position; } } }