From time to time you have to work on files that are stored in user PC, like when you’re working with SOI (SAP Office Integration) for example. All operations that are done are save directly to a file which is stored on PC so if you would like to zip it with cl_abap_zip class then you would need to copy file to ABAP memory, then zip it using cl_abap_zip and then save back file to PC. Sometimes this is not the best solution, especially when working on slow connections via VPN or similar. So with help there goes cl_gui_frontend_services=>execute. This method allows you to run every file on PC directly – also a program with proper parameters. In my example I will use IZArc (it’s free and fast).

So what I’m doing here is:
– passing full path to the file which will be zipped
– call cl_gui_frontend_services=>environment_get_variable to check program files directories
– concatenate file path with parameter of IZArc “-ad”
– concatenate program files directories with ‘IZArc\IZArc.exe’
– check if IZArc exists with  cl_gui_frontend_services=>file_exist
– if yes then I’m executing it with cl_gui_frontend_services=>execute.

Here is the full code for such function:

form zip_file_on_pc using f_path type csequence
                          f_synchro
type string
                   
changing f_ziped type c.

  data: f_apppath type string.
  data: f_apppath64 type string.
  data: f_parameter type string.
  data: f_pathloc type string.
  data: f_result type abap_bool.

  f_pathloc = f_path.
  clear f_ziped.

  check f_path is not initial.

  clear f_apppath.
  f_parameter = ‘-ad’. “parameter for IZArc to add file
“check sytem variables for program files

  cl_gui_frontend_services=>environment_get_variable(
        exporting
          variable             = ‘PROGRAMFILES’
        changing
          value                f_apppath
       
exceptions
         cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          others               = 4
          ).
  if sysubrc eq 0.
     cl_gui_cfw=>flush(
            exceptions
              cntl_system_error = 1
              cntl_error        = 2
              others            = 3
                 ).
     if sysubrc eq 0 and f_apppath is not initial.
“add IZArc folder and exe file to path
        concatenate f_apppath ‘\IZArc\IZArc.exe’ into f_apppath.
     endif.
   endif.
clear f_apppath64.
“check sytem variables for program files in 64 bit
  cl_gui_frontend_services=>environment_get_variable(
        exporting
          variable             = ‘PROGRAMW6432’
        changing
          value                f_apppath64
       
exceptions
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          others               = 4
          ).
  if sysubrc eq 0.
     cl_gui_cfw=>flush(
          exceptions
            cntl_system_error = 1
            cntl_error        = 2
            others            = 3
            ).
      if sysubrc eq 0 and f_apppath64 is not initial.
“add IZArc folder and exe file to path
        concatenate f_apppath64 ‘\IZArc\IZArc.exe’ into f_apppath64.
      endif.
  endif.

  if f_parameter is not initial and f_apppath is not initial.
    concatenate ‘”‘ f_pathloc ‘”‘ into f_pathloc.
“add full path of file to zip to parameter -ad
    concatenate f_parameter f_pathloc into f_parameter
                                              
separated by space.
“check if program IZArc exists in first directory
    cl_gui_frontend_services=>file_exist(
      exporting
        file                 = f_apppath
      receiving
        result              
= f_result
     
exceptions
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        others               = 5
           ).
    if sysubrc eq 0 and f_result = abap_true.
      “if yes then execute compression
cl_gui_frontend_services=>execute(
      exporting
*        document = f_path
        application = f_apppath
       
parameter   = f_parameter
        synchronous
= f_synchro
     
exceptions
        cntl_error = 1
        error_no_gui = 2
        bad_parameter = 3
        file_not_found = 4
        path_not_found = 5
        file_extension_unknown = 6
        error_execute_failed = 7
        synchronous_failed = 8
        not_supported_by_gui = 9
        others = 10
        ).
      if sysubrc eq 0.
        f_ziped = ‘X’.
      endif.
    Else.
“if not the we check second directory
      cl_gui_frontend_services=>file_exist(
      exporting
        file                 = f_apppath64
        receiving
        result              
= f_result
     
exceptions
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        others               = 5
        ).
      if sysubrc eq 0 and f_result = abap_true.
        cl_gui_frontend_services=>execute(
        exporting
*        document = f_path
          application = f_apppath64
         
parameter   = f_parameter
          synchronous
= f_synchro
       
exceptions
          cntl_error = 1
          error_no_gui = 2
          bad_parameter = 3
          file_not_found = 4
          path_not_found = 5
          file_extension_unknown = 6
          error_execute_failed = 7
          synchronous_failed = 8
          not_supported_by_gui = 9
          others = 10
          ).
        if sysubrc eq 0.
          f_ziped = ‘X’.
        endif.
      endif.
    endif.
  endif.

endform.     

Article from http://abapblog.com by Łukasz Pęgiel

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

    1. Łukasz Pęgiel Post author

      In our company we use IZArc so that’s why I prepared a solution for that program, but in fact this can be used with most of the archiving programs available on market as long as they provide command line parameters. I think this could be done with VBS script to zip directly with Windows built-in zipper but it was faster for me to create such solution as posted.

      Thanks for the comment.

      (0) 
      1. Manish Kumar

        The VBS option is also there for built-in zip, but it is unreliable has it calls a dll whose usage is undocumented.

        For a folder having several tiny files, the zipping process can take significant time. In that case your parameter f_synchro would be handy to execute the program asynchronously.

        (0) 

Leave a Reply