是否可以分块对文件进行base64编码? [英] Is it possible to base64-encode a file in chunks?

查看:34
本文介绍了是否可以分块对文件进行base64编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一个巨大的输入文件进行 base64 编码并最终得到一个文本输出文件,并且我正在尝试找出是否可以对输入文件进行逐位编码,或者我是否需要一次编码整个事物.

I'm trying to base64 encode a huge input file and end up with an text output file, and I'm trying to find out whether it's possible to encode the input file bit-by-bit, or whether I need to encode the entire thing at once.

这将在 AS/400 (iSeries) 上完成,如果这有什么不同的话.我正在使用自己的 base64 编码例程(用 RPG 编写),效果很好,而且,如果不是大小限制的情况,那就没问题了.

This will be done on the AS/400 (iSeries), if that makes any difference. I'm using my own base64 encoding routine (written in RPG) which works excellently, and, were it not a case of size limitations, would be fine.

推荐答案

不可能逐位,而是一次3个字节,或者一次是 3 个字节的倍数就可以了!.

换句话说,如果您将输入文件拆分为大小为 3 个字节的倍数的块",您可以分别对这些块进行编码,并将生成的 B64 编码片段拼凑在一起(在相应的顺序,当然.请注意,最后一个 chuink 的大小不必完全是 3 个字节的倍数,取决于其大小的模 3 值,其对应的 B64 值将包含一些这样的填充字符(通常是等号)但这没关系,因为这将是唯一具有(并且需要)这种填充的部分.

In other words if you split your input file in "chunks" which size(s) is (are) multiples of 3 bytes, you can encode the chunks separately and piece together the resulting B64-encoded pieces together (in the corresponding orde, of course. Note that the last chuink needn't be exactly a multiple of 3 bytes in size, depending on the modulo 3 value of its size its corresponding B64 value will have a few of these padding characters (typically the equal sign) but that's ok, as thiswill be the only piece that has (and needs) such padding.

在解码方向上,思路相同,只是需要将B64编码的数据拆分为4字节的倍数.根据需要并行/单独解码它们,并通过将解码部分附加在一起(再次以相同顺序)重新组合原始数据.

In the decoding direction, it is the same idea except that you need to split the B64-encoded data in multiples of 4 bytes. Decode them in parallel / individually as desired and re-piece the original data by appending the decoded parts together (again in the same order).

示例:

文件"内容 =永远不要与数据争论."(吉米中子).
直接编码 = Ik5ldmVyIGFyZ3VlIHdpdGggdGhlIGRhdGEuIiAoSmltbXkgTmV1dHJvbik=

"File" contents = "Never argue with the data." (Jimmy Neutron).
Straight encoding = Ik5ldmVyIGFyZ3VlIHdpdGggdGhlIGRhdGEuIiAoSmltbXkgTmV1dHJvbik=

现在,分块:
"永不争吵     -->     Ik5ldmVyIGFyZ3Vl
     -->    IHdpdGggdGhl
data." (Jimmy Neutron) --> IGRhdGEuIiAoSmltbXkgTmV1dHJvbik=

Now, in chunks:
"Never argue     -->     Ik5ldmVyIGFyZ3Vl
with the         -->        IHdpdGggdGhl
data." (Jimmy Neutron) --> IGRhdGEuIiAoSmltbXkgTmV1dHJvbik=

如您所见,3 个编码块的数量与为整个文件生成的代码相同.

As you see piece in that order the 3 encoded chunks amount the same as the code produced for the whole file.

解码是类似的,只要它们是 4 字节的倍数,就可以使用任意大小的块.用于编码的大小之间绝对不需要任何类型的对应关系.(尽管将每个方向(例如 300 和 400)标准化为一个单一尺寸可能会使事情更加统一且更易于管理.

Decoding is done similarly, with arbitrary chuncked sized provided they are multiples of 4 bytes. There is absolutely not need to have any kind of correspondance between the sizes used for encoding. (although standardizing to one single size for each direction (say 300 and 400) may makes things more uniform and easier to manage.

这篇关于是否可以分块对文件进行base64编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆