转载:http://www.cnblogs.com/jzywh/archive/2008/04/20/base64_encode_large_file.html

The class System.Convert provide two basic methods "ToBase64String()" and "Convert.FromBase64String()" to encode a byte array to a base64 string and decode a base64 string to a byte array.

public string Encode(byte[] data)
{
    return Convert.ToBase64String(data);
}
        
public byte[] Decode(string strBase64)
{
    return Convert.FromBase64String(strBase64);
}

It is very good to use them to encode and decode base64. But in some case, it is a disaster.

For example, if you want to encode a 4 gb file to base64, the code above must throw an OutOfMemory exception., because you must read the file into a byte array. So we need to look for another way to encode and decode by base64.

Long days ago, a man have posted an article about how to deal with it.

http://blogs.microsoft.co.il/blogs/kim/archive/2007/10/09/base64-encode-large-files-very-large-files.aspx

This man use XmlWriter to work around it.

By researching the basis of the Base64 encoding in rfc, I found another more directly way to deal with it.

According rfc3548, base64 encode data in the unit of 3 bytes to 4 bytes, if the last part's length is less than 3,
the char '=' will be padded. So we can encode file in small chunks whose size is 3, then we can get the encoding data of the file by combiling encoding data of every chunks.

So I have below code:

public void EncodeFile(string inputFile, string outputFile)
{
       using(FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
          {
                using(StreamWriter outputWriter = new StreamWriter(outputFile, false, Encoding.ASCII))
              {
                  byte[] data = new byte[3 * 1024]; //Chunk size is 3k
                  int read    = inputStream.Read(data, 0, data.Length);
                  
               while(read > 0)
                    {
                      outputWriter.Write(Convert.ToBase64String(data, 0, read));
                      read = inputStream.Read(data, 0, data.Length);
                  }
                  
                  outputWriter.Close();                    
              }
              
              inputStream.Close();
          }
      }

    public void DecodeFile(string inputFile, string outputFile)
        {
          using (FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
              using (FileStream outputStream = File.Create(outputFile))
                {
                  byte[] data = new byte[4 * 1024]; //Chunk size is 4k
                  int read = inputStream.Read(data, 0, data.Length);

                  byte[] chunk    = new byte[3 * 1024];
          
                  while (read > 0)
                   {
                      chunk = Convert.FromBase64String(Encoding.ASCII.GetString(data, 0, read));
                      outputStream.Write(chunk, 0, chunk.Length);
                      read = inputStream.Read(data, 0, data.Length);
                  }

                  outputStream.Close();
              }

              inputStream.Close();
          }
      }

The methods also can be improved to support mime format (76 chars per line).

public static void EncodeFile(string inputFile, string outputFile)
       {
         using(FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
           {
              using(StreamWriter outputWriter = new StreamWriter(outputFile, false, Encoding.ASCII))                {
                  byte[] data = new byte[57 * 1024]; //Chunk size is 57k
                  int read    = inputStream.Read(data, 0, data.Length);
                  
                 while(read > 0)
                   {
                      outputWriter.WriteLine(Convert.ToBase64String(data, 0, read, Base64FormattingOptions.InsertLineBreaks));
                      read = inputStream.Read(data, 0, data.Length);
                  }
                  
                  outputWriter.Close();                    
              }
              
              inputStream.Close();
          }
      }

      public static void DecodeFile(string inputFile, string outputFile)
       {
      using (StreamReader reader = new StreamReader(inputFile, Encoding.ASCII, true))
           {
          using (FileStream outputStream = File.Create(outputFile))
               {                
              string line = reader.ReadLine();

              while (!string.IsNullOrEmpty(line))
                   {
                  if (line.Length > 76)
                      throw new InvalidDataException("Invalid mime-format base64 file");

                  byte[] chunk = Convert.FromBase64String(line);
                  outputStream.Write(chunk, 0, chunk.Length);
                  line = reader.ReadLine();
              }

              outputStream.Close();
          }

          reader.Close();
      }
  }

 

Base64 encode/decode large file的更多相关文章

  1. node_nibbler:自定义Base32/base64 encode/decode库

    https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [ ...

  2. javascript base64 encode decode 支持中文

    * 字符编码 ** 一定要知道数据的字符编码 ** 使用utf-8字符编码存储数据 ** 使用utf-8字符编码输出数据 * Crypto.js 支持中文 Base64编码说明 Base64编码要求把 ...

  3. BASE64 Encode Decode

    package com.humi.encryption; import java.io.IOException; import java.io.UnsupportedEncodingException ...

  4. python encode decode unicode区别及用法

    decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...

  5. java URLEncoder 和Base64.encode()

    参考: http://www.360doc.com/content/10/1103/12/1485725_66213001.shtml (URLEncode) http://blog.csdn.net ...

  6. python编码问题之\"encode\"&\"decode\"

    python encode decode 编码 decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换 ...

  7. python编码encode decode(解惑)

    关于python 字符串编码一直没有搞清楚,今天总结了一下. Python 字符串类型 Python有两种字符串类型:str 与 unicode. 字符串实例 # -*- coding: utf-8 ...

  8. (转)Integrating Intel® Media SDK with FFmpeg for mux/demuxing and audio encode/decode usages 1

    Download Article and Source Code Download Integrating Intel® Media SDK with FFmpeg for mux/demuxing ...

  9. python3.3 unicode(encode&decode)

    最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...

随机推荐

  1. MVC中的@Html.DisplayFor等方法如何控制日期的显示格式(转)

    http://www.tuicool.com/articles/BNVBR3 在Sql Server2005中,如果将某字段定义成日期 时间 类型DateTime,那么在视图中会默认显示成年月日时分秒 ...

  2. Read ListViewItem content from another process z

    Normal Windows GUI applications work with messages that are sent to a window or control and the cont ...

  3. oracle执行.sql文件

    ->win+R; ->CMD; ->SQLPLUS /NOLOG; ->CONNECT USER/PASSWORD@ORCL; ->@D:/XXX.SQL;

  4. 启动Selenium RC —— 我的第一个shell

    打开终端 1. 新建一个sh文件 $ vim a.sh 2. 写入以下内容 #! /bin/bash cd Desktop/selenium/jar java -jar selenium-server ...

  5. C#调用VC DLL堆栈不对称

    今天在调程序时,C#调用VC 编译的dll出现堆栈不对称,查了一下资料,转载在这里供大家参考. 问题描述:对 PInvoke 函数“xxFunction()”的调用导致堆栈不对称.原因可能是托管的 P ...

  6. 2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)

    http://acm.uestc.edu.cn/#/problem/show/1218 既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个.有一个trick就是如果只放一根,那么多长都可 ...

  7. [置顶] 使用U盘安装ubuntu系统

    使用U盘安装ubuntu系统 在网上找了很多教程,都不起效,提示:“从光盘上读取数据出错”. 总结出了几个关键点. 首先,版本,Ubuntu 12.04 Server,一般的U盘安装都会报:“从光盘上 ...

  8. 如何解决Python脚本在Linux和Windows上的格式问题

    python是一种对缩进有严格要求的语言, Python脚本可以使用非常多的工具进行编写,笔者在Linux系统使用JEdit进行Python脚本编写,由于在Linux编写脚本比较痛苦,比如想一眼看出相 ...

  9. HTML5初学者福利!11个在线学习网站推荐

    HTML5初学者福利!11个在线学习网站推荐 HTML5的强大及流行趋势,让更多的人想要系统的对它进行学习.而大多数人获取HTML5知识的重要途径都是网络,不过面对五花八门的搜索结果,是不是觉得摸不着 ...

  10. heX:用HTML5和Node.JS开发桌面应用

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...