-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathas_zip.sql
539 lines (534 loc) · 20.7 KB
/
as_zip.sql
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
CREATE OR REPLACE package as_zip
is
/**********************************************
**
** Author: Anton Scheffer
** Date: 25-01-2012
** Website: http://technology.amis.nl/blog
**
** Changelog:
** Date: 04-08-2016
** fixed endless loop for empty/null zip file
** Date: 28-07-2016
** added support for defate64 (this only works for zip-files created with 7Zip)
** Date: 31-01-2014
** file limit increased to 4GB
** Date: 29-04-2012
** fixed bug for large uncompressed files, thanks Morten Braten
** Date: 21-03-2012
** Take CRC32, compressed length and uncompressed length from
** Central file header instead of Local file header
** Date: 17-02-2012
** Added more support for non-ascii filenames
** Date: 25-01-2012
** Added MIT-license
** Some minor improvements
******************************************************************************
******************************************************************************
Copyright (C) 2010,2011 by Anton Scheffer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
******************************************************************************
******************************************** */
type file_list is table of clob;
g_size_limit integer := power(2, 32);
g_size_limit_sqlcode integer := -20200;
g_size_limit_message varchar2(200) := 'Maximum file size of 4GB exceeded';
--
function file2blob
( p_dir varchar2
, p_file_name varchar2
)
return blob;
--
function get_file_list
( p_dir varchar2
, p_zip_file varchar2
, p_encoding varchar2 := null
)
return file_list;
--
function get_file_list
( p_zipped_blob blob
, p_encoding varchar2 := null
)
return file_list;
--
function get_file
( p_dir varchar2
, p_zip_file varchar2
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob;
--
function get_file
( p_zipped_blob blob
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob;
--
procedure add1file
( p_zipped_blob in out nocopy blob
, p_name varchar2
, p_content blob
, p_date date default sysdate
);
--
procedure finish_zip( p_zipped_blob in out nocopy blob );
--
procedure save_zip
( p_zipped_blob blob
, p_dir varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.zip'
);
--
/*
declare
g_zipped_blob blob;
begin
as_zip.add1file( g_zipped_blob, 'test4.txt', null ); -- a empty file
as_zip.add1file( g_zipped_blob, 'dir1/test1.txt', utl_raw.cast_to_raw( q'<A file with some more text, stored in a subfolder which isn't added>' ) );
as_zip.add1file( g_zipped_blob, 'test1234.txt', utl_raw.cast_to_raw( 'A small file' ) );
as_zip.add1file( g_zipped_blob, 'dir2/', null ); -- a folder
as_zip.add1file( g_zipped_blob, 'dir3/', null ); -- a folder
as_zip.add1file( g_zipped_blob, 'dir3/test2.txt', utl_raw.cast_to_raw( 'A small filein a previous created folder' ) );
as_zip.finish_zip( g_zipped_blob );
as_zip.save_zip( g_zipped_blob, 'MY_DIR', 'my.zip' );
dbms_lob.freetemporary( g_zipped_blob );
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
--
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 blob2num( p_blob blob, p_len integer, p_pos integer )
return number
is
rv number;
begin
rv := utl_raw.cast_to_binary_integer( dbms_lob.substr( p_blob, p_len, p_pos ), utl_raw.little_endian );
if rv < 0
then
rv := rv + 4294967296;
end if;
return rv;
end;
--
function raw2varchar2( p_raw raw, p_encoding varchar2 )
return varchar2
is
begin
return coalesce( 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 little_endian( p_big number, p_bytes pls_integer := 4 )
return raw
is
t_big number := p_big;
begin
if t_big > 2147483647
then
t_big := t_big - 4294967296;
end if;
return utl_raw.substr( utl_raw.cast_from_binary_integer( t_big, utl_raw.little_endian ), 1, p_bytes );
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;
if dbms_lob.istemporary( file_blob ) = 1
then
dbms_lob.freetemporary( file_blob );
end if;
raise;
end;
--
function get_file_list
( p_zipped_blob blob
, p_encoding varchar2 := null
)
return file_list
is
t_ind integer;
t_hd_ind integer;
t_rv file_list;
t_encoding varchar2(32767);
begin
t_ind := nvl( dbms_lob.getlength( p_zipped_blob ), 0 ) - 21;
loop
exit when t_ind < 1 or dbms_lob.substr( p_zipped_blob, 4, t_ind ) = c_END_OF_CENTRAL_DIRECTORY;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := blob2num( p_zipped_blob, 4, t_ind + 16 ) + 1;
t_rv := file_list();
t_rv.extend( blob2num( p_zipped_blob, 2, t_ind + 10 ) );
for i in 1 .. blob2num( p_zipped_blob, 2, t_ind + 8 )
loop
if p_encoding is null
then
if utl_raw.bit_and( dbms_lob.substr( p_zipped_blob, 1, t_hd_ind + 9 ), hextoraw( '08' ) ) = hextoraw( '08' )
then
t_encoding := 'AL32UTF8'; -- utf8
else
t_encoding := 'US8PC437'; -- IBM codepage 437
end if;
else
t_encoding := p_encoding;
end if;
t_rv( i ) := raw2varchar2
( dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_hd_ind + 28 )
, t_hd_ind + 46
)
, t_encoding
);
t_hd_ind := t_hd_ind + 46
+ blob2num( p_zipped_blob, 2, t_hd_ind + 28 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 30 ) -- Extra field length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 32 ); -- File comment length
end loop;
--
return t_rv;
end;
--
function get_file_list
( p_dir varchar2
, p_zip_file varchar2
, p_encoding varchar2 := null
)
return file_list
is
begin
return get_file_list( file2blob( p_dir, p_zip_file ), p_encoding );
end;
--
function get_file
( p_zipped_blob blob
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob
is
t_tmp blob;
t_ind integer;
t_hd_ind integer;
t_fl_ind integer;
t_encoding varchar2(32767);
t_len integer;
begin
t_ind := nvl( dbms_lob.getlength( p_zipped_blob ), 0 ) - 21;
loop
exit when t_ind < 1 or dbms_lob.substr( p_zipped_blob, 4, t_ind ) = c_END_OF_CENTRAL_DIRECTORY;
t_ind := t_ind - 1;
end loop;
--
if t_ind <= 0
then
return null;
end if;
--
t_hd_ind := blob2num( p_zipped_blob, 4, t_ind + 16 ) + 1;
for i in 1 .. blob2num( p_zipped_blob, 2, t_ind + 8 )
loop
if p_encoding is null
then
if utl_raw.bit_and( dbms_lob.substr( p_zipped_blob, 1, t_hd_ind + 9 ), hextoraw( '08' ) ) = hextoraw( '08' )
then
t_encoding := 'AL32UTF8'; -- utf8
else
t_encoding := 'US8PC437'; -- IBM codepage 437
end if;
else
t_encoding := p_encoding;
end if;
if p_file_name = raw2varchar2
( dbms_lob.substr( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_hd_ind + 28 )
, t_hd_ind + 46
)
, t_encoding
)
then
t_len := blob2num( p_zipped_blob, 4, t_hd_ind + 24 ); -- uncompressed length
if t_len = 0
then
if substr( p_file_name, -1 ) in ( '/', '\' )
then -- directory/folder
return null;
else -- empty file
return empty_blob();
end if;
end if;
--
if dbms_lob.substr( p_zipped_blob, 2, t_hd_ind + 10 ) in ( hextoraw( '0800' ) -- deflate
, hextoraw( '0900' ) -- deflate64
)
then
t_fl_ind := blob2num( p_zipped_blob, 4, t_hd_ind + 42 );
t_tmp := hextoraw( '1F8B0800000000000003' ); -- gzip header
dbms_lob.copy( t_tmp
, p_zipped_blob
, blob2num( p_zipped_blob, 4, t_hd_ind + 20 )
, 11
, t_fl_ind + 31
+ blob2num( p_zipped_blob, 2, t_fl_ind + 27 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_fl_ind + 29 ) -- Extra field length
);
dbms_lob.append( t_tmp, utl_raw.concat( dbms_lob.substr( p_zipped_blob, 4, t_hd_ind + 16 ) -- CRC32
, little_endian( t_len ) -- uncompressed length
)
);
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 := blob2num( p_zipped_blob, 4, t_hd_ind + 42 );
dbms_lob.createtemporary( t_tmp, true );
dbms_lob.copy( t_tmp
, p_zipped_blob
, t_len
, 1
, t_fl_ind + 31
+ blob2num( p_zipped_blob, 2, t_fl_ind + 27 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_fl_ind + 29 ) -- Extra field length
);
return t_tmp;
end if;
end if;
t_hd_ind := t_hd_ind + 46
+ blob2num( p_zipped_blob, 2, t_hd_ind + 28 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 30 ) -- Extra field length
+ blob2num( p_zipped_blob, 2, t_hd_ind + 32 ); -- File comment length
end loop;
--
return null;
end;
--
function get_file
( p_dir varchar2
, p_zip_file varchar2
, p_file_name varchar2
, p_encoding varchar2 := null
)
return blob
is
begin
return get_file( file2blob( p_dir, p_zip_file ), p_file_name, p_encoding );
end;
--
procedure add1file
( p_zipped_blob in out nocopy blob
, p_name varchar2
, p_content blob
, p_date date default sysdate
)
is
t_now timestamp with time zone;
t_blob blob;
t_len integer;
t_clen integer;
t_crc32 raw(4) := hextoraw( '00000000' );
t_compressed boolean := false;
t_name raw(32767);
begin
t_now := cast(nvl(p_date, sysdate) as timestamp with local time zone) at time zone 'UTC';
t_len := nvl( dbms_lob.getlength( p_content ), 0 );
if t_len > 0
then
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob ) - 18;
t_compressed := t_clen < t_len;
t_crc32 := dbms_lob.substr( t_blob, 4, t_clen + 11 );
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;
t_name := utl_i18n.string_to_raw( compose(p_name), 'AL32UTF8' );
dbms_lob.append( p_zipped_blob
, utl_raw.concat( c_LOCAL_FILE_HEADER -- Local file header signature
, hextoraw( '1400' ) -- version 2.0
, case when t_name = utl_i18n.string_to_raw( p_name, 'US8PC437' )
then hextoraw( '0000' ) -- no General purpose bits
else hextoraw( '0008' ) -- set Language encoding flag (EFS)
end
, case when t_compressed
then hextoraw( '0800' ) -- deflate
else hextoraw( '0000' ) -- stored
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
+ ( greatest(to_number( to_char( t_now, 'yyyy' ) ) - 1980, 0) ) * 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
, hextoraw( '0000' ) -- Extra field length
, t_name -- File name
)
);
if t_compressed
then
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 11 ); -- compressed content
elsif t_clen > 0
then
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 1 ); -- content
end if;
if dbms_lob.istemporary( t_blob ) = 1
then
dbms_lob.freetemporary( t_blob );
end if;
if g_size_limit < dbms_lob.getlength( p_zipped_blob ) then
raise_application_error (g_size_limit_sqlcode, g_size_limit_message || ' in as_zip.add1file');
end if;
end;
--
procedure finish_zip( p_zipped_blob in out nocopy 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, improved by Dirk Strack' );
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;
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( '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_offs + 30
) -- File name
)
);
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
, 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
)
);
if g_size_limit < dbms_lob.getlength( p_zipped_blob ) then
raise_application_error (g_size_limit_sqlcode, g_size_limit_message || ' in as_zip.finish_zip');
end if;
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;
/