Skip to content

Commit

Permalink
fix: reduce memory consumption (#78)
Browse files Browse the repository at this point in the history
Fixes #74
  • Loading branch information
bobbyg603 authored Oct 3, 2024
1 parent a211f2b commit ffcc77e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions BugSplatDotNetStandard/Utils/ZipUtils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
Expand All @@ -11,14 +12,21 @@ internal class InMemoryFile

public static InMemoryFile FromFileInfo(FileInfo fileInfo)
{
using (FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (MemoryStream memoryStream = new MemoryStream())
const int bufferSize = 81920; // 80 KB buffer
using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize))
{
fileStream.CopyTo(memoryStream);
return new InMemoryFile()
var bytes = new byte[fileInfo.Length];
int bytesRead, totalBytesRead = 0;

while ((bytesRead = fileStream.Read(bytes, totalBytesRead, Math.Min(bufferSize, bytes.Length - totalBytesRead))) > 0)
{
totalBytesRead += bytesRead;
}

return new InMemoryFile
{
FileName = fileInfo.Name,
Content = memoryStream.ToArray()
Content = bytes
};
}
}
Expand All @@ -30,7 +38,6 @@ internal interface IZipUtils
FileInfo CreateZipFile(string zipFileFullName, IEnumerable<FileInfo> files);
string CreateZipFileFullName(string inputFileName);
Stream CreateZipFileStream(string zipFileFullName);

}

internal class ZipUtils : IZipUtils
Expand Down

0 comments on commit ffcc77e

Please sign in to comment.