private void TestWriteFileWinAPI1() { // This function tests out the Windows API function WriteFile. This function tests the WriteFile function // by writing out the entire file with one call. 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.Write(BytesInFiles[FileLoop]); WFIO.Close(); stopwatch.Stop(); S = " Total time writing " + FileDesc[FileLoop] + " with WFIO1.Write API No Parsing = " + stopwatch.Elapsed.TotalMilliseconds.ToString(); DispUIMsg(S); } } catch (System.Exception ex) { S = "TestWriteFileWinAPI1: threw an exception of type " + ex.GetType().ToString(); DispUIMsg(S); DispUIMsg(ex.Message); } }
private void TestWrite() { // This function tests the Write function. int BytesWritten, BytesRead, Loop; String S; try { // Open the file and output the buffer. WFIO.OpenForWriting(TestFileName2); BytesWritten = WFIO.Write(VerBytesRead); WFIO.Close(); // Check to see if the data that was written out 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(TestFileName2); BytesRead = WFIO.ReadUntilEOF(); WFIO.Close(); // Check to see if the data read in matches the verification buffer. if (BytesRead != BytesWritten) { S = " TestWrite: 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 = " TestWrite: test failed - the " + Loop.ToString() + " element does not match the verification buffer."; DispUIMsg(S); return; } } S = " TestWrite: Passed"; DispUIMsg(S); } catch (System.Exception ex) { S = " TestWrite: threw an exception of type " + ex.GetType().ToString(); DispUIMsg(S); S = " " + ex.Message; DispUIMsg(S); } }
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); }
public static bool Copy(string sourceFileName, string destFileName, bool overwrite, IFileCopyCallout callout, Action <ProgressMessage> report = null, Func <bool> cancel = null ) { byte[] buf = new byte[BUFSIZ]; DateTime dt0 = DateTime.Now; long ivlcnt = 0; long total = new System.IO.FileInfo(sourceFileName).Length; long count = 0; using (var threadProgress = new ThreadProgress(report)) { using (WinFileIO wfioRd = new WinFileIO(buf), wfioWr = new WinFileIO(buf)) { wfioRd.OpenForReading(sourceFileName); wfioWr.OpenForWriting(destFileName, overwrite); int read = 0; while (true) { if (cancel?.Invoke() ?? false) { return(false); } read = wfioRd.ReadBlocks(BUFSIZ); if (read <= 0) { break; } callout?.ProcessBuffer(buf, read, count); wfioWr.Write(read); count += read; DateTime dt = DateTime.Now; long tot_ms = (int)(dt - dt0).TotalMilliseconds; long q = tot_ms / IVL_MS; if (q <= ivlcnt) { continue; } ivlcnt = q; threadProgress.Report((double)count / total); } ; } } return(true); }
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 TestInitialPositionWrite() { const string FILE_NAME = "WinFileIOTests.TestInitialPositionWrite"; byte[] buffer = new byte[1]; WinFileIO wfio = new WinFileIO(buffer); wfio.OpenForWriting(FILE_NAME); Assert.AreEqual(0, wfio.Position); wfio.Close(); fileNames.Add(FILE_NAME); }
public void TestLengthWrite() { string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestLengthWrite"); 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.Close(); fileNames.Add(filePath); }
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 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 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 TestWriteFileWinApi1(string pathToFile, int bytesInFile) { _wfio.OpenForWriting(pathToFile); _wfio.Write(bytesInFile); _wfio.Close(); }