Base64 encode/decode large file
转载: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.
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的更多相关文章
- node_nibbler:自定义Base32/base64 encode/decode库
https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [ ...
- javascript base64 encode decode 支持中文
* 字符编码 ** 一定要知道数据的字符编码 ** 使用utf-8字符编码存储数据 ** 使用utf-8字符编码输出数据 * Crypto.js 支持中文 Base64编码说明 Base64编码要求把 ...
- BASE64 Encode Decode
package com.humi.encryption; import java.io.IOException; import java.io.UnsupportedEncodingException ...
- python encode decode unicode区别及用法
decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...
- java URLEncoder 和Base64.encode()
参考: http://www.360doc.com/content/10/1103/12/1485725_66213001.shtml (URLEncode) http://blog.csdn.net ...
- python编码问题之\"encode\"&\"decode\"
python encode decode 编码 decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换 ...
- python编码encode decode(解惑)
关于python 字符串编码一直没有搞清楚,今天总结了一下. Python 字符串类型 Python有两种字符串类型:str 与 unicode. 字符串实例 # -*- coding: utf-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 ...
- python3.3 unicode(encode&decode)
最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...
随机推荐
- 延期(deferred)的承诺(promise) — jq异步编程浅析
引子 相信各位developers对js中的异步概念不会陌生,异步操作后的逻辑由回调函数来执行,回调函数(callback function)顾名思义就是“回头调用的函数”,函数体事先已定义好,在未来 ...
- SQL Server 2008空间数据应用系列一:空间信息基础
转自:http://www.cnblogs.com/beniao/archive/2011/01/18/1933412.html Microsoft SQL Server 2008 提供了全面性的空间 ...
- GotFocus和PreviewLeftButtonDown事件
当TextBox获得焦点后,其中的文字会被全选.通过GotFocus和PreviewLeftButtonDown事件,就可以模拟上述行为. 如果用户只是用键盘操作,GotFocus事件就足够了. 如果 ...
- javascript二维数组
var a= new Array(new Array(1,2),new Array('b','c')); document.write(a[1][1]); 说白了,就是利用for循环定义二维数组! & ...
- 配置Memcache服务器并实现主从复制功能(repcached)(转)
1.repcached介绍repcached 是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步 ...
- C# 多线程是否结束可通过线程池可以判断
C# ManualResetEvent信号状态判断线程池是否结束 这是一段重要的代码,小猪两个小时的研究成果,记下来备查. using System; using System.Collection ...
- cocos2d-x ios8 输入框显示bug
https://github.com/cocos2d/cocos2d-x/pull/8149
- Java JDK8 安装及环境变量配置
步骤: 1.下载JDK 2.安装 3.配置环境变量 注意: 1.在选择安装路径时,不要选择C盘或D盘下的[Program Files]目录.此目录名中有空格,会导致配置不成功. 参照链接: http: ...
- STM32F407 ADC DMA 采样实验
转载:http://home.eeworld.com.cn/my/space-uid-361439-blogid-239703.html STM32F407ADC采样实验 热度 1已有 5472 次阅 ...
- C#- FTP递归下载文件
c# ftp递归下载文件,找来找去这个最好.(打断点,一小处foreach要改成for) /// <summary> /// ftp文件上传.下载操作类 /// </summary& ...