Base64 encode/decode large file

大文件Base64编码解码技巧
本文探讨了在处理大文件时使用Base64编码存在的内存限制问题,并提供了一种通过逐块读取文件并进行编码的方法,避免了内存溢出的风险。此外,还介绍了如何支持MIME格式编码。

作者: 江大鱼 发表于 2008-04-20 21:48 原文链接 阅读: 754 评论: 0

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)
ExpandedBlockStart.gifContractedBlock.gif
{
    
return Convert.ToBase64String(data);
}

        
public   byte [] Decode( string  strBase64)
ExpandedBlockStart.gifContractedBlock.gif
{
    
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)
ExpandedBlockStart.gifContractedBlock.gif
{
            
using(FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using(StreamWriter outputWriter = new StreamWriter(outputFile, false, Encoding.ASCII))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
byte[] data = new byte[3 * 1024]; //Chunk size is 3k
                    int read    = inputStream.Read(data, 0, data.Length);
                    
                    
while(read > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        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)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
using (FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using (FileStream outputStream = File.Create(outputFile))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        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();
            }

        }


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

public   static   void  EncodeFile( string  inputFile,  string  outputFile)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
using(FileStream inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using(StreamWriter outputWriter = new StreamWriter(outputFile, false, Encoding.ASCII))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
byte[] data = new byte[57 * 1024]; //Chunk size is 57k
                    int read    = inputStream.Read(data, 0, data.Length);
                    
                    
while(read > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        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)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
using (StreamReader reader = new StreamReader(inputFile, Encoding.ASCII, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using (FileStream outputStream = File.Create(outputFile))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{                
                    
string line = reader.ReadLine();

                    
while (!string.IsNullOrEmpty(line))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
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();
            }

        }
好久没更新BLOG了, 今天来篇英文的文章,真是倍感吃力阿。
作者: 江大鱼
出处: http://jzywh.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

评论: 0 查看评论 发表评论

软件研发团队管理年会(上海,7.10-7.11)


最新新闻:
· 6个强大的社会化网络搜索引擎(2010-06-12 08:58)
· 马雪征:把联想的经验带入TPG(2010-06-12 08:54)
· 苹果如何基业长青(2010-06-12 08:52)
· 腾讯:平台优势催动“利润发生器”(2010-06-12 08:52)
· Google News测试新版页面(2010-06-12 08:50)

编辑推荐:谈谈IT软件开发工程师的基本功

网站导航:博客园首页  个人主页  新闻  闪存  小组  博问  社区  知识库

转载于:https://my.oschina.net/kjiang/blog/5513

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值