Skip to content

CL_ABAP_ZIP

Create and extract ZIP archives entirely in ABAP memory — no file system access required.

Purpose

CL_ABAP_ZIP lets you build multi-file ZIP archives in memory and provide them as a single download, or extract files from an uploaded ZIP without touching the server filesystem. All data moves as xstring.

Key methods

Method Description
ADD( name content ) Add a file to the archive (content as xstring)
GET( name ) Extract a file by name — populates content attribute
DELETE( name ) Remove a file from the archive
SAVE( ) Serialise the archive → returns xstring (the ZIP bytes)
LOAD( zip ) Populate the object from existing ZIP xstring
FILES Table attribute — list of entries (name, size)

GET returns content via EXPORTING parameter

lo_zip->get( EXPORTING name = 'file.csv' IMPORTING content = DATA(lx) ) decompresses the entry and returns it as xstring in content. Always check the file exists in FILES before calling GET.

Examples

Create a ZIP with two CSV files and download it

REPORT z_zip_create_demo.

START-OF-SELECTION.

  DATA(lo_zip) = NEW cl_abap_zip( ).

  " Build first CSV content
  DATA lv_csv1 TYPE string.
  lv_csv1 = |CARRID,CONNID,PRICE| && cl_abap_char_utilities=>newline.

  SELECT carrid, connid, price FROM sflight INTO TABLE @DATA(lt_flights) UP TO 5 ROWS.
  LOOP AT lt_flights INTO DATA(ls_f).
    lv_csv1 = lv_csv1 && |{ ls_f-carrid },{ ls_f-connid },{ ls_f-price }| && cl_abap_char_utilities=>newline.
  ENDLOOP.

  " Convert string to xstring (UTF-8)
  DATA(lo_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
  lo_conv->write( data = lv_csv1 ).
  DATA(lx_csv1) = lo_conv->get_buffer( ).

  " Build second CSV
  DATA lv_csv2 TYPE string.
  lv_csv2 = |MATNR,MAKTX| && cl_abap_char_utilities=>newline.
  SELECT matnr, maktx FROM makt INTO TABLE @DATA(lt_makt) UP TO 5 ROWS WHERE spras = 'E'.
  LOOP AT lt_makt INTO DATA(ls_m).
    lv_csv2 = lv_csv2 && |{ ls_m-matnr },{ ls_m-maktx }| && cl_abap_char_utilities=>newline.
  ENDLOOP.

  DATA(lo_conv2) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
  lo_conv2->write( data = lv_csv2 ).
  DATA(lx_csv2) = lo_conv2->get_buffer( ).

  " Add both files to the ZIP
  lo_zip->add( name = 'flights.csv'   content = lx_csv1 ).
  lo_zip->add( name = 'materials.csv' content = lx_csv2 ).

  " Serialise to xstring
  DATA(lx_zip) = lo_zip->save( ).

  " Convert xstring to binary table for download
  DATA lt_rawdata TYPE STANDARD TABLE OF x255.
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING  buffer     = lx_zip
    TABLES     binary_tab = lt_rawdata.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename = 'C:\temp\export.zip'
      filetype = 'BIN'
    TABLES
      data_tab = lt_rawdata
    EXCEPTIONS
      OTHERS   = 1.

  IF sy-subrc = 0.
    MESSAGE 'ZIP downloaded to C:\temp\export.zip' TYPE 'S'.
  ENDIF.

Read files from an uploaded ZIP

REPORT z_zip_read_demo.

START-OF-SELECTION.

  " Upload a ZIP file from the user's PC (binary mode)
  DATA lt_rawdata TYPE STANDARD TABLE OF x255.
  DATA lv_filename TYPE string VALUE 'C:\temp\upload.zip'.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename   = lv_filename
      filetype   = 'BIN'
    TABLES
      data_tab   = lt_rawdata
    EXCEPTIONS
      OTHERS     = 1.

  IF sy-subrc <> 0.
    MESSAGE 'Upload failed' TYPE 'E'.
    RETURN.
  ENDIF.

  " Convert binary table back to xstring
  DATA lx_uploaded TYPE xstring.
  LOOP AT lt_rawdata INTO DATA(ls_raw).
    CONCATENATE lx_uploaded ls_raw INTO lx_uploaded IN BYTE MODE.
  ENDLOOP.

  " Load the ZIP
  DATA(lo_zip) = NEW cl_abap_zip( ).
  lo_zip->load( zip = lx_uploaded ).

  " List all files in the archive
  WRITE: / 'Files in ZIP:'.
  LOOP AT lo_zip->files INTO DATA(ls_file).
    WRITE: / ls_file-name, ls_file-size.
  ENDLOOP.

  " Extract a specific file by name
  " First check it exists
  IF line_exists( lo_zip->files[ name = 'flights.csv' ] ).
    lo_zip->get(
      EXPORTING name    = 'flights.csv'
      IMPORTING content = DATA(lx_content) ).

    " Convert xstring back to string
    DATA(lo_in) = cl_abap_conv_in_ce=>create(
                    encoding = 'UTF-8'
                    input    = lx_content ).
    DATA lv_csv TYPE string.
    lo_in->read( IMPORTING data = lv_csv ).

    " Split into lines
    DATA lt_lines TYPE STANDARD TABLE OF string.
    SPLIT lv_csv AT cl_abap_char_utilities=>newline INTO TABLE lt_lines.

    WRITE: / 'Content of flights.csv:'.
    LOOP AT lt_lines INTO DATA(lv_line).
      WRITE: / lv_line.
    ENDLOOP.
  ELSE.
    MESSAGE 'flights.csv not found in ZIP' TYPE 'W'.
  ENDIF.

File names in the ZIP are case-sensitive

lo_zip->get( name = 'Materials.csv' ) fails silently if the actual entry is materials.csv. Always use the exact name from the FILES table when extracting.

See also

Comments