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 ...
随机推荐
- Java数值转化为二进制、十进制、十六进制字符串
int i = 123;String binStr = Integer.toBinaryString(i); String otcStr = Integer.toOctalString(i); Str ...
- ArrayList源码解析
ArrayList简介 ArrayList定义 1 public class ArrayList<E> extends AbstractList<E> implements L ...
- MySQL优化技巧之五(mysql查询性能优化)
对于高性能数据库操作,只靠设计最优的库表结构.建立最好的索引是不够的,还需要合理的设计查询.如果查询写得很糟糕,即使库表结构再合理.索引再合适,也无法实现高性能.查询优化.索引优化.库表结构优化需要齐 ...
- LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树
分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...
- Java和C++的区别
这是一个Java语言和C++语言之间的比较. 目录 [隐藏] 1 设计目标 2 语言特性 2.1 语法 2.2 语义 2.3 资源管理 2.4 库 2.5 运行时 2.6 模板 vs. 泛型 2.7 ...
- Timer和counter
什么是Timer,什么是Counter 几乎每个嵌入式板都会有counter和timer,重要性比肩gpio.本质上来看timer和counter几乎是一样的东西,底层都是一个硬件counter,如果 ...
- HW3.1
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- [转]2-SAT问题及其算法
转自:http://www.cppblog.com/MatoNo1/archive/2011/07/13/150766.html [2-SAT问题]现有一个由N个布尔值组成的序列A,给出一些限制关系, ...
- HDU1075 - What Are You Talking About(Trie树)
题目大意 给定一些火星文单词以及对应的英语单词,然后给你一些火星文,要求你翻译成对应的英文 题解 第一次写Trie树! 把所有火星文单词插入到Trie树中,并且每个火星文单词结尾的节点记录相应英文单词 ...
- java中服务器启动时,执行定时任务
package com.ripsoft.util; import java.util.Calendar; import java.util.Timer; import javax.servlet.Se ...