使用base64对大文件进行编码

时间:2018-04-17 02:06:25

标签: php encoding base64

我正在尝试对base64编码中几MB或有时GB的文件进行编码/解码,但是某些数据以错误的方式进行编码/解码,导致奇怪的字符如: 。

我正在通过块编码读取文件块并单独保存(可能这是问题,但我无法弄明白)。

这是我到目前为止所尝试的内容:

<?php

function encode_file($Ifilename, $Efilename){
    $handle = fopen($Ifilename, 'rb');
    $outHandle = fopen($Efilename, 'wb');
    $bufferSize = 8151;
    while(!feof($handle)){
        $buffer = fread($handle, $bufferSize);
        $ebuffer = base64_encode($buffer);
        fwrite($outHandle, $ebuffer);
    }
    fclose($handle);
    fclose($outHandle);
}

function decode_file($Ifilename, $Efilename){
    $handle = fopen($Ifilename, 'rb');
    $outHandle = fopen($Efilename, 'wb');
    $bufferSize = 8151;
    while(!feof($handle)){
        $buffer = fread($handle, $bufferSize);
        $dbuffer = base64_decode($buffer);
        fwrite($outHandle, $dbuffer);
    }
    fclose($handle);
    fclose($outHandle);
}

encode_file('input.txt', 'out.bin');//Big text file ~4MBs

decode_file('out.bin', 'out.txt');

1 个答案:

答案 0 :(得分:0)

在读取base64上的整个Wikipedia article之后,我发现每3个字符编码为4个base64字符,这就是导致文件损坏的原因。

修复是在编码时简单地将缓冲区设置为n,其中n是3的倍数。

解码时将缓冲区设置为N,其中N是4的倍数。

工作代码:

<?php
function encode_file($Ifilename, $Efilename){
    $handle = fopen($Ifilename, 'rb');
    $outHandle = fopen($Efilename, 'wb');
    $bufferSize = 3 * 256;// 3 bytes of ASCII encodes to 4 bytes of base64
    while(!feof($handle)){
        $buffer = fread($handle, $bufferSize);
        $ebuffer = base64_encode($buffer);
        fwrite($outHandle, $ebuffer);
    }
    fclose($handle);
    fclose($outHandle);
}

function decode_file($Ifilename, $Efilename){
    $handle = fopen($Ifilename, 'rb');
    $outHandle = fopen($Efilename, 'wb');
    $bufferSize = 4 * 256; // 4 bytes of base64 decodes to 3 bytes of ASCII
    while(!feof($handle)){
        $buffer = fread($handle, $bufferSize);
        $dbuffer = base64_decode($buffer);
        fwrite($outHandle, $dbuffer);
    }
    fclose($handle);
    fclose($outHandle);
}

encode_file('input.txt', 'out.bin');

decode_file('out.bin', 'output.txt');