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

半城伤御伤魂 提交于 2019-12-06 03:48:20

问题


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"

回答1:


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;


来源:https://stackoverflow.com/questions/26772666/how-to-use-utl-compress-package-to-compress-the-txt-file-and-create-gz-file-in-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!