Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Generate csv file compressed (.gz):

former_member1571
Discoverer
0 Likes
2,951

Hello everyone,

I need to generate a csv file from an internal table, I did that before using SAP_CONVERT_TO_CSV_FORMAT and GUI_DOWNLOAD. Now I need the file to be compressed with .gz extension.

Thank you for your help!

Hello everyone,

I need to generate a csv file from an internal table, I did that before using SAP_CONVERT_TO_CSV_FORMAT and GUI_DOWNLOAD. Now I need the file to be compressed with .gz extension.

Thank you for your help!

5 REPLIES 5
Read only

Former Member
0 Likes
2,331

Thank you for asking a question in the SAP Community!

Your question is very important to us. It would be helpful for you to take the tutorial: https://community.sap.com/resources/questions-and-answers which provides tips for preparing questions that draw the best responses from our members. Plus, by organizing your Community profile using this tutorial: https://developers.sap.com/tutorials/community-profile.html you will encourage more readers to respond to your questions!

Thank you!

Read only

Sandra_Rossi
Active Contributor
2,331

Just search:

create GZip site:sap.com

(you should also ask your client if they want a gzip with header or without header)

Read only

former_member1571
Discoverer
0 Likes
2,331

I created a programm as below, I obtained a .gz but can not open it in my PC, please check my coding

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
  EXPORTING
    i_field_seperator    = ';'
  TABLES
    i_tab_sap_data       = gt_csv
  CHANGING
    i_tab_converted_data = gt_csvdata
  EXCEPTIONS
    conversion_failed    = 1
    OTHERS               = 2.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

LOOP AT gt_csvdata INTO ls_csvdata.
  CONCATENATE ls_csvdata cl_abap_char_utilities=>newline INTO ls_csvdata.
  CONDENSE ls_csvdata.
  MODIFY gt_csvdata FROM  ls_csvdata.
  CLEAR : ls_csvdata.
ENDLOOP.

lt_csvdata[] = gt_csvdata[].
REFRESH : gt_csvdata.
CONCATENATE 'DateDeDebutDeTranche' 'DateDeFinDeTranche'
            'CodeOperande' 'Installation' 'Saison'
            'ValeurOperande' 'CodeSupp'
    INTO ld_header
    SEPARATED BY ';'."c_semicol'.

CONCATENATE ld_header cl_abap_char_utilities=>cr_lf INTO ld_header.
*
CLEAR : ls_csvdata.
ls_csvdata = ld_header.
APPEND ls_csvdata TO gt_csvdata.
APPEND LINES OF lt_csvdata TO gt_csvdata.


CONCATENATE LINES OF gt_csvdata INTO l_string.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text   = l_string
  IMPORTING
    buffer = lv_xstring
  EXCEPTIONS
    failed = 1
    OTHERS = 2.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*    CREATE OBJECT lo_zip. "Create instance of Zip Class
*  lo_zip->support_unicode_names = abap_true.
*
*  lo_zip->add(  name    =  'Test.csv'"gd_file "'ZCONTINU_REPORT.csv'
*  content = lv_xstring ).
*
*  lv_zip_xstring = lo_zip->save( ).

" get the length of xstring
raw_in_len = xstrlen( lv_xstring ).

" compress the file
TRY.
    CALL METHOD cl_abap_gzip=>compress_binary
      EXPORTING
        raw_in         = lv_xstring
        raw_in_len     = raw_in_len
        compress_level = 6
      IMPORTING
        gzip_out       = lv_zip_xstring
        gzip_out_len   = gv_bin_length_o.
  CATCH cx_parameter_invalid_range .
  CATCH cx_sy_buffer_overflow .
ENDTRY.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer        = lv_zip_xstring
  IMPORTING
    output_length = gv_bin_length_o
  TABLES
    binary_tab    = ct_csv.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

" download to presentation server
CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
   bin_filesize            = gv_bin_length_o
    filename                = 'c:template3.gz'
    filetype                = 'BIN'
*    codepage                = '4120'
  CHANGING
    data_tab                = ct_csv
  EXCEPTIONS
    file_write_error        = 1
    no_batch                = 2
    gui_refuse_filetransfer = 3
    invalid_type            = 4
    no_authority            = 5
    unknown_error           = 6
    header_not_allowed      = 7
    separator_not_allowed   = 8
    filesize_not_allowed    = 9
    header_too_long         = 10
    dp_error_create         = 11
    dp_error_send           = 12
    dp_error_write          = 13
    unknown_dp_error        = 14
    access_denied           = 15
    dp_out_of_memory        = 16
    disk_full               = 17
    dp_timeout              = 18
    file_not_found          = 19
    dataprovider_exception  = 20
    control_flush_error     = 21
    not_supported_by_gui    = 22
    error_no_gui            = 23
    OTHERS                  = 24.

Read only

0 Likes
2,331

Did you install software that is able to read that gzip?

Did you ask your client if they want a gzip with header or without header? (the one you created is without header)