将 zip 文件编码为 Base64

use*_*198 5 c# zip base64 encode

我想将 zip 文件解码为 base64 字符串。文件可能达到 100 MB,并出现 OutOfMemory 异常并重新启动 Visual Studio。我的编码代码:

public static string EncodeToBase64(string zipPath)
{
    using (FileStream fs = new FileStream(zipPath, FileMode.Open, FileAccess.Read))
    {
       byte[] filebytes = new byte[fs.Length];
       fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
       return Convert.ToBase64String(filebytes);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以做什么来解决它?

Dav*_*ell 3

不要试图一次性完成所有事情。

循环访问 FileStream 中的字节。获取三个字节的倍数并对它们进行编码。请务必使用 StringBuilder 来构建输出。

这将大大减少内存使用量。