Use block-based reading instead of CopyTo

This commit is contained in:
Matt Nadareski
2025-10-09 10:31:59 -04:00
parent 41bc410452
commit a9ea457808
2 changed files with 16 additions and 4 deletions

View File

@@ -40,6 +40,7 @@
- Pre-compress skeleton files with Zstd
- Fix broken file count tests
- Pre-compress state files with Zstd
- Use block-based reading instead of CopyTo
### 3.4.2 (2025-09-30)

View File

@@ -676,15 +676,26 @@ namespace MPF.Processors
if (!File.Exists(file))
return false;
// Create and write the output
try
{
// Prepare the input and output streams
using var ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var ofs = File.Open($"{file}.zst", FileMode.Create, FileAccess.Write, FileShare.None);
using var zst = new ZStandardStream(ofs, CompressionMode.Compress, compressionLevel: 19, leaveOpen: true);
ifs.CopyTo(zst);
zst.Flush();
// Compress and write in blocks
int read = 0;
do
{
byte[] buffer = new byte[3 * 1024 * 1024];
read = ifs.Read(buffer, 0, buffer.Length);
if (read == 0)
break;
zst.Write(buffer, 0, read);
zst.Flush();
} while (read > 0);
}
catch
{