1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/*
This should have been run during the encode step.
create directory TestAA_DIR as '/testcases/base64';
grant all on directory TESTAA_DIR to refresh;
*/
set serveroutput on
declare
l_directory_name VARCHAR2(40) := 'TESTAA_DIR';
l_test blob;
file_out utl_file.file_type;
l_clob_length number;
l_attachment_clob clob;
bfile_data BFILE;
v_size_element number :=0;
v_size_endelement number := 0;
l_pos number := 1;
l_chunk_size number := 1000;
l_dest_offset INTEGER := 1;
l_src_offset INTEGER := 1;
l_src_csid NUMBER := NLS_CHARSET_ID('UTF8');
l_lang_ctx INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
l_warn INTEGER;
FUNCTION utl_base64_decode_pdf_file
( l_pdf_blob IN BLOB
) RETURN BLOB
IS
l_pdf_blob_decoded BLOB;
BEGIN
l_pdf_blob_decoded := NULL;
l_pdf_blob_decoded := utl_encode.base64_decode(l_pdf_blob);
RETURN l_pdf_blob_decoded;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END utl_base64_decode_pdf_file;
begin
/* read the file as character [clob] and strip out the xml tag before decoding */
dbms_lob.createtemporary(l_attachment_clob, true);
bfile_data := BFILENAME( l_directory_name, 'result-xml-clob.txt' );
dbms_lob.fileopen(bfile_data, dbms_lob.file_readonly);
dbms_lob.loadclobfromfile( dest_lob => l_attachment_clob
, src_bfile => bfile_data
, amount => dbms_lob.lobmaxsize
, dest_offset => l_dest_offset
, src_offset => l_src_offset
, bfile_csid => l_src_csid
, lang_context => l_lang_ctx
, warning => l_warn
);
dbms_lob.fileclose(bfile_data);
/* now strip out the <Attachment> tag added when encoding */
v_size_element := dbms_lob.instr(l_attachment_clob, '>' /*default offset and default first occurrence*/);
v_size_endelement := v_size_element + 1;
/* strip off the last one first, as not to affect the instr location of the first tag */
dbms_lob.erase(l_attachment_clob, v_size_endelement, dbms_lob.getlength(l_attachment_clob) - v_size_endelement);
/* then strip off the first xml tag */
dbms_lob.erase(l_attachment_clob, v_size_element /*default offset*/ );
/* now the file should have the XML tag stripped out, though white space is there */
dbms_output.put_line('clob size w/o xml tag ' ||dbms_lob.getlength(l_attachment_clob));
l_clob_length := dbms_lob.getlength(l_attachment_clob);
/* reset variables used when loading clob */
l_dest_offset :=1;
l_src_offset :=1;
l_warn := null;
l_lang_ctx := DBMS_LOB.DEFAULT_LANG_CTX;
/* convert the clob to a blob to decode */
dbms_lob.createtemporary(l_test, true);
dbms_lob.converttoblob(dest_lob => l_test
,src_clob => l_attachment_clob
,amount => l_clob_length
,dest_offset => l_dest_offset
,src_offset => l_src_offset
,blob_csid => l_src_csid
,lang_context => l_lang_ctx
,warning => l_warn);
/* decode */
l_test := utl_base64_decode_pdf_file(l_test);
/* write the decoded file to disk */
dbms_output.put_line('length: ' || dbms_lob.getlength(l_test));
file_out := utl_file.fopen(l_directory_name,'decoded-file.pdf','WB');
utl_file.put_raw(file_out, l_test);
utl_file.fflush(file_out);
utl_file.fclose(file_out);
end;
/
|
Partager