Apr 2, 2016

How to Create a Zip File in ABAP

By Fareez Ahamed

We generate different kinds of files varying from Excel sheets to Text files based on the data available in SAP. It is very much possible to zip multiple reports within a single zip file and download the zip file from SAP.

In this article we are going to generate a zip file which will contain Material Number (MATNR) and Material Description (MAKTX) in two languages (English and German) in two different files.

SAP has provided the class CL_ABAP_ZIP for reading and writing zip files which we are going to utilize to generate zip file. Following is the sequence of process involved to generate a zip file.

  • Fetch data into internal tables
  • Convert them to xstring.
  • Create object for CL_ABAP_ZIP
  • Use add method to add new files to the zip archive
  • Now, save method returns the final zip file in xstring
  • Convert the xstring to internal table
  • Download it using GUI_DOWNLOAD

The result:



And the program is as follows:

*&---------------------------------------------------------------------*
*& Report  ZDI_ZIP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdi_zip.

DATA:
      BEGIN OF file_stru,
        matnr TYPE matnr,
        maktx TYPE maktx,
      END OF file_stru,
      BEGIN OF s_matnr,
        matnr TYPE matnr,
      END OF s_matnr,
      BEGIN OF s_line,
        line TYPE LENGTH 256,
      END OF s_line,
      gt_mara_en LIKE STANDARD TABLE OF file_stru,
      gt_mara_de LIKE STANDARD TABLE OF file_stru,
      gt_matnr LIKE STANDARD TABLE OF s_matnr,
      go_zip TYPE REF TO cl_abap_zip,
      g_xstr_en TYPE xstring,
      g_xstr_de TYPE xstring,
      g_xstr_zip TYPE xstring.

START-OF-SELECTION.

  "Read and prepare data
  PERFORM fetch_data.

  "Create object for CL_ABAP_ZIP
  CREATE OBJECT go_zip.

  "Convert itabs to xstring
  PERFORM convert_to_xstring
    TABLES gt_mara_en
    USING  g_xstr_en.

  PERFORM convert_to_xstring
    TABLES gt_mara_de
    USING  g_xstr_de.

  "Add xstring as file content to zip
  go_zip->add(
    EXPORTING
      name 'en.txt'
      content g_xstr_en
  ).

  go_zip->add(
    EXPORTING
      name 'de.txt'
      content g_xstr_de
  ).

  "Get the xstring for final zip file
  g_xstr_zip go_zip->save).

  "Dowload the xstring received
  "from save method
  PERFORM download_zip_file.

  WRITE 'File Downloaded'.


*&---------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------*
*       text
*----------------------------------------------------*
FORM fetch_data.
  DATA:
        lt_mara_en LIKE STANDARD TABLE OF file_stru,
        lt_mara_de LIKE STANDARD TABLE OF file_stru,
        s_temp LIKE file_stru.

  "Select thousand materials
  SELECT
    matnr
  FROM mara
  INTO TABLE gt_matnr
  UP TO 1000 ROWS.

  "Select their english texts
  SELECT
    matnr
    maktx
  FROM makt
  INTO TABLE lt_mara_en
  FOR ALL ENTRIES IN gt_matnr
    WHERE matnr EQ gt_matnr-matnr
    AND spras EQ 'E'.

  "Select their german texts
  SELECT
    matnr
    maktx
  FROM makt
  INTO TABLE lt_mara_de
  FOR ALL ENTRIES IN gt_matnr
    WHERE matnr EQ gt_matnr-matnr
    AND spras EQ 'D'.

  SORT gt_matnr ASCENDING.
  SORT gt_mara_en ASCENDING.
  SORT gt_mara_de ASCENDING.

  LOOP AT gt_matnr INTO s_matnr.

    CLEAR s_temp.

    READ TABLE lt_mara_en INTO s_temp
      WITH KEY matnr s_matnr-matnr.

    IF sy-subrc EQ 0.
      APPEND s_temp TO gt_mara_en.
    ELSE.
      s_temp-matnr s_matnr-matnr.
      APPEND s_temp TO gt_mara_en.
    ENDIF.

    CLEAR s_temp.

    READ TABLE lt_mara_de INTO s_temp
      WITH KEY matnr s_matnr-matnr.

    IF sy-subrc EQ 0.
      APPEND s_temp TO gt_mara_de.
    ELSE.
      s_temp-matnr s_matnr-matnr.
      APPEND s_temp TO gt_mara_de.
    ENDIF.

  ENDLOOP.

ENDFORM.                    "fetch_data

*&----------------------------------------------*
*&      Form  convert_to_xstring
*&----------------------------------------------*
*       text
*-----------------------------------------------*
*      -->P_TAB      text
*      -->P_XSTR     text
*-----------------------------------------------*
FORM convert_to_xstring
  TABLES p_tab
  USING p_xstr.

  DATA:
        lt_lines LIKE STANDARD TABLE OF s_line.

  LOOP AT p_tab INTO file_stru.
    s_line-line file_stru.
    APPEND s_line TO lt_lines.
  ENDLOOP.

  CALL FUNCTION 'SCMS_TEXT_TO_XSTRING'
*   EXPORTING
*     FIRST_LINE       = 0
*     LAST_LINE        = 0
*     MIMETYPE         = ' '
   IMPORTING
     buffer           p_xstr
   TABLES
     text_tab         lt_lines
   EXCEPTIONS
     failed           1
     OTHERS           2
            .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


ENDFORM.                    "convert_to_xstring

*&------------------------------------------------*
*&      Form  download_zip_file
*&------------------------------------------------*
*       text
*-------------------------------------------------*
FORM download_zip_file.

  DATA:
        l_len TYPE i,
        lt_lines LIKE STANDARD TABLE OF s_line.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer          g_xstr_zip
*     APPEND_TO_TABLE = ' '
    IMPORTING
      output_length   l_len
    TABLES
      binary_tab      lt_lines.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename   'D:/out.zip'
      filetype   'BIN'
    IMPORTING
      filelength l_len
    TABLES
      data_tab   lt_lines.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    "download_zip_file



No comments :