ORACLE Database Discussions: Generating Password Protected PDF Document Usuing As - PDF - Mini Package - SQL & PL/SQL
ORACLE Database Discussions: Generating Password Protected PDF Document Usuing As - PDF - Mini Package - SQL & PL/SQL
Generating password protected pdf document usuing as_pdf_mini package - SQL & PL/SQL
Actually i want to implement password protected pdf file which is generated using as_pdf_mini.
so i want help on that.....
946903 wrote:
Actually i want to implement password protected pdf file which is generated using as_pdf_mini.
so i want help on that.....What does this have to do with Oracle RDBMS?
i have used this pl/sql package...i m successfully generating pdf documents with this package....but i want to have password protected pdf files...so wanted help on that...
as_pdf3 has more possiblities but no password protection. But is not that difficult to add password protection, it took me 4 hours to add it to my working version as_pdf4 :)
And no, as_pdf4 is not ready for publication at this moment.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
t_u := dbms_crypto.encrypt( t_encryption_padding, dbms_crypto.encrypt_rc4, utl_raw.substr( t_tmp, 1, 5 ) );
--dbms_output.put_line( t_u );
t_tmp := utl_raw.concat( utl_raw.substr( t_tmp, 1, 5 )
, utl_raw.substr( utl_raw.cast_from_binary_integer( 8, utl_raw.little_endian ), 1, 3 ) -- object nr
, utl_raw.substr( utl_raw.cast_from_binary_integer( 0, utl_raw.little_endian ), 1, 2 ) -- generation nr
);
t_tmp := dbms_crypto.hash( t_tmp, dbms_crypto.hash_md5 );
dbms_output.put_line( dbms_crypto.encrypt( 'FEFF', dbms_crypto.encrypt_rc4, utl_raw.substr( t_tmp, 1, 10 ) ) );
dbms_output.put_line( dbms_crypto.encrypt( utl_raw.cast_to_raw( 'AS-PDF 0.3.0 by Anton Scheffer' ), dbms_crypto.encrypt_rc4, utl_raw.substr( t_tmp, 1, 10 ) ) );
end;Anton
Related
Hello Username,
with UTLCOMPRESS you can only zip one file. If you need more than one you can use JAVA http://www.oracle.com/global/de/community/tipps/zip/index.html.
Regards
Marcus
Edited by: Marwim on 17.08.2010 15:27
Username wrote:
Hey everyone, I'm having trouble figuring out how to use UTL_COMPRESS.
What I want to do is take several BLOBs from one table, and put them into one zip file. Can anyone show me an example of where this is done?
{ snip }
So what I've posted works fine, it compresses a single file. What I want is to take all three of those blobs (for example) and add them to one zip file. I've looked into using UTL_COMPRESS.LZ_COMPRESS
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
b_dl_file2 BLOB;
b_dl_file3 BLOB;
-
function little_endian( p_big in number, p_bytes in pls_integer := 4 )
return raw
is
begin
return utl_raw.substr( utl_raw.cast_from_binary_integer( p_big, utl_raw.little_endian ), 1, p_bytes );
end;
--
procedure add1file
( p_zipped_blob in out blob
, p_name in varchar2
, p_content in blob
)
is
t_now date;
t_blob blob;
t_clen integer;
begin
t_now := sysdate;
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob );
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob, true );
end if;
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0304' ) -- Local file header signature
, hextoraw( '1400' ) -- version 2.0
, hextoraw( '0000' ) -- no General purpose bits
, hextoraw( '0800' ) -- deflate
, little_endian( to_number( to_char( t_now, 'ss' ) ) / 2
+ to_number( to_char( t_now, 'mi' ) ) * 32
+ to_number( to_char( t_now, 'hh24' ) ) * 2048
, 2
) -- File last modification time
, little_endian( to_number( to_char( t_now, 'dd' ) )
+ to_number( to_char( t_now, 'mm' ) ) * 32
+ ( to_number( to_char( t_now, 'yyyy' ) ) - 1980 ) * 512
, 2
) -- File last modification date
, dbms_lob.substr( t_blob, 4, t_clen - 7 ) -- CRC-32
, little_endian( t_clen - 18 ) -- compressed size
, little_endian( dbms_lob.getlength( p_content ) ) -- uncompressed size
, little_endian( length( p_name ), 2 ) -- File name length
, hextoraw( '0000' ) -- Extra field length
, utl_raw.cast_to_raw( p_name ) -- File name
)
);
dbms_lob.append( p_zipped_blob, dbms_lob.substr( t_blob, t_clen - 18, 11 ) ); -- compressed content
dbms_lob.freetemporary( t_blob );
end;
--
procedure finish_zip( p_zipped_blob in out blob )
is
t_cnt pls_integer := 0;
t_offs integer;
t_offs_dir_header integer;
t_offs_end_header integer;
t_comment raw(32767) := utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
begin
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := dbms_lob.instr( p_zipped_blob, hextoraw( '504B0304' ), 1 );
while t_offs > 0
loop
t_cnt := t_cnt + 1;
dbms_lob.append( p_zipped_blob
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
, utl_raw.concat( hextoraw( '504B0102' ) -- Central directory file header signature
, hextoraw( '1400' ) -- version 2.0
, dbms_lob.substr( p_zipped_blob, 26, t_offs + 4 )
, hextoraw( '0000' ) -- File comment length
, hextoraw( '0000' ) -- Disk number where file starts
, hextoraw( '0100' ) -- Internal file attributes
, hextoraw( '2000B681' ) -- External file attributes
, little_endian( t_offs - 1 ) -- Relative offset of local file header
, dbms_lob.substr( p_zipped_blob
, utl_raw.cast_to_binary_integer( dbms_lob.substr( p_zipped_blob, 2, t_offs + 26 ), utl_raw.little_endian )
, t_offs + 30
) -- File name
)
);
t_offs := dbms_lob.instr( p_zipped_blob, hextoraw( '504B0304' ), t_offs + 32 );
end loop;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0506' ) -- End of central directory signature
, hextoraw( '0000' ) -- Number of this disk
, hextoraw( '0000' ) -- Disk where central directory starts
, little_endian( t_cnt, 2 ) -- Number of central directory records on this disk
, little_endian( t_cnt, 2 ) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header ) -- Size of central directory
, little_endian( t_offs_dir_header ) -- Relative offset of local file header
, little_endian( nvl( utl_raw.length( t_comment ), 0 ), 2 ) -- ZIP file comment length
, t_comment
)
);
end;
--
procedure save_zip
( p_zipped_blob in blob
, p_dir in varchar2 := 'MY_DIR'
, p_filename in varchar2 := 'my.zip'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir, p_filename, 'wb' );
for i in 0 .. trunc( ( dbms_lob.getlength( p_zipped_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh, dbms_lob.substr( p_zipped_blob, t_len, i * t_len + 1 ) );
end loop;
utl_file.fclose( t_fh );
end;
begin
select FILE_BLOB
into b_dl_file1
from FILES
where FILE_ID = 64;
select FILE_BLOB
into b_dl_file2
from FILES
where FILE_ID = 65;
select FILE_BLOB
into b_dl_file3
from FILES
where FILE_ID = 66;
add1file( g_zipped_blob, 'test1.bin', b_dl_file1 );
add1file( g_zipped_blob, 'test2.bin', b_dl_file2 );
add1file( g_zipped_blob, 'test2.bin', b_dl_file3 );
finish_zip( g_zipped_blob );
save_zip( g_zipped_blob, 'MY_DIR', 'my.zip' );
end;Anton
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Hats off, Anton! One of your superb great posts again ;)
That 's the continuation for one of the elder posts about Re: Create Zip File:
»
But maybe, if one elaborates more on this, one can modify the bytestream to add some zip headers to make the file zip-compatible
«
you just did it, bravo!
Wow! Thanks! That's exactly what I'm looking for. I'm having some trouble getting this code to run though, I'm recieving the following error:
"ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275 "
I've been debugging and it looks like its coming from the add1file procedure.
I modified the code a bit to make it into a package:
CREATE OR REPLACE PACKAGE zip_blobs AS
/*Package written by Anton Scheffer, used with permission*/
PROCEDURE add1file(p_zipped_blob in out blob, p_name in varchar2, p_content in blob);
PROCEDURE finish_zip(p_zipped_blob in out blob);
PROCEDURE save_zip(p_zipped_blob in blob, p_dir in varchar2, p_filename in varchar2);
END zip_blobs;CREATE OR REPLACE PACKAGE BODY zip_blobs AS
--
function little_endian( p_big in number, p_bytes in pls_integer := 4 )
return raw
is
begin
return utl_raw.substr( utl_raw.cast_from_binary_integer( p_big, utl_raw.little_endian ), 1, p_bytes );
end;
--
procedure add1file
( p_zipped_blob in out blob
, p_name in varchar2
, p_content in blob
)
is
t_now date;
t_blob blob;
t_clen integer;
begin
t_now := sysdate;
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob );
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob, true );
end if;
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0304' ) -- Local file header signature
, hextoraw( '1400' ) -- version 2.0
, hextoraw( '0000' ) -- no General purpose bits
, hextoraw( '0800' ) -- deflate
, little_endian( to_number( to_char( t_now, 'ss' ) ) / 2
+ to_number( to_char( t_now, 'mi' ) ) * 32
+ to_number( to_char( t_now, 'hh24' ) ) * 2048
, 2
) -- File last modification time
, little_endian( to_number( to_char( t_now, 'dd' ) )
+ to_number( to_char( t_now, 'mm' ) ) * 32
+ ( to_number( to_char( t_now, 'yyyy' ) ) - 1980 ) * 512
, 2
) -- File last modification date
, dbms_lob.substr( t_blob, 4, t_clen - 7 ) -- CRC-32
, little_endian( t_clen - 18 ) -- compressed size
, little_endian( dbms_lob.getlength( p_content ) ) -- uncompressed size
, little_endian( length( p_name ), 2 ) -- File name length
, hextoraw( '0000' ) -- Extra field length
, utl_raw.cast_to_raw( p_name ) -- File name
)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
);
dbms_lob.append( p_zipped_blob, dbms_lob.substr( t_blob, t_clen - 18, 11 ) ); -- compressed content
dbms_lob.freetemporary( t_blob );
end;
--
procedure finish_zip( p_zipped_blob in out blob )
is
t_cnt pls_integer := 0;
t_offs integer;
t_offs_dir_header integer;
t_offs_end_header integer;
t_comment raw(32767) := utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
begin
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := dbms_lob.instr( p_zipped_blob, hextoraw( '504B0304' ), 1 );
while t_offs > 0
loop
t_cnt := t_cnt + 1;
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0102' ) -- Central directory file header signature
, hextoraw( '1400' ) -- version 2.0
, dbms_lob.substr( p_zipped_blob, 26, t_offs + 4 )
, hextoraw( '0000' ) -- File comment length
, hextoraw( '0000' ) -- Disk number where file starts
, hextoraw( '0100' ) -- Internal file attributes
, hextoraw( '2000B681' ) -- External file attributes
, little_endian( t_offs - 1 ) -- Relative offset of local file header
, dbms_lob.substr( p_zipped_blob
, utl_raw.cast_to_binary_integer( dbms_lob.substr( p_zipped_blob, 2, t_offs + 26 ), utl_raw.little_endian )
, t_offs + 30
) -- File name
)
);
t_offs := dbms_lob.instr( p_zipped_blob, hextoraw( '504B0304' ), t_offs + 32 );
end loop;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0506' ) -- End of central directory signature
, hextoraw( '0000' ) -- Number of this disk
, hextoraw( '0000' ) -- Disk where central directory starts
, little_endian( t_cnt, 2 ) -- Number of central directory records on this disk
, little_endian( t_cnt, 2 ) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header ) -- Size of central directory
, little_endian( t_offs_dir_header ) -- Relative offset of local file header
, little_endian( nvl( utl_raw.length( t_comment ), 0 ), 2 ) -- ZIP file comment length
, t_comment
)
);
end;
--
procedure save_zip
( p_zipped_blob in blob
, p_dir in varchar2
, p_filename in varchar2
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir, p_filename, 'wb' );
for i in 0 .. trunc( ( dbms_lob.getlength( p_zipped_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh, dbms_lob.substr( p_zipped_blob, t_len, i * t_len + 1 ) );
end loop;
utl_file.fclose( t_fh );
end;
END zip_blobs;My code:
DECLARE
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
b_dl_file1 BLOB;
b_dl_file2 BLOB;
b_dl_file3 BLOB;
b_zipped_blob BLOB;
BEGIN
select FILE_BLOB
into b_dl_file1
from FILES
where FILE_ID = 64;
select FILE_BLOB
into b_dl_file2
from FILES
where FILE_ID = 65;
select FILE_BLOB
into b_dl_file3
from FILES
where FILE_ID = 66;
zip_blobs.add1file(b_zipped_blob, 'test1.bin', b_dl_file1);
zip_blobs.add1file(b_zipped_blob, 'test2.bin', b_dl_file2);
zip_blobs.add1file(b_zipped_blob, 'test3.bin', b_dl_file3);
zip_blobs.finish_zip(b_zipped_blob);
insert into compressed_files(compressed_blob) values(b_zipped_blob);
END;So I'm inserting it into a table, but even when I use save_zip, I get the same error. I tried playing around with freeing p_zipped_blob and not freeing t_blob, but it didn't seem to work, and I'm
Thanks!
It's just some code I had lying around. See http://technology.amis.nl/blog/8090/parsing-a-microsoft-word-docx-and-unzip-zipfiles-with-plsql for some unzipping code
Anton
Thanks again Anton, I got it working. It looks like certain file sizes break the add1files procedure, I'm not sure why, but I have more than enough information to debug it. This was a HUGE help!
I can save you some time: I just found out that dbms_lob.substr returns a raw, not a blob. And that has a max size of 32767 bytes. So the procedure failed for the large (compressed) blobs.
A simple solution change the line
- dbms_lob.append( p_zipped_blob, dbms_lob.substr( t_blob, t_clen - 18, 11 ) ); -- compressed content to
dbms_lob.copy( p_zipped_blob, t_blob, t_clen - 18, dbms_lob.getlength( p_zipped_blob ) + 1, 11 ); -- compressed contentSo I learn something new about PL/SQL every day :)
Thanks! I had come to the same conclusion, and was writing a loop that would break the blob into 32K chunks, and append them one by one, but that line worked perfectly. Lucky I checked back here!
Big thanks to Anton. This is a great package and has proved very helpful and useful.
Morning
I am running apex 4.1 on Oracle 11gR2 on a Red Hat Linux Server.
I hate to beat a dead horse, but I found your code for zipping multiple files and I am tring to use it but I am having problems with the finish_zip procedure. I never get out of the loop.
I see where you are setting the t_offs as the location of ''504B03040' to start, but I am not sure about what is happening at the end of the loop, I am not sure if I would ever get a 0 or less at the
I put a output line for testing and I alwas get the compressed file size plus one which is the t_offs from the first test.
Merph
Evening,
thats strange. My first guess would be that you have been modifying the code, my second quess would be that you are calling finish_zip without calling add1file before, and my last guess would be that
But I don't know what you are doing, so it's just guessing.
Anton
Hello Shan,
does the linked package in your previous posting not work?
Re: ZIp File using utlzip procedure
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Regards
Marcus
Hi Marwin,
I used that package for ziping my files. That is working fine with me. i cant find the code for unzip.
Can you give me a link for unzip the zip folder???
It will be very helpful for me.
Cheers,
Shan
Hello Shan,
the posting contains a link to zip and a link to unzip.
Regards
Marcus
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
begin
as_zip.add1file( g_zipped_blob, 'test1.txt', utl_raw.cast_to_raw( 'Dit is de laatste test! Waarom wordt dit dan niet gecomprimeerd?' ) );
as_zip.add1file( g_zipped_blob, 'test1234.txt', utl_raw.cast_to_raw( 'En hier staat wat anders' ) );
as_zip.finish_zip( g_zipped_blob );
as_zip.save_zip( g_zipped_blob, 'MY_DIR', 'my.zip' );
end;
--
declare
t_dir varchar2(100) := 'MY_DIR';
t_zip varchar2(100) := 'my.zip';
zip_files as_zip.file_list;
begin
zip_files := as_zip.get_file_list( t_dir, t_zip );
for i in zip_files.first() .. zip_files.last
loop
dbms_output.put_line( zip_files( i ) );
dbms_output.put_line( utl_raw.cast_to_varchar2( as_zip.get_file( t_dir, t_zip, zip_files( i ) ) ) );
end loop;
end;
*/
end;
/
CREATE OR REPLACE package body as_zip
is
--
function raw2num(
p_value in raw
)
return number
is
begin -- note: FFFFFFFF => -1
return utl_raw.cast_to_binary_integer( p_value
, utl_raw.little_endian
);
end;
--
function file2blob(
p_dir in varchar2
, p_file_name in varchar2
)
return blob
is
file_lob bfile;
file_blob blob;
begin
file_lob := bfilename( p_dir
, p_file_name
);
dbms_lob.open( file_lob
, dbms_lob.file_readonly
);
dbms_lob.createtemporary( file_blob
, true
);
dbms_lob.loadfromfile( file_blob
, file_lob
, dbms_lob.lobmaxsize
);
dbms_lob.close( file_lob );
return file_blob;
exception
when others
then
if dbms_lob.isopen( file_lob ) = 1
then
dbms_lob.close( file_lob );
end if;
if dbms_lob.istemporary( file_blob ) = 1
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
then
dbms_lob.freetemporary( file_blob );
end if;
raise;
end;
--
function raw2varchar2(
p_raw in raw
, p_encoding in varchar2
)
return varchar2
is
begin
return nvl
( utl_i18n.raw_to_char( p_raw
, p_encoding
)
, utl_i18n.raw_to_char
( p_raw
, utl_i18n.map_charset( p_encoding
, utl_i18n.generic_context
, utl_i18n.iana_to_oracle
)
)
);
end;
function get_file_list(
p_dir in varchar2
, p_zip_file in varchar2
, p_encoding in varchar2 := null
)
return file_list
is
begin
return get_file_list( file2blob( p_dir
, p_zip_file
)
, p_encoding
);
end;
--
function get_file_list(
p_zipped_blob in blob
, p_encoding in varchar2 := null
)
return file_list
is
t_ind integer;
t_hd_ind integer;
t_rv file_list;
begin
t_ind := dbms_lob.getlength( p_zipped_blob ) - 21;
loop
exit when dbms_lob.substr( p_zipped_blob
, 4
, t_ind
) = hextoraw( '504B0506' )
or t_ind < 1;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := raw2num( dbms_lob.substr( p_zipped_blob
, 4
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
, t_ind + 16
) ) + 1;
t_rv := file_list( );
t_rv.extend( raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_ind + 10
) ) );
for i in 1 .. raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_ind + 8
) )
loop
t_rv( i ) :=
raw2varchar2
( dbms_lob.substr( p_zipped_blob
, raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 28
) )
, t_hd_ind + 46
)
, p_encoding
);
t_hd_ind :=
t_hd_ind
+ 46
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 28
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 30
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 32
) );
end loop;
--
return t_rv;
end;
--
function get_file(
p_dir in varchar2
, p_zip_file in varchar2
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob
is
begin
return get_file( file2blob( p_dir
, p_zip_file
)
, p_file_name
, p_encoding
);
end;
--
function get_file(
p_zipped_blob in blob
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob
is
t_tmp blob;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
t_ind integer;
t_hd_ind integer;
t_fl_ind integer;
begin
t_ind := dbms_lob.getlength( p_zipped_blob ) - 21;
loop
exit when dbms_lob.substr( p_zipped_blob
, 4
, t_ind
) = hextoraw( '504B0506' )
or t_ind < 1;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := raw2num( dbms_lob.substr( p_zipped_blob
, 4
, t_ind + 16
) ) + 1;
for i in 1 .. raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_ind + 8
) )
loop
if p_file_name =
raw2varchar2
( dbms_lob.substr( p_zipped_blob
, raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 28
) )
, t_hd_ind + 46
)
, p_encoding
)
then
if dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 10
) = hextoraw( '0800' ) -- deflate
then
t_fl_ind :=
raw2num( dbms_lob.substr( p_zipped_blob
, 4
, t_hd_ind + 42
) );
t_tmp := hextoraw( '1F8B0800000000000003' ); -- gzip header
dbms_lob.copy( t_tmp
, p_zipped_blob
, raw2num( dbms_lob.substr( p_zipped_blob
, 4
, t_fl_ind + 19
) )
, 11
, t_fl_ind
+ 31
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_fl_ind + 27
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_fl_ind + 29
) )
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
);
dbms_lob.append( t_tmp
, dbms_lob.substr( p_zipped_blob
, 4
, t_fl_ind + 15
)
);
dbms_lob.append( t_tmp
, dbms_lob.substr( p_zipped_blob, 4, t_fl_ind + 23 )
);
return utl_compress.lz_uncompress( t_tmp );
end if;
--
if dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 10
) =
hextoraw( '0000' )
-- The file is stored (no compression)
then
t_fl_ind :=
raw2num( dbms_lob.substr( p_zipped_blob
, 4
, t_hd_ind + 42
) );
return dbms_lob.substr( p_zipped_blob
, raw2num( dbms_lob.substr( p_zipped_blob
, 4
, t_fl_ind + 19
) )
, t_fl_ind
+ 31
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_fl_ind + 27
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_fl_ind + 29
) )
);
end if;
end if;
t_hd_ind :=
t_hd_ind
+ 46
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 28
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 30
) )
+ raw2num( dbms_lob.substr( p_zipped_blob
, 2
, t_hd_ind + 32
) );
end loop;
--
return null;
end;
--
function little_endian(
p_big in number
, p_bytes in pls_integer := 4
)
return raw
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
is
begin
return utl_raw.substr
( utl_raw.cast_from_binary_integer( p_big
, utl_raw.little_endian
)
, 1
, p_bytes
);
end;
--
procedure add1file(
p_zipped_blob in out blob
, p_name in varchar2
, p_content in blob
)
is
t_now date;
t_blob blob;
t_clen integer;
begin
t_now := sysdate;
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob );
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob
, true
);
end if;
dbms_lob.append
( p_zipped_blob
, utl_raw.concat
( hextoraw( '504B0304' ) -- Local file header signature
, hextoraw( '1400' ) -- version 2.0
, hextoraw( '0000' ) -- no General purpose bits
, hextoraw( '0800' ) -- deflate
, little_endian
( to_number( to_char( t_now
, 'ss'
) ) / 2
+ to_number( to_char( t_now
, 'mi'
) ) * 32
+ to_number( to_char( t_now
, 'hh24'
) ) * 2048
, 2
) -- File last modification time
, little_endian
( to_number( to_char( t_now
, 'dd'
) )
+ to_number( to_char( t_now
, 'mm'
) ) * 32
+ ( to_number( to_char( t_now
, 'yyyy'
) ) - 1980 ) * 512
, 2
) -- File last modification date
, dbms_lob.substr( t_blob
, 4
, t_clen - 7
) -- CRC-32
, little_endian( t_clen - 18 ) -- compressed size
, little_endian( dbms_lob.getlength( p_content ) )
-- uncompressed size
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
, little_endian( length( p_name )
, 2
) -- File name length
, hextoraw( '0000' ) -- Extra field length
, utl_raw.cast_to_raw( p_name ) -- File name
)
);
dbms_lob.copy( p_zipped_blob
, t_blob
, t_clen - 18
, dbms_lob.getlength( p_zipped_blob ) + 1
, 11
); -- compressed content
dbms_lob.freetemporary( t_blob );
end;
--
procedure finish_zip(
p_zipped_blob in out blob
)
is
t_cnt pls_integer := 0;
t_offs integer;
t_offs_dir_header integer;
t_offs_end_header integer;
t_comment raw( 32767 )
:= utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
begin
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := dbms_lob.instr( p_zipped_blob
, hextoraw( '504B0304' )
, 1
);
while t_offs > 0
loop
t_cnt := t_cnt + 1;
dbms_lob.append
( p_zipped_blob
, utl_raw.concat
( hextoraw( '504B0102' )
-- Central directory file header signature
, hextoraw( '1400' ) -- version 2.0
, dbms_lob.substr( p_zipped_blob
, 26
, t_offs + 4
)
, hextoraw( '0000' ) -- File comment length
, hextoraw( '0000' ) -- Disk number where file starts
, hextoraw( '0100' ) -- Internal file attributes
, hextoraw( '2000B681' ) -- External file attributes
, little_endian( t_offs - 1 )
-- Relative offset of local file header
, dbms_lob.substr
( p_zipped_blob
, utl_raw.cast_to_binary_integer
( dbms_lob.substr( p_zipped_blob
, 2
, t_offs + 26
)
, utl_raw.little_endian
)
, t_offs + 30
) -- File name
)
);
t_offs :=
dbms_lob.instr( p_zipped_blob
, hextoraw( '504B0304' )
, t_offs + 32
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
);
end loop;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append
( p_zipped_blob
, utl_raw.concat
( hextoraw( '504B0506' ) -- End of central directory signature
, hextoraw( '0000' ) -- Number of this disk
, hextoraw( '0000' ) -- Disk where central directory starts
, little_endian
( t_cnt
, 2
) -- Number of central directory records on this disk
, little_endian( t_cnt
, 2
) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header )
-- Size of central directory
, little_endian
( t_offs_dir_header )
-- Relative offset of local file header
, little_endian
( nvl( utl_raw.length( t_comment )
, 0
)
, 2
) -- ZIP file comment length
, t_comment
)
);
end;
--
procedure save_zip(
p_zipped_blob in blob
, p_dir in varchar2 := 'MY_DIR'
, p_filename in varchar2 := 'my.zip'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir
, p_filename
, 'wb'
);
for i in 0 .. trunc( ( dbms_lob.getlength( p_zipped_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh
, dbms_lob.substr( p_zipped_blob
, t_len
, i * t_len + 1
)
);
end loop;
utl_file.fclose( t_fh );
end;
--
end;
/
Hi Martin.
Ya i saw that unzip link with contains as_zip package. When i pass my directory name and zip file name i am not getting the unzip file
create or replace
package as_zip
is
type file_list is table of clob;
--
function get_file_list
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
( p_dir in varchar2
, p_zip_file in varchar2
, p_encoding in varchar2 := null
)
return file_list;
--
function get_file_list
( p_zipped_blob in blob
, p_encoding in varchar2 := null
)
return file_list;
--
function get_file
( p_dir in varchar2
, p_zip_file in varchar2
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob;
--
function get_file
( p_zipped_blob in blob
, p_file_name in varchar2
, p_encoding in varchar2 := null
)
return blob;
--
procedure add1file
( p_zipped_blob in out blob
, p_name in varchar2
, p_content in blob
);
--
procedure finish_zip( p_zipped_blob in out blob );
--
procedure save_zip
( p_zipped_blob in blob
, p_dir in varchar2 := 'MY_DIR'
, p_filename in varchar2 := 'my.zip'
);
--
/*
declare
g_zipped_blob blob;
begin
as_zip.add1file( g_zipped_blob, 'test1.txt', utl_raw.cast_to_raw( 'Dit is de laatste test! Waarom wordt dit dan niet gecomprimeerd?' ) );
as_zip.add1file( g_zipped_blob, 'test1234.txt', utl_raw.cast_to_raw( 'En hier staat wat anders' ) );
as_zip.finish_zip( g_zipped_blob );
as_zip.save_zip( g_zipped_blob, 'MY_DIR', 'my.zip' );
end;
--
declare
zip_files as_zip.file_list;
begin
zip_files := as_zip.get_file_list( 'MY_DIR', 'my.zip' );
for i in zip_files.first() .. zip_files.last
loop
dbms_output.put_line( zip_files( i ) );
dbms_output.put_line( utl_raw.cast_to_varchar2( as_zip.get_file( 'MY_DIR', 'my.zip', zip_files( i ) ) ) );
end loop;
end;
*/
end;create or replace
package body as_zip
is
--
function raw2num( p_value in raw )
return number
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
is
begin -- note: FFFFFFFF => -1
return utl_raw.cast_to_binary_integer( p_value, utl_raw.little_endian );
end;
--
function file2blob( p_dir in varchar2, p_file_name in varchar2 )
return blob
is
file_lob bfile;
file_blob blob;
begin
dbms_output.put_line(p_dir||p_file_name);
dbms_output.put_line('entering');
file_lob := bfilename( p_dir, p_file_name );
dbms_lob.open( file_lob, dbms_lob.file_readonly );
dbms_lob.createtemporary( file_blob, true );
dbms_lob.loadfromfile( file_blob, file_lob, dbms_lob.lobmaxsize );
dbms_lob.close( file_lob );
return file_blob;
exception
when others then
if dbms_lob.isopen( file_lob ) = 1
then
dbms_lob.close( file_lob );
end if;
if dbms_lob.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
{
byte[] buffer = new byte[4096];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
----------------------------------------------------------------
create or replace
PACKAGE "SSSL_FILE_ZIP"
IS
PROCEDURE compressFile (p_in_file IN VARCHAR2, p_out_file IN VARCHAR2)
AS
LANGUAGE JAVA
NAME 'UTLZip.compressFile(java.lang.String,
java.lang.String)';
END;
Thanks in Advance
San,
San wrote:
i Have referred some code to create a zip file. Its working fine. Now i want to add the password protection in that zip file. Can anyone help me out to achieve this???Does not seem architecturally sou
PL/SQL is a database server language. It is abstracted from the operating system. It deals with the database environment and database objects. The only times that PL/SQL typically steps outside these
Your code uses PL/SQL to wrap Java code to access the o/s to zip external files - an action very specific to external processing and not related in any way to database objects and the PL/SQL language.
The are boundaries in languages - defined by the purpose of that language and the runtime and execution environment of that language. Using PL/SQL to zip files (via Java via punching holes in the Java
Nah, it's not that rare Billy, we have to do something similar... generating data in XML files which are then zipped up and shipped over FTP to those who need it who them load it up into their databas
;)
>
Now i want to add the password protection in that zip file
>
What do you mean by that exactly?
If you mean create a password-protected zip file that can be opened by Winzip then you won't be able to do that.
If you mean you want encrypt the entire zip file so that a password is needed to unencrypt the entire zip file then that is possible. The unencryption would then be done using another Java procedure.
If you mean you want to encrypt the contents of each zip entry individually using your own encryption then you can do that using Java functionality. But you will then need to unencrypt each entry usin
Provide a step-by-step example of how you plan to produce the zip file, get it a user and how you plan to have the user use the zip file (Winzip, Java, etc).
rp0428 wrote:
>
If you mean create a password-protected zip file that can be opened by Winzip then you won't be able to do that.
>Winzip supports at least "traditional PKWARE encryption" and it's own AES-format, documented at http://www.winzip.com/win/en/aes_info.htm
It's very well possible to create a password-protected zip file which can be opened by Winzip
>
Winzip supports at least "traditional PKWARE encryption" and it's own AES-format, documented at http://www.winzip.com/win/en/aes_info.htm
It's very well possible to create a password-protected zip file which can be opened by Winzip
>
It isn't possible using standard Java provided functionality: that is, the java.util.zip package. That package can neither read nor write encrypted zip files produced by any manufacturer.
The package has NO support for encryption for zip entries or the central directory.
Aaah, but I didn't know that (standard) java a requirement was. Else you can use for instance http://code.google.com/p/winzipaes
Or giive me a couple days and I could extend as_zip and do it in plsql.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
I have no Winzip at hand, but this works with WinRar and 7-Zip
CREATE OR REPLACE package as_zip3
is
--
function file2blob
( p_dir varchar2
, p_file_name varchar2
)
return blob;
--
procedure add1file
( p_zipped_blob in out blob
, p_name varchar2
, p_content blob
, p_password varchar2 := null
);
--
procedure finish_zip( p_zipped_blob in out blob );
--
procedure save_zip
( p_zipped_blob blob
, p_dir varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.zip'
);
--
end;
/
CREATE OR REPLACE package body as_zip3
is
--
c_LOCAL_FILE_HEADER constant raw(4) := hextoraw( '504B0304' ); -- Local file header signature
c_END_OF_CENTRAL_DIRECTORY constant raw(4) := hextoraw( '504B0506' ); -- End of central directory signature
--
function little_endian( p_big number, p_bytes pls_integer := 4 )
return raw
is
begin
return utl_raw.substr( utl_raw.cast_from_binary_integer( p_big, utl_raw.little_endian ), 1, p_bytes );
end;
--
function blob2num( p_blob blob, p_len integer, p_pos integer )
return number
is
begin
return utl_raw.cast_to_binary_integer( dbms_lob.substr( p_blob, p_len, p_pos ), utl_raw.little_endian );
end;
--
function file2blob
( p_dir varchar2
, p_file_name varchar2
)
return blob
is
file_lob bfile;
file_blob blob;
begin
file_lob := bfilename( p_dir, p_file_name );
dbms_lob.open( file_lob, dbms_lob.file_readonly );
dbms_lob.createtemporary( file_blob, true );
dbms_lob.loadfromfile( file_blob, file_lob, dbms_lob.lobmaxsize );
dbms_lob.close( file_lob );
return file_blob;
exception
when others then
if dbms_lob.isopen( file_lob ) = 1
then
dbms_lob.close( file_lob );
end if;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if dbms_lob.istemporary( file_blob ) = 1
then
dbms_lob.freetemporary( file_blob );
end if;
raise;
end;
--
function encrypt( p_pw varchar2, p_src blob )
return blob
is
t_salt raw(16);
t_key raw(32);
t_pw raw(32767) := utl_raw.cast_to_raw( p_pw );
t_key_bits pls_integer := 256;
t_key_length pls_integer := t_key_bits / 8 * 2 + 2;
t_cnt pls_integer := 1000;
t_keys raw(32767);
t_sum raw(32767);
t_mac raw(20);
t_iv raw(16);
t_block raw(16);
t_len pls_integer;
t_rv blob;
begin
t_salt := dbms_crypto.randombytes( t_key_bits / 16 );
for i in 1 .. ceil( t_key_length / 20 )
loop
t_mac := dbms_crypto.mac( utl_raw.concat( t_salt, to_char( i, 'fm0xxxxxxx' ) ), dbms_crypto.HMAC_SH1, t_pw );
t_sum := t_mac;
for j in 1 .. t_cnt - 1
loop
t_mac := dbms_crypto.mac( t_mac, dbms_crypto.HMAC_SH1, t_pw );
t_sum := utl_raw.bit_xor( t_mac, t_sum );
end loop;
t_keys := utl_raw.concat( t_keys, t_sum );
end loop;
t_keys := utl_raw.substr( t_keys, 1, t_key_length );
t_key := utl_raw.substr( t_keys, 1, t_key_bits / 8 );
t_rv := utl_raw.concat( t_salt, utl_raw.substr( t_keys, -2, 2 ) );
--
for i in 0 .. trunc( ( dbms_lob.getlength( p_src ) - 1 ) / 16 )
loop
t_block := dbms_lob.substr( p_src, 16, i * 16 + 1 );
t_len := utl_raw.length( t_block );
if t_len < 16
then
t_block := utl_raw.concat( t_block, utl_raw.copies( '00', 16 - t_len ) );
end if;
t_iv := utl_raw.reverse( to_char( i + 1, 'fm000000000000000000000000000000x' ) );
dbms_lob.writeappend( t_rv, t_len, dbms_crypto.encrypt( t_block, dbms_crypto.ENCRYPT_AES256 + dbms_crypto.CHAIN_CFB + dbms_crypto.PAD_NONE, t_key, t_iv ) );
end loop;
--
t_mac := dbms_crypto.mac( dbms_lob.substr( t_rv, dbms_lob.getlength( p_src ), t_key_bits / 16 + 2 + 1 ), dbms_crypto.HMAC_SH1, utl_raw.substr( t_keys, 1 + t_key_bits / 8, t_key_bits / 8 ) );
dbms_lob.writeappend( t_rv, 10, t_mac );
return t_rv;
end;
--
procedure add1file
( p_zipped_blob in out blob
, p_name varchar2
, p_content blob
, p_password varchar2 := null
)
is
t_now date;
t_blob blob;
t_len integer;
t_clen integer;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
t_crc32 raw(4) := hextoraw( '00000000' );
t_compressed boolean := false;
t_encrypted boolean := false;
t_name raw(32767);
t_extra raw(11);
begin
t_now := sysdate;
t_len := nvl( dbms_lob.getlength( p_content ), 0 );
if t_len > 0
then
dbms_lob.createtemporary( t_blob, true );
dbms_lob.copy( t_blob, utl_compress.lz_compress( p_content ), dbms_lob.lobmaxsize , 1, 11 );
t_clen := dbms_lob.getlength( t_blob ) - 8;
t_compressed := t_clen < t_len;
t_crc32 := dbms_lob.substr( t_blob, 4, t_clen + 1 );
dbms_lob.trim( t_blob, t_clen );
end if;
if not t_compressed
then
t_clen := t_len;
t_blob := p_content;
end if;
--
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob, true );
end if;
--
if p_password is not null and t_len > 0
then
t_encrypted := true;
t_crc32 := hextoraw( '00000000' );
t_extra := hextoraw( '019907000200414503' || case when t_compressed
then '0800' -- deflate
else '0000' -- stored
end
);
t_blob := encrypt( p_password, t_blob );
t_clen := dbms_lob.getlength( t_blob );
end if;
t_name := utl_i18n.string_to_raw( p_name, 'AL32UTF8' );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( utl_raw.concat( c_LOCAL_FILE_HEADER -- Local file header signature
, hextoraw( '3300' ) -- version 5.1
)
, case when t_encrypted
then hextoraw( '01' ) -- encrypted
else hextoraw( '00' )
end
, case when t_name = utl_i18n.string_to_raw( p_name, 'US8PC437' )
then hextoraw( '00' )
else hextoraw( '08' ) -- set Language encoding flag (EFS)
end
, case when t_encrypted
then '6300'
else
case when t_compressed
then hextoraw( '0800' ) -- deflate
else hextoraw( '0000' ) -- stored
end
end
, little_endian( to_number( to_char( t_now, 'ss' ) ) / 2
+ to_number( to_char( t_now, 'mi' ) ) * 32
+ to_number( to_char( t_now, 'hh24' ) ) * 2048
, 2
) -- File last modification time
, little_endian( to_number( to_char( t_now, 'dd' ) )
+ to_number( to_char( t_now, 'mm' ) ) * 32
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
+ ( to_number( to_char( t_now, 'yyyy' ) ) - 1980 ) * 512
, 2
) -- File last modification date
, t_crc32 -- CRC-32
, little_endian( t_clen ) -- compressed size
, little_endian( t_len ) -- uncompressed size
, little_endian( utl_raw.length( t_name ), 2 ) -- File name length
, little_endian( nvl( utl_raw.length( t_extra ), 0 ), 2 ) -- Extra field length
, utl_raw.concat( t_name -- File name
, t_extra
)
)
);
if t_len > 0
then
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 1 ); -- (compressed) content
end if;
dbms_lob.freetemporary( t_blob );
end;
--
procedure finish_zip( p_zipped_blob in out blob )
is
t_cnt pls_integer := 0;
t_offs integer;
t_offs_dir_header integer;
t_offs_end_header integer;
t_comment raw(32767) := utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
t_len pls_integer;
begin
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := 1;
while dbms_lob.substr( p_zipped_blob, utl_raw.length( c_LOCAL_FILE_HEADER ), t_offs ) = c_LOCAL_FILE_HEADER
loop
t_cnt := t_cnt + 1;
t_len := blob2num( p_zipped_blob, 2, t_offs + 28 );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( hextoraw( '504B0102' ) -- Central directory file header signature
, hextoraw( '3F00' ) -- version 6.3
, dbms_lob.substr( p_zipped_blob, 26, t_offs + 4 )
, hextoraw( '0000' ) -- File comment length
, hextoraw( '0000' ) -- Disk number where file starts
, hextoraw( '0000' ) -- Internal file attributes =>
-- 0000 binary file
-- 0100 (ascii)text file
, case
when dbms_lob.substr( p_zipped_blob
, 1
, t_offs + 30 + blob2num( p_zipped_blob, 2, t_offs + 26 ) - 1
) in ( hextoraw( '2F' ) -- /
, hextoraw( '5C' ) -- \
)
then hextoraw( '10000000' ) -- a directory/folder
else hextoraw( '2000B681' ) -- a file
end -- External file attributes
, little_endian( t_offs - 1 ) -- Relative offset of local file header
, dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_offs + 26 ) + t_len
, t_offs + 30
) -- File name + extra data field
)
);
t_offs := t_offs + 30 + blob2num( p_zipped_blob, 4, t_offs + 18 ) -- compressed size
+ blob2num( p_zipped_blob, 2, t_offs + 26 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_offs + 28 ); -- Extra field length
end loop;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( c_END_OF_CENTRAL_DIRECTORY -- End of central directory signature
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
, hextoraw( '0000' ) -- Number of this disk
, hextoraw( '0000' ) -- Disk where central directory starts
, little_endian( t_cnt, 2 ) -- Number of central directory records on this disk
, little_endian( t_cnt, 2 ) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header ) -- Size of central directory
, little_endian( t_offs_dir_header ) -- Offset of start of central directory, relative to start of archive
, little_endian( nvl( utl_raw.length( t_comment ), 0 ), 2 ) -- ZIP file comment length
, t_comment
)
);
end;
--
procedure save_zip
( p_zipped_blob blob
, p_dir varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.zip'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir, p_filename, 'wb' );
for i in 0 .. trunc( ( dbms_lob.getlength( p_zipped_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh, dbms_lob.substr( p_zipped_blob, t_len, i * t_len + 1 ) );
end loop;
utl_file.fclose( t_fh );
end;
--
end;
/
Hi,
Thanks For your reply.
I have created the mentioned package.
Now i have directory and file name .
I call file2blob function .
when i pass the directory and file name it has to create a zip file with passowrd protection.
How to achieve this??
declare
t_zip blob;
begin
as_zip3.add1file( t_zip, 'my_file.txt', as_zip3.file2blob( 'MY_DIR', 'my_file.txt' ), 'secret' );
as_zip3.finish_zip( t_zip );
as_zip3.save_zip( t_zip, 'MY_DIR', 'my_file.zip' );
dbms_lob.freetemporary( t_zip );
end;
/
Hi,
Thanks I have execute the below query but i am getting error stats that
Error starting at line 1 in command:
declare
t_zip blob;
begin
as_zip3.add1file( t_zip, 'NEFT-OCT2012-ARN-62508.dbf', as_zip3.file2blob( 'BROKERAGE', 'NEFT-OCT2012-ARN-62508.dbf' ), 'secret' );
AS_ZIP3.FINISH_ZIP( T_ZIP );
as_zip3.save_zip( t_zip, 'BROKERAGE', 'NEFT-OCT2012-ARN-62508.zip' );
dbms_lob.freetemporary( t_zip );
end;
Error report:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.DBMS_LOB", line 826
ORA-06512: at "SMF_APEXRPS.AS_ZIP3", line 95
ORA-06512: at "SMF_APEXRPS.AS_ZIP3", line 147
ORA-06512: at line 4
06502. 00000 - "PL/SQL: numeric or value error%s"
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
*Cause:
*Action:code which i executed is
declare
t_zip blob;
begin
as_zip3.add1file( t_zip, 'NEFT-OCT2012-ARN-62508.dbf', as_zip3.file2blob( 'BROKERAGE', 'NEFT-OCT2012-ARN-62508.dbf' ), 'secret' );
AS_ZIP3.FINISH_ZIP( T_ZIP );
as_zip3.save_zip( t_zip, 'BROKERAGE', 'NEFT-OCT2012-ARN-62508.zip' );
dbms_lob.freetemporary( t_zip );
end;
/ Thnaks In advance.
I didn't tested the code with large files, it was just a quick hack.
Try changing the encrypt function with this
function encrypt( p_pw varchar2, p_src blob )
return blob
is
t_salt raw(16);
t_key raw(32);
t_pw raw(32767) := utl_raw.cast_to_raw( p_pw );
t_key_bits pls_integer := 256;
t_key_length pls_integer := t_key_bits / 8 * 2 + 2;
t_cnt pls_integer := 1000;
t_keys raw(32767);
t_sum raw(32767);
t_mac raw(20);
t_iv raw(16);
t_block raw(16);
t_len pls_integer;
t_rv blob;
t_tmp blob;
begin
t_salt := dbms_crypto.randombytes( t_key_bits / 16 );
for i in 1 .. ceil( t_key_length / 20 )
loop
t_mac := dbms_crypto.mac( utl_raw.concat( t_salt, to_char( i, 'fm0xxxxxxx' ) ), dbms_crypto.HMAC_SH1, t_pw );
t_sum := t_mac;
for j in 1 .. t_cnt - 1
loop
t_mac := dbms_crypto.mac( t_mac, dbms_crypto.HMAC_SH1, t_pw );
t_sum := utl_raw.bit_xor( t_mac, t_sum );
end loop;
t_keys := utl_raw.concat( t_keys, t_sum );
end loop;
t_keys := utl_raw.substr( t_keys, 1, t_key_length );
t_key := utl_raw.substr( t_keys, 1, t_key_bits / 8 );
t_rv := utl_raw.concat( t_salt, utl_raw.substr( t_keys, -2, 2 ) );
--
for i in 0 .. trunc( ( dbms_lob.getlength( p_src ) - 1 ) / 16 )
loop
t_block := dbms_lob.substr( p_src, 16, i * 16 + 1 );
t_len := utl_raw.length( t_block );
if t_len < 16
then
t_block := utl_raw.concat( t_block, utl_raw.copies( '00', 16 - t_len ) );
end if;
t_iv := utl_raw.reverse( to_char( i + 1, 'fm000000000000000000000000000000x' ) );
dbms_lob.writeappend( t_rv, t_len, dbms_crypto.encrypt( t_block, dbms_crypto.ENCRYPT_AES256 + dbms_crypto.CHAIN_CFB + dbms_crypto.PAD_NONE, t_key, t_iv ) );
end loop;
--
dbms_lob.createtemporary( t_tmp, true );
dbms_lob.copy( t_tmp, t_rv, dbms_lob.getlength( p_src ), 1, t_key_bits / 16 + 2 + 1 );
t_mac := dbms_crypto.mac( t_tmp, dbms_crypto.HMAC_SH1, utl_raw.substr( t_keys, 1 + t_key_bits / 8, t_key_bits / 8 ) );
dbms_lob.writeappend( t_rv, 10, t_mac );
dbms_lob.freetemporary( t_tmp );
return t_rv;
end;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
How to Decrypt xlsx files
Hi, I am looking for a solution that someone may have, regarding decripting xlsx files.Maybe Anton Scheffer has an idea? When an xlsx is workbook protected, and you create it with, or edit/save with.
Well xls and xlsx files are binary files... meaning they are just a bunch of bytes.If they are protected workbooks, then usually someone has used a password to encrypt the contents. If you haven't go
BluShadow... Well.. I need a pl/sql procedure to decrypt the "encryptedpackage" on a xlsx file, of which the signature tests as if it were an xls file.I have the password, the workbook is just protect
So, how are you currently reading the contents of this excel file? Are you using Oracle heterogenous services? or is it actually in the database as you seem to be suggesting and you're reading it by
I cannot say that I have tried this or even heard of anyone using it, but, did you know you can write Oracle stored procedures in C#? http://docs.oracle.com/cd/E11882_01/appdev.112/e10767/procedures_
BluShadow wrote:
So, how are you currently reading the contents of this excel file? Are you using Oracle heterogenous services? or is it actually in the database as you seem to be suggesting and you're reading it by
The files are being uploaded into the DB.To read the contents... I am using one of Anton Scheffer solutions.But the problem arises when the uploaded excel file is workbook-protected. If the file is n
ascheffer wrote:
You might find some information in the Microsoft documentation https://msdn.microsoft.com/en-us/library/cc313071(v=office.12).aspx
Thank you Anton.I do have the file and I have read it, but given the times I have to finish this task, I need something more clear on the subject, or maybe knowing that part of the oracle tools provid
The solution could be java stored procedure with Apache POI (https://poi.apache.org/)
I you want to do stuff like decrypting xlsx you do need knowledge of the low-level file structure of the files, and how to handle that in plsql.
Using POI directly was also my first idea, as I'm kinda quite related to it ... but it seems, that it is not so easy to include java libs [1] [2] into Oracle.If either calling an external java program
Jaydarp-Oracle schrieb:
... and when I read as xlsx find the "portions" of the file that match an "EncryptedPackage", I get the stream, but... I am missing a way to decrypt that stream with the appropiate password, so I can
So you found a way to read the OLE32 container and get the EncryptedPackage stream?This code would be a good starter, i.e. it would save me a lot of java code to include.When the stream is decrypted,
Hi, Sorry it took me so long to reply to this!! I've been a bit out.To get this package and read the ole32, I just used the code that is "behind" a plugin from Anton Scheffer, it has a encript hex str
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
hashRaw raw(100);
ivRaw raw(100);
decryptedVerifierInput raw(100);
decryptedVerifierValue raw(100);
decryptedKeyValue raw(100);
decryptedSalt raw(100);
p_excel blob;
p_info blob;
t_info_xml xmltype;
decr_excel blob;
decr_len number;
--
procedure blob2file
( p_blob blob
, p_directory varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.xlsx'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_directory
, p_filename
, 'wb'
);
for i in 0 .. trunc( ( dbms_lob.getlength( p_blob ) - 1 ) / t_len )
loop
utl_file.put_raw( t_fh
, dbms_lob.substr( p_blob
, t_len
, i * t_len + 1
)
);
end loop;
utl_file.fclose( t_fh );
end;
--
function file2blob( p_dir varchar2, p_file_name varchar2 )
return blob
is
t_raw raw(32767);
t_blob blob;
fh utl_file.file_type;
begin
fh := utl_file.fopen( p_dir, p_file_name, 'rb' );
dbms_lob.createtemporary( t_blob, true );
loop
begin
utl_file.get_raw( fh, t_raw );
dbms_lob.append( t_blob, t_raw );
exception
when no_data_found
then
exit;
end;
end loop;
utl_file.fclose( fh );
return t_blob;
exception
when others
then
if utl_file.is_open( fh )
then
utl_file.fclose( fh );
end if;
raise;
end;
--
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
procedure read_cfb( p_cf blob )
is
t_header raw(512);
t_byte_order pls_integer;
t_encoding varchar2(30);
t_ssz pls_integer; -- sector size
t_sssz pls_integer; -- short sector size
t_sectId pls_integer;
t_tmp_sectId t_sectId%type;
type tp_secids is table of t_sectId%type index by pls_integer;
t_msat tp_secids;
t_sat tp_secids;
t_ssat tp_secids;
t_sector raw(2048);
t_short_container blob;
t_stream blob;
t_len pls_integer;
t_name varchar2(32 char);
--
c_Free_SecID constant pls_integer := -1; -- Free sector, may exist in the file, but is not part of any stream
c_End_Of_Chain_SecID constant pls_integer := -2; -- Trailing SecID in a SecID chain
c_SAT_SecID constant pls_integer := -3; -- Sector is used by the sector allocation table
c_MSAT_SecID constant pls_integer := -4; -- Sector is used by the master sector allocation table
--
c_DIR_Empty constant raw(1) := hextoraw( '00' ); -- Empty
c_DIR_Storage constant raw(1) := hextoraw( '01' ); -- User storage
c_DIR_Stream constant raw(1) := hextoraw( '02' ); -- User stream
c_DIR_Lock constant raw(1) := hextoraw( '03' ); -- LockBytes
c_DIR_Property constant raw(1) := hextoraw( '04' ); -- Property
c_DIR_Root constant raw(1) := hextoraw( '05' ); -- Root storage
begin
t_header := dbms_lob.substr( p_cf, 512, 1 );
if ( t_header is null
or utl_raw.length( t_header ) < 512
or utl_raw.substr( t_header, 1, 8 ) != hextoraw( 'D0CF11E0A1B11AE1' )
)
then
return;
end if;
t_byte_order := case when utl_raw.substr( t_header, 29, 2 ) = hextoraw( 'FEFF' ) then utl_raw.little_endian else utl_raw.big_endian end;
if t_byte_order = utl_raw.little_endian
then
t_encoding := 'AL16UTF16LE';
else
t_encoding := 'AL16UTF16';
end if;
t_ssz := power( 2, utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 31, 2 ), t_byte_order ) );
t_sssz := power( 2, utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 33, 2 ), t_byte_order ) );
--
-- read the master sector allocation table
for i in 0 .. 109 - 1
loop
t_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 77 + i * 4, 4 ), t_byte_order );
exit when t_sectId = c_Free_SecID;
t_msat( i ) := t_sectId;
end loop;
t_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 69, 4 ), t_byte_order );
while t_sectId != c_End_Of_Chain_SecID
loop
t_sector := dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_sectId + 1 );
for i in 0 .. t_ssz / 4 - 2
loop
t_msat( t_msat.count() ) := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 4 + 1, 4 ), t_byte_order );
end loop;
t_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, -4, 4 ), t_byte_order );
end loop;
--
-- read and build the sector allocation table
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
for j in 0 .. t_msat.count() - 1
loop
t_sector := dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_msat( j ) + 1 );
for i in 0 .. t_ssz / 4 - 1
loop
t_sat( t_sat.count() ) := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 4 + 1, 4 ), t_byte_order );
end loop;
end loop;
--
-- read and build the short-sector allocation table
t_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 61, 4 ), t_byte_order );
while t_sectId != c_End_Of_Chain_SecID
loop
t_sector := dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_sectId + 1 );
for i in 0 .. t_ssz / 4 - 1
loop
t_ssat( t_ssat.count() ) := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 4 + 1, 4 ), t_byte_order );
end loop;
t_sectId := t_sat( t_sectId );
end loop;
--
-- read directory tree
t_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 49, 4 ), t_byte_order );
while t_sectId != c_End_Of_Chain_SecID
loop
t_sector := dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_sectId + 1 );
for i in 0 .. t_ssz / 128 - 1
loop
t_len := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 128 + 65, 2 ), t_byte_order );
if t_len > 2
then
t_name := utl_i18n.raw_to_char( utl_raw.substr( t_sector, i * 128 + 1, t_len - 2 ), t_encoding );
end if;
case utl_raw.substr( t_sector, i * 128 + 67, 1 )
when c_DIR_Stream
then
dbms_lob.createtemporary( t_stream, true );
t_tmp_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 128 + 117, 4 ), t_byte_order );
t_len := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 128 + 121, 4 ), t_byte_order );
if t_len >= utl_raw.cast_to_binary_integer( utl_raw.substr( t_header, 57, 4 ), t_byte_order )
then
while t_tmp_sectId != c_End_Of_Chain_SecID
loop
dbms_lob.append( t_stream, dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_tmp_sectId + 1 ) );
t_tmp_sectId := t_sat( t_tmp_sectId );
end loop;
else
while t_tmp_sectId != c_End_Of_Chain_SecID
loop
dbms_lob.append( t_stream, dbms_lob.substr( t_short_container, t_sssz, t_sssz * t_tmp_sectId + 1 ) );
t_tmp_sectId := t_ssat( t_tmp_sectId );
end loop;
end if;
dbms_lob.trim( t_stream, t_len );
if t_name = 'EncryptedPackage'
then
p_excel := t_stream;
end if;
if t_name = 'EncryptionInfo'
then
p_info := t_stream;
end if;
when c_DIR_Root
then
dbms_lob.createtemporary( t_short_container, true );
t_tmp_sectId := utl_raw.cast_to_binary_integer( utl_raw.substr( t_sector, i * 128 + 117, 4 ), t_byte_order );
while t_tmp_sectId != c_End_Of_Chain_SecID
loop
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
dbms_lob.append( t_short_container, dbms_lob.substr( p_cf, t_ssz, 512 + t_ssz * t_tmp_sectId + 1 ) );
t_tmp_sectId := t_sat( t_tmp_sectId );
end loop;
else
null;
end case;
end loop;
t_sectId := t_sat( t_sectId );
end loop;
if dbms_lob.istemporary( t_short_container ) = 1
then
dbms_lob.freetemporary( t_short_container );
end if;
if dbms_lob.istemporary( t_stream ) = 1
then
dbms_lob.freetemporary( t_stream );
end if;
end;
function GenerateKey( salt raw, password raw, blockKey raw, hashSize number )
return raw
is
hashBuf raw(1000);
begin
hashBuf := dbms_crypto.hash( utl_raw.concat( salt, password ), dbms_crypto.hash_sh1 );
for i in 0 .. spinCount - 1
loop
hashBuf := dbms_crypto.hash( utl_raw.concat( utl_raw.cast_from_binary_integer( i, utl_raw.little_endian ), hashBuf ), dbms_crypto.hash_sh1 );
end loop;
hashBuf := dbms_crypto.hash( utl_raw.concat( hashBuf, blockKey ), dbms_crypto.hash_sh1 );
if utl_raw.length( hashBuf ) < hashSize
then
hashBuf := utl_raw.overlay( hextoraw( '01' ), hashBuf, hashSize + 1, 0, hextoraw( '36' ) );
else
hashBuf := utl_raw.substr( hashBuf, 1, hashSize );
end if;
return hashBuf;
end;
function Decrypt( key raw, iv raw, src raw, pad number := dbms_crypto.PAD_ZERO )
return raw
is
begin
return dbms_crypto.decrypt( src, dbms_crypto.ENCRYPT_AES128 + dbms_crypto.CHAIN_CBC + pad, key, iv );
end;
begin
dbms_output.put_line( 'x' );
t_cf := file2blob( 'MY_DIR', 'my.xlsx' );
read_cfb( t_cf );
dbms_lob.freetemporary( t_cf );
t_info_xml := xmltype( utl_raw.cast_to_varchar2( dbms_lob.substr( p_info, 10000, 9 ) ) );
dataSaltValue := t_info_xml.extract( 'encryption/keyData/#saltValue', 'xmlns="http://schemas.microsoft.com/office/2006/encryption"' ).getstringval();
saltValue := t_info_xml.extract( 'encryption/keyEncryptors/keyEncryptor/p:encryptedKey/#saltValue', 'xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p="http://schemas.
encryptedKeyValue := t_info_xml.extract( 'encryption/keyEncryptors/keyEncryptor/p:encryptedKey/#encryptedKeyValue', 'xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p="http://
encryptedVerifierInput := t_info_xml.extract( 'encryption/keyEncryptors/keyEncryptor/p:encryptedKey/#encryptedVerifierHashInput', 'xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p
encryptedVerifierValue := t_info_xml.extract( 'encryption/keyEncryptors/keyEncryptor/p:encryptedKey/#encryptedVerifierHashValue', 'xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p
encryptedHmacKey := t_info_xml.extract( 'encryption/dataIntegrity/#encryptedHmacKey', 'xmlns="http://schemas.microsoft.com/office/2006/encryption"' ).getstringval();
encryptedHmacValue := t_info_xml.extract( 'encryption/dataIntegrity/#encryptedHmacValue', 'xmlns="http://schemas.microsoft.com/office/2006/encryption"' ).getstringval();
spincount := t_info_xml.extract( 'encryption/keyEncryptors/keyEncryptor/p:encryptedKey/#spinCount', 'xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p="http://schemas.microsoft.com
--
-- verify password
saltRaw := utl_encode.base64_decode( utl_raw.cast_to_raw( saltValue ) );
verifierInputKey := GenerateKey( saltRaw, utl_i18n.string_to_raw( p_pw, 'AL16UTF16LE' ), encrVerifierHashInputBlockKey, keyBits / 8 );
decryptedVerifierInput := Decrypt( verifierInputKey, saltRaw, utl_encode.base64_decode( utl_raw.cast_to_raw( encryptedVerifierInput ) ) );
verifierValueKey := GenerateKey( saltRaw, utl_i18n.string_to_raw( p_pw, 'AL16UTF16LE' ), encrVerifierHashValueBlockKey, keyBits / 8 );
decryptedVerifierValue := Decrypt( verifierValueKey, saltRaw, utl_encode.base64_decode( utl_raw.cast_to_raw( encryptedVerifierValue ) ) );
-- dbms_output.put_line( verifierInputKey );
-- dbms_output.put_line( verifierValueKey );
-- dbms_output.put_line( decryptedVerifierInput );
-- dbms_output.put_line( decryptedVerifierValue );
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
-- dbms_output.put_line( dbms_crypto.hash( decryptedVerifierInput, dbms_crypto.hash_sh1 ) );
dbms_output.put_line( case when dbms_crypto.hash( decryptedVerifierInput, dbms_crypto.hash_sh1 ) = decryptedVerifierValue then 'password OK' else 'password not OK' end);
--
hashRaw := GenerateKey( saltRaw, utl_i18n.string_to_raw( p_pw, 'AL16UTF16LE' ), encryptedKeyValueBlockKey, keyBits / 8 );
decryptedKeyValue := Decrypt( hashRaw, saltRaw, utl_encode.base64_decode( utl_raw.cast_to_raw( encryptedKeyValue ) ) );
--
-- verify integrity
saltRaw := utl_encode.base64_decode( utl_raw.cast_to_raw( encryptedHmacKey ) );
hashRaw := utl_encode.base64_decode( utl_raw.cast_to_raw( encryptedHmacValue ) );
ivRaw := dbms_crypto.hash( utl_raw.concat( utl_encode.base64_decode( utl_raw.cast_to_raw( dataSaltValue ) ), encrIntegritySaltBlockKey ), dbms_crypto.hash_sh1 );
if utl_raw.length( ivRaw ) < blockSize
then
ivRaw := utl_raw.overlay( hextoraw( '01' ), ivRaw, blockSize + 1, 0, hextoraw( '00' ) );
else
ivRaw := utl_raw.substr( ivRaw, 1, blockSize );
end if;
decryptedSalt := Decrypt( decryptedKeyValue, ivRaw, saltRaw );
dbms_output.put_line( 'key ' || encryptedHmacKey );
dbms_output.put_line( 'val ' || encryptedHmacValue );
dbms_output.put_line( 'mac ' || dbms_crypto.mac( p_excel, dbms_crypto.hmac_sh1, decryptedSalt ) );
ivRaw := dbms_crypto.hash( utl_raw.concat( utl_encode.base64_decode( utl_raw.cast_to_raw( dataSaltValue ) ), encrIntegrityHmacValueBlocKkey ), dbms_crypto.hash_sh1 );
if utl_raw.length( ivRaw ) < blockSize
then
ivRaw := utl_raw.overlay( hextoraw( '01' ), ivRaw, blockSize + 1, 0, hextoraw( '00' ) );
else
ivRaw := utl_raw.substr( ivRaw, 1, blockSize );
end if;
-- dbms_output.put_line( rawtohex( DecryptAlgorithm( decryptedKeyValue, ivRaw, hashRaw ) ) );
dbms_output.put_line( case when dbms_crypto.mac( p_excel, dbms_crypto.hmac_sh1, decryptedSalt ) = dbms_lob.substr( Decrypt( decryptedKeyValue, ivRaw, hashRaw ), 100, 1 ) then 'package file OK' else
--
-- decrypt file
decr_len := to_number( rawtohex( utl_raw.reverse( dbms_lob.substr( p_excel, 8, 1 ) ) ), 'xxxxxxxxxxxxxxxx' );
dbms_lob.createtemporary( decr_excel, true );
for i in 0 .. trunc( ( dbms_lob.getlength( p_excel ) - 9 ) / 4096 )
loop
ivRaw := dbms_crypto.hash( utl_raw.concat( utl_encode.base64_decode( utl_raw.cast_to_raw( dataSaltValue ) ), utl_raw.cast_from_binary_integer( i, utl_raw.little_endian ) ), dbms_crypto.hash_sh1 );
if utl_raw.length( ivRaw ) < blockSize
then
ivRaw := utl_raw.overlay( hextoraw( '01' ), ivRaw, blockSize + 1, 0, hextoraw( '36' ) );
else
ivRaw := utl_raw.substr( ivRaw, 1, blockSize );
end if;
dbms_lob.append( decr_excel, Decrypt( decryptedKeyValue, ivRaw, dbms_lob.substr( p_excel, 4096, 9 + i * 4096 ), dbms_crypto.PAD_NONE ) );
end loop;
dbms_lob.trim( decr_excel, decr_len);
blob2file( decr_excel, 'MY_DIR', '2_book.xlsx' );
dbms_lob.freetemporary( decr_excel );
dbms_lob.freetemporary( p_excel );
dbms_lob.freetemporary( p_info );
end;
Thank you Anton.I will try it as soon as I can.I have got the separted part for the encryption package/info. I just could not do much further from it.I will - again, as soon as I can- try out the dese
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
WHEN UTL_HTTP.end_of_body THEN
NULL;
END;
l_list := json_list(l_response_text);
FOR i IN 1..l_list.count
LOOP
A_id := json_ext.get_string(json(l_list.get(i)),'_id');
l_val := json_ext.get_json_value(json(l_list.get(i)),'Photo');
dbms_lob.createtemporary(Photo, true, 2);
json_value.get_string(l_val, Photo);
dbms_output.put_line(dbms_lob.getlength(Photo));
dbms_output.put_line(dbms_lob.substr(Photo, 20, 1));
blobOriginal := base64decode1(Photo);
A_Name := json_ext.get_string(json(l_list.get(i)),'Name');
Remarks := json_ext.get_string(json(l_list.get(i)),'Remarks');
Status := json_ext.get_string(json(l_list.get(i)),'Status');
UserId := json_ext.get_string(json(l_list.get(i)),'UserId');
A_Date := json_ext.get_string(json(l_list.get(i)),'Date');
A_Time := json_ext.get_string(json(l_list.get(i)),'Time');
MSG_status := json_ext.get_string(json(l_list.get(i)),'MSG_status');
Oracle_Flag := json_ext.get_string(json(l_list.get(i)),'Oracle_Flag');
acl := json_ext.get_string(json(l_list.get(i)),'acl');
INSERT
INTO Appery_Photos
(
A_id,
Photo,
DecodedPhoto,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
)
VALUES
(
A_id,
Photo,
blobOriginal,
A_Name,
Remarks,
Status,
UserId,
A_Date,
A_Time,
MSG_status ,
Oracle_Flag,
acl
);
dbms_lob.freetemporary(Photo);
END LOOP;
-- finalizing
UTL_HTTP.end_response(l_http_response);
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
UTL_HTTP.end_response(l_http_response);
END;
Any help is deeply appreciated.
function clobbase642blob( p_clob clob )return blobis t_blob blob; t_buffer varchar2(32767); t_pos number := 1; t_size number := nls_charset_decl_len( 32764, nls_charset_id( 'char_cs' ) ); t_len n
Unfortunately, this function also gives the same result. The BLOB file cannot be openned.!
In that case I would seriously doubt the content of your received clob. Did you displayed the base64 decoded clob after you received it and after json decoding?
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The value Photo is stored in the column Photo in the table which is of the type CLOB. I openned that value by double click and then copy the entire text into the online converter I mentioned above. It
After I run this I can see your pictures. Looks like you are a Winston fan Please note the substr I used on the Photo clob. declare tmp blob;function clobbase642blob( p_clob clob )return blobis t_b
In my environment it worked with something like below (inspired from some answer in another thread from SY):
declare
l_blob blob;
l_clob clob;
procedure decodeBlob( blobSource in out blob, clobBase64 in clob ) is
step constant number := 2400;
l_tmp raw(2400);
begin
for i in 0..trunc( length(clobBase64)/step ) loop
l_tmp := utl_encode.base64_decode(utl_raw.cast_to_raw(SubStrb(clobBase64, i * step + 1, step)));
dbms_lob.writeappend (blobSource
, utl_raw.length(l_tmp)
, l_tmp
);
end loop;
end;
begin
dbms_lob.createtemporary(l_blob, true, 2);
dbms_lob.createtemporary(l_clob, true, 2);
select replace(replace(mybase64stringasClob, chr(10)), chr(13)) into l_clob from my_table where id = 1;
decodeBlob(l_blob, l_clob);
update my_other_table set myblob = l_blob where id = 1;
dbms_lob.freetemporary(l_clob);
dbms_lob.freetemporary(l_blob);
end;
Notice that 2400 was just to get a small picture split. You may use 24000 for example.
Why not store just the bae64-encoded string and just convert it if needed.May be it should just be used in e.g. html, xml and then there may be no conversion needed.
The problem is/was not the base64 decoding. He doesn't have base64 encoded strings, but base64 encode dataURi's, something like data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA So OP has to use somehting
Yup it works perfectly now. In fact, yes I'm fan of Winston Many thanks ascheffer
ascheffer wrote:
The problem is/was not the base64 decoding. He doesn't have base64 encoded strings, but base64 encode dataURi's, something like
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA
So OP has to use somehting like clobbase642blob( substr( Photo, instr( Photo, ',' ) + 1 ) )
Ok, i just didnt want to open that large word document.However, i find my solution slightly more readable and compact.It's a quick fix with the substr:select replace(replace(substr(mybase64stringasClo
Categories
ORACLE Database Discus...
Database
Business Intelligence
Fusion Middleware Arch...
XML
Application Server
Data Warehousing
E-Business Suite Archi...
Customer Advisory Boar...
Technologies (Korean)(...
SOA Suite (Korean)
Applications (Korean)(...
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Migration and Moderniz...
Communications Service...
Siebel Archived Forums...
Hyperion System 9 Appl...
Master Data Management...
Identity Management Ar...
Technology Network Com...
Archived BEA Dev2Dev F...
WebLogic Server/Java E...
AquaLogic User Interac...
WebLogic Event Server/...
Industries
Construction and Engin...
Archived Discussion Fo...
Primavera Customer Com...
⼩憩⼀会
Oracle Policy Automation
User Productivity Kit ...
Oracle Solaris
Embedded Technologies
Other Topics (Archived)
Java Real-Time(Archived)
Agile PLM
Information Discovery
HCM Functional Discuss...
Developer (Archived)
My Oracle Support Comm...
Oracle Mobile
Oracle SysDev Hub
Cloud Infrastructure C...
Oracle Partner Cloud C...
FBGBU
Application Developmen...
JDeveloper and ADF
General Database Discu...
WebCenter
LCM: 11i Install/Upgrade
OC4J(Archived)
Generic Linux
Java Database Connecti...
Archived Forum(Archived)
Identity Manager
Sales and Marketing
Human Resource Managem...
XML DB
IA 64 Beta Program(Arc...
C++ Call Interface (OC...
On Demand: E-Business ...
Berkeley DB
Products (Korean)(Arch...
Performance Tuning (Ko...
Oracle Business Rules ...
엔터프라이즈 관리자 ((Korean))(...
OC4J (Korean)(Archived)
Technologies - XML (Ko...
Communications Converg...
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Essbase Analytics(Arch...
Financial Consolidatio...
Financial Consolidation
Coherence Support
Long Messages Forum(Ar...
Enterprise 2.0(Archived)
weblogic.community.gen...
System Management and ...
开发⼯具
weblogic.developer.int...
liquid.developer.inter...
Cryptography
Java Native Interface ...
Storage General Discus...
Solaris Archived Forum...
Driver Development(Arc...
Oracle Enterprise Mana...
Technical
Big Data Connectors/Ha...
Business Questions(Arc...
Database Cloud Service
Customizations, Extens...
Payroll
My Oracle Support - Li...
Oracle x86 Servers
Storage Cloud Service
Service Supply Chain
Oracle Financials Clou...
Forms Gruppe
Partner Cloud Center o...
Distribuidor Oracle
HGBU
Resources
database
java
software
javascript
android
java
csharp
php
python
ios
jquery
html
sql
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Copyright © 2018 www.databasedevelop.com
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD