2009-01-24 88 views
26

有人可以提供一些关于如何做到这一点?我可以为常规文本或字节数组执行此操作,但不知道如何处理PDF。我是否首先将pdf填充到一个字节数组中?Base64在C#中编码PDF?

+2

为什么PDF应该与字节数组不同? – 2009-01-24 03:32:12

回答

39

使用File.ReadAllBytes加载PDF文件,然后使用Convert.ToBase64String(bytes)正常编码字节数组。

+2

Yikes!这可能会变大。 – 2009-01-24 03:34:43

+0

确实。 但是现在机器有很多内存。如果有必要,从文件中读取缓冲区块是一项非常标准的技术:) – 2009-01-24 04:52:19

34

有一种方式,你可以做到这一点块,这样你就不必一次烧掉大量的内存。

.Net包含一个编码器,可以完成分块,但它有种奇怪的地方。他们把它放在System.Security.Cryptography命名空间中。

我已经测试了下面的示例代码,并且使用我的方法或上面的Andrew的方法得到了相同的输出。

以下是它的工作原理:启动一个名为CryptoStream的类。这是一种插入另一个流的适配器。您将一个名为CryptoTransform的类插入到CryptoStream中(它依次连接到您的文件/内存/网络流),并在数据从数据流读取或写入数据流时对数据执行数据转换。

通常情况下,转换是加密/解密,但.net包括ToBase64和FromBase64转换,所以我们不会加密,只是编码。

这是代码。我列出了Andrew的建议(也许命名不清),以便您可以比较输出。

 

    class Base64Encoder 
    { 
     public void Encode(string inFileName, string outFileName) 
     { 
      System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.ToBase64Transform(); 
      using(System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName), 
             outFile = System.IO.File.Create(outFileName)) 
      using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(outFile, transform, System.Security.Cryptography.CryptoStreamMode.Write)) 
      { 
       // I'm going to use a 4k buffer, tune this as needed 
       byte[] buffer = new byte[4096]; 
       int bytesRead; 

       while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0) 
        cryptStream.Write(buffer, 0, bytesRead); 

       cryptStream.FlushFinalBlock(); 
      } 
     } 

     public void Decode(string inFileName, string outFileName) 
     { 
      System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform(); 
      using (System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName), 
             outFile = System.IO.File.Create(outFileName)) 
      using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(inFile, transform, System.Security.Cryptography.CryptoStreamMode.Read)) 
      { 
       byte[] buffer = new byte[4096]; 
       int bytesRead; 

       while ((bytesRead = cryptStream.Read(buffer, 0, buffer.Length)) > 0) 
        outFile.Write(buffer, 0, bytesRead); 

       outFile.Flush(); 
      } 
     } 

     // this version of Encode pulls everything into memory at once 
     // you can compare the output of my Encode method above to the output of this one 
     // the output should be identical, but the crytostream version 
     // will use way less memory on a large file than this version. 
     public void MemoryEncode(string inFileName, string outFileName) 
     { 
      byte[] bytes = System.IO.File.ReadAllBytes(inFileName); 
      System.IO.File.WriteAllText(outFileName, System.Convert.ToBase64String(bytes)); 
     } 
    } 
 

我也在玩附加CryptoStream的地方。在Encode方法中,我将它附加到输出(写入)流,所以当我实例化CryptoStream时,我使用它的Write()方法。

当我阅读时,我将它附加到输入(阅读)流,因此我使用CryptoStream上的读取方法。附加到哪个流并不重要。我只需将合适的Read或Write枚举成员传递给CryptoStream的构造函数即可。