Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UTL_COMPRESS package to compress the txt file and create gz file in unix with oracle 11 g?

Tags:

oracle11g

I have following requirement : Actually I have txt file I need to compress this file and create gz file using oracle UTL_COMPRESS package. I need to implement this functionality on Unix box with Oracle 11g. I have tried it with below code and it is working for some extent.I mean it is working to compress the small size file.

DECLARE
   f utl_file.file_type;
   compressed     BLOB;
  data_b BFILE;
BEGIN
   f := UTL_FILE.fopen ('DIR_UTL_COM_TEST', 'Test1.gz', 'wb');
   data_b := BFILENAME ('DIR_UTL_COM_TEST','pk_intibuy_pkb.txt');
   DBMS_LOB.FILEOPEN (data_b, DBMS_LOB.LOB_READONLY);
   DBMS_LOB.createtemporary (compressed, false);
   compressed := UTL_COMPRESS.lz_compress (data_b,6);
   UTL_FILE.put_raw(f, compressed, true);
   UTL_FILE.fclose (f);
   DBMS_LOB.FILECLOSE (data_b);
   DBMS_LOB.freetemporary (compressed);
END;

But this code is not working to compress the large file. Please help if some implement this functionality in oracle 11g. Would be much appreciate. Error Messages:

Error report:
ORA-06502: PL/SQL: numeric or value error 
ORA-06512: at line 11 06502. 00000 - "PL/SQL: numeric or value error%s"
like image 396
Rohit Avatar asked Nov 01 '22 14:11

Rohit


1 Answers

I am able to resolve the problem. I have modified the code and below code is working fine for compress the large size file as well.

DECLARE
   in_filename VARCHAR2(100);
   src_file   BFILE;
   v_content  BLOB;
   v_blob_len INTEGER;
   v_file     utl_file.file_type;
   v_buffer   RAW(32767);
   v_amount   BINARY_INTEGER := 32767;
   v_pos      INTEGER := 1;
BEGIN
    in_filename := 'Test.txt';
   src_file := bfilename('DIR_UTL_COM_TEST', in_filename);
   dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
   v_content  := utl_compress.lz_compress(src_file, 9);
   v_blob_len := dbms_lob.getlength(v_content);
   v_file     := utl_file.fopen('DIR_UTL_COM_TEST',
                                in_filename || '.gz',
                                'wb');
   WHILE v_pos < v_blob_len LOOP
      dbms_lob.READ(v_content, v_amount, v_pos, v_buffer);
      utl_file.put_raw(v_file, v_buffer, TRUE);
      v_pos := v_pos + v_amount;
   END LOOP;
   utl_file.fclose(v_file);

EXCEPTION
   WHEN OTHERS THEN
      IF utl_file.is_open(v_file) THEN
         utl_file.fclose(v_file);
      END IF;
      RAISE;
END;
like image 57
Rohit Avatar answered Nov 18 '22 19:11

Rohit