public void TestSetPositionWrite() { //Test setting Position durung a write by writing the contents of the file to disk backwards one byte at a time string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionWrite"); byte[] buffer = new byte[1]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(filePath); Assert.AreEqual(0, wfio.Position); for (int i = FILE_CONTENT.Length - 1; i >= 0; i--) { wfio.Position = i; Assert.AreEqual(i, wfio.Position); buffer[0] = FILE_CONTENT[i]; wfio.WriteBlocks(1); } wfio.Close(); byte[] actual = readFile(filePath); CollectionAssert.AreEqual(FILE_CONTENT, actual); }
private void TestWriteFileWinAPI2() { // This function tests out the Windows API function WriteFile. This function tests the WriteFile function // by writing out the file in blocks. String S; int FileLoop; try { // Get how fast it takes to write ou the .683MB, 10MB, and 50MB files into memory. for (FileLoop = 0; FileLoop < 3; FileLoop++) { Stopwatch stopwatch = Stopwatch.StartNew(); WFIO.OpenForWriting(FileNames[FileLoop]); WFIO.WriteBlocks(BytesInFiles[FileLoop]); WFIO.Close(); stopwatch.Stop(); S = " Total time writing " + FileDesc[FileLoop] + " with WFIO2.WriteBlocks API No Parsing = " + stopwatch.Elapsed.TotalMilliseconds.ToString(); DispUIMsg(S); } } catch (System.Exception ex) { S = "TestWriteFileWinAPI2: threw an exception of type " + ex.GetType().ToString(); DispUIMsg(S); DispUIMsg(ex.Message); } }
public void WritePointArray(byte[] points_buffer) { if (File.Exists(_FullName)) { Close(); File.Delete(_FullName); } _WFIO = new WinFileIO(); _WFIO.OpenForWriting(_FullName); byte[] header_bytes = _Header.ToByteArray(); _WFIO.PinBuffer(header_bytes); _WFIO.WriteBlocks(header_bytes.Length); _WFIO.PinBuffer(points_buffer); _WFIO.WriteBlocks(points_buffer.Length); _WFIO.Close(); }
public void TestLengthWriteIncrements() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestLengthWriteIncrements"); byte[] buffer = new byte[FILE_CONTENT.Length]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(filePath); Array.Copy(FILE_CONTENT, buffer, buffer.Length); wfio.WriteBlocks(buffer.Length); Assert.AreEqual((long)FILE_CONTENT.Length, wfio.Length); wfio.WriteBlocks(1); Assert.AreEqual((long)FILE_CONTENT.Length + 1, wfio.Length); wfio.Close(); fileNames.Add(filePath); }
private void TestWriteBlocks() { // This function tests the WriteBlocks function. int BytesWritten, BytesRead, Loop; String S; try { // Open the file and output the buffer. WFIO.OpenForWriting(TestFileName2); BytesWritten = WFIO.WriteBlocks(VerBytesRead); WFIO.Close(); // Check to see if the data read in matches the verification buffer. if (BytesWritten != VerBytesRead) { S = " TestWrite: test failed - BytesWritten != VerBytesRead"; DispUIMsg(S); return; } // Read in the file that was just written out. WFIO.OpenForReading(TestFileName); BytesRead = WFIO.ReadUntilEOF(); WFIO.Close(); // Check to see if the data read in matches the verification buffer. if (BytesRead != BytesWritten) { S = " TestWriteBlocks: test failed - bytes read != bytes written"; DispUIMsg(S); return; } // Compare the 2 arrays. If there are any differences, then report an error. for (Loop = 0; Loop < BytesWritten; Loop++) { if (ByteBuf[Loop] != ByteBufVer[Loop]) { S = " TestWriteBlocks: test failed - the " + Loop.ToString() + " element does not match the verification buffer."; DispUIMsg(S); return; } } S = " TestWriteBlocks: Passed"; DispUIMsg(S); } catch (System.Exception ex) { S = " TestWriteBlocks: threw an exception of type " + ex.GetType().ToString(); DispUIMsg(S); S = " " + ex.Message; DispUIMsg(S); } }
public static void Extract(string FileName, Action <int, int> processedEvent, bool is64 = false) { using (FileStream fs = new FileStream(FileName, FileMode.Open)) { BinaryReader br = new BinaryReader(fs); BPKG pkg = new BPKG(fs, is64); for (var i = 0; i < pkg.Files.Length; i++) { BPKG_FTE fte = pkg.Files[i]; // Seek to location of binary file data fs.Position = fte.FileDataOffset; byte[] packedFileData = br.ReadBytes(fte.FileDataSizeStored); byte[] unpackedFileData = Unpack(packedFileData, fte.FileDataSizeStored, fte.FileDataSizeSheared, fte.FileDataSizeUnpacked, fte.IsEncrypted, fte.IsCompressed); string extractedPath = $"{FileName}.files\\{fte.FilePath}"; string extractedDir = new FileInfo(extractedPath).DirectoryName; if (!Directory.Exists(extractedDir)) { ; } Directory.CreateDirectory(extractedDir); if (extractedPath.EndsWith("xml") || extractedPath.EndsWith("x16")) { // decode bxml MemoryStream output = new MemoryStream(); MemoryStream input = new MemoryStream(unpackedFileData); Convert(input, BXML.DetectType(input), output, BXML_TYPE.BXML_PLAIN); using (WinFileIO writer = new WinFileIO(output.ToArray())) { writer.OpenForWriting(extractedPath); writer.WriteBlocks((int)output.Length); } } else { // extract raw File.WriteAllBytes(extractedPath, unpackedFileData); } processedEvent(i + 1, pkg.Files.Length); } } }
public void TestPositionIncrementsWrite() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestPositionIncrementsWrite"); byte[] buffer = new byte[] { 7, 3 }; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(filePath); wfio.WriteBlocks(buffer.Length); Assert.AreEqual(2, wfio.Position); wfio.Close(); fileNames.Add(filePath); }
public void TestWriteFirstByte() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestWriteFirstByte"); byte[] buffer = new byte[1]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(filePath); buffer[0] = FILE_CONTENT[0]; wfio.WriteBlocks(buffer.Length); wfio.Close(); byte[] expected = FILE_CONTENT.Take(1).ToArray(); byte[] actual = readFile(filePath); CollectionAssert.AreEqual(expected, actual); }
public void TestSetPositionReadWrite() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionReadWrite"); byte[] buffer = new byte[1]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForReadingWriting(filePath); Assert.AreEqual(0, wfio.Position); //Write stuff, move about writing more, then come back in & read it in etc... buffer[0] = 1; wfio.WriteBlocks(1); buffer[0] = 3; wfio.WriteBlocks(1); buffer[0] = 3; wfio.WriteBlocks(1); buffer[0] = 7; wfio.WriteBlocks(1); wfio.Position = 0; wfio.ReadBlocks(1); Assert.AreEqual(1, buffer[0]); wfio.ReadBlocks(1); Assert.AreEqual(3, buffer[0]); wfio.ReadBlocks(1); Assert.AreEqual(3, buffer[0]); wfio.ReadBlocks(1); Assert.AreEqual(7, buffer[0]); wfio.Position = 2; buffer[0] = 24; wfio.WriteBlocks(1); wfio.Position = 0; buffer[0] = 10; wfio.WriteBlocks(1); wfio.Position = 2; wfio.ReadBlocks(1); Assert.AreEqual(24, buffer[0]); wfio.Position = 0; wfio.ReadBlocks(1); Assert.AreEqual(10, buffer[0]); wfio.Close(); //Now check that the final file is what we expected it to be byte[] expected = new byte[] { 10, 3, 24, 7 }; byte[] actual = readFile(filePath); CollectionAssert.AreEqual(expected, actual); }
public void TestWriteWholeFile() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestWriteWholeFile"); byte[] buffer = new byte[FILE_CONTENT.Length]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(filePath); for (int i = 0; i < buffer.Length; i++) { buffer[i] = FILE_CONTENT[i]; } wfio.WriteBlocks(buffer.Length); wfio.Close(); byte[] expected = FILE_CONTENT; byte[] actual = readFile(filePath); CollectionAssert.AreEqual(expected, actual); }
public static void Compress(string Folder, Action <int, int> reportProgress, bool is64 = false, int compression = 1) { string[] files = Directory.EnumerateFiles(Folder, "*", SearchOption.AllDirectories).ToArray(); MemoryStream fileTableEntriesStream = new MemoryStream(); MemoryStream fileTableStream = new MemoryStream(); for (var i = 0; i < files.Length; i++) { reportProgress(i + 1, files.Length); string path = files[i].Replace(Folder, "").TrimStart('\\'); byte[] unpackedFileBuffer; using (FileStream fis = new FileStream(files[i], FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 2 << 15)) using (MemoryStream buf = new MemoryStream()) { if (path.EndsWith(".xml") || path.EndsWith(".x16")) { // encode bxml Convert(fis, BXML.DetectType(fis), buf, BXML_TYPE.BXML_BINARY); } else { // compress raw fis.CopyTo(buf); } unpackedFileBuffer = buf.ToArray(); } BPKG_FTE entry = new BPKG_FTE { FilePathLength = path.Length, FilePath = path, IsCompressed = true, IsEncrypted = true, FileDataOffset = (int)fileTableStream.Position, FileDataSizeUnpacked = unpackedFileBuffer.Length }; byte[] packedFileBuffer = Pack(unpackedFileBuffer, out entry.FileDataSizeSheared, out entry.FileDataSizeStored, entry.IsEncrypted, entry.IsCompressed, compression); fileTableStream.Write(packedFileBuffer, 0, packedFileBuffer.Length); entry.WriteTo(fileTableEntriesStream, is64); } MemoryStream packageStream = new MemoryStream(); BinaryWriter package = new BinaryWriter(packageStream); package.Write(new[] { (byte)'U', (byte)'O', (byte)'S', (byte)'E', (byte)'D', (byte)'A', (byte)'L', (byte)'B' }); // Signature package.Write(2); // Version package.Write(new byte[] { 0, 0, 0, 0, 0 }); // Unknown 1 if (is64) { package.Write(fileTableStream.Length); package.Write((long)files.Length); } else { package.Write((int)fileTableStream.Length); package.Write(files.Length); } package.Write(true); // Is compressed package.Write(true); // Is encrypted package.Write(new byte[62]); // Unknown 2 int FTESizePacked; byte[] packedFTEBuffer = Pack(fileTableEntriesStream.ToArray(), out _, out FTESizePacked, true, true, compression); if (is64) { package.Write((long)FTESizePacked); package.Write(fileTableEntriesStream.Length); } else { package.Write(FTESizePacked); package.Write((int)fileTableEntriesStream.Length); } package.Write(packedFTEBuffer); int globalOffset = (int)packageStream.Position + (is64 ? 8 : 4); if (is64) { package.Write((long)globalOffset); } else { package.Write(globalOffset); } // Write the packed file data package.Write(fileTableStream.ToArray()); WinFileIO writer = new WinFileIO(packageStream.ToArray()); writer.OpenForWriting(Folder.Replace(".files", "")); writer.WriteBlocks((int)packageStream.Length); writer.Close(); }
public void TestWriteFileWinApi2(string pathToFile, int bytesInFile) { _wfio.OpenForWriting(pathToFile); _wfio.WriteBlocks(bytesInFile); _wfio.Close(); }