create zip folder with cl_abap_zip

REPORT  ztest.

DATA: gv_file_length    TYPE i,
      gv_filehexcontent TYPE xstring,
      gv_zipfilehex     TYPE xstring,
      gt_filebincontent TYPE solix_tab,
      gt_zipfilebin     TYPE solix_tab,
      gv_zipfilename    TYPE string,
      gv_folder         TYPE string,
      go_zipper         TYPE REF TO cl_abap_zip,
      gt_files          TYPE TABLE OF char255 WITH HEADER LINE,
      gv_file           TYPE string,
      gv_file_fullpath  TYPE string,
      gv_results        TYPE match_result_tab,
      gv_result         TYPE match_result ,
      gv_folder_name    TYPE string,
      gv_lines          TYPE i,
      gv_no_files       TYPE i.
*--------------------------------------------------------------------*
*   START-OF-SELECTION
*--------------------------------------------------------------------*
START-OF-SELECTION.
  cl_gui_frontend_services=>directory_browse(
        EXPORTING  window_title    = 'Folder selection'
        CHANGING   selected_folder = gv_folder
        EXCEPTIONS OTHERS          = 4 ).
  CHECK sy-subrc EQ 0.
  cl_gui_frontend_services=>directory_list_files(
        EXPORTING directory = gv_folder files_only  = 'X'
        CHANGING  file_table  = gt_files[] count = gv_no_files
        EXCEPTIONS OTHERS      = 4 ).
  CHECK sy-subrc EQ 0 AND gv_no_files GT 0.
  CREATE OBJECT go_zipper.
  FIND ALL OCCURRENCES OF '\' IN gv_folder IN CHARACTER MODE RESULTS gv_results.
  DESCRIBE TABLE gv_results LINES gv_lines.
  READ TABLE gv_results INTO gv_result INDEX gv_lines.
  ADD 1 TO gv_result-offset.
  gv_folder_name = gv_folder+gv_result-offset.
  LOOP AT gt_files.
    MOVE gt_files TO gv_file.
    CONCATENATE gv_folder '\' gv_file INTO gv_file_fullpath.
    cl_gui_frontend_services=>gui_upload(
        EXPORTING
          filename   = gv_file_fullpath
          filetype   = 'BIN'
        IMPORTING
          filelength = gv_file_length
        CHANGING
          data_tab   = gt_filebincontent
        EXCEPTIONS
          OTHERS     = 4 ).
    CHECK sy-subrc EQ 0.
    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
        input_length = gv_file_length
      IMPORTING
        buffer       = gv_filehexcontent
      TABLES
        binary_tab   = gt_filebincontent
      EXCEPTIONS
        failed       = 1
        OTHERS       = 2.
    CHECK sy-subrc EQ 0.
*Populate folder name & file name
    CONCATENATE gv_folder_name '\' gv_file INTO gv_file.
    go_zipper->add( name = gv_file content = gv_filehexcontent ).
  ENDLOOP.
  gv_zipfilehex = go_zipper->save( ).
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = gv_zipfilehex
    TABLES
      binary_tab = gt_zipfilebin.
  CONCATENATE gv_folder '.zip' INTO gv_zipfilename.
  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename = gv_zipfilename
      filetype = 'BIN'
    CHANGING
      data_tab = gt_zipfilebin
    EXCEPTIONS
      OTHERS   = 4.

你可能感兴趣的:(create)