Some time ago I received a new challenge at work: The business requested to send a zipped file by e-mail, with password.
Sending files by e-mail is not something new. But zip the file and set a password from ABAP was a challenge. After some researching, I have not found something like a function module or a class to do it from an ABAP program. But what I have found is that from an ABAP program, we can run commands on the server side. This, open the door for a possible solution.
So, basically, what I did to solve this issue is: generate the file from the ABAP program and save it on the server side. Then run this command and zip the file together with the password. All in the same command.
Below, the steps to be followed:
- Generate the file in the server side. In this case was an excel so first I converted to an XSTRING and then save with the OPEN DATASET/TRANSFER:
OPEN DATASET p_file FOR OUTPUT IN BINARY MODE. IF sy-subrc = 0. TRANSFER lv_xstring TO p_file. CLOSE DATASET p_file. ENDIF.
2. We have to create the command on the server. It is quite simple, in our case we called it ZSD_ENCRYPT (maybe not so correctly called :). And then the most important is that as marked in below red box we enter “ZIP”.
3. Then we call below function modules. There is no much to comment about it but it is to do kind of check of the command. Please adapt the coding with correct error handling and according to the naming convention you use:
DATA: BEGIN OF command_list OCCURS 0. INCLUDE STRUCTURE sxpgcolist. DATA: END OF command_list . DATA: status LIKE btcxp3-exitstat, commandname LIKE sxpgcolist-name VALUE 'ZSD_ENCRYPT', sel_no LIKE sy-tabix. * GET LIST OF EXTERNAL COMMANDS CALL FUNCTION 'SXPG_COMMAND_LIST_GET' EXPORTING commandname = commandname operatingsystem = sy-opsys TABLES command_list = command_list EXCEPTIONS OTHERS = 1. CALL FUNCTION 'SXPG_COMMAND_CHECK' EXPORTING commandname = command_list-name operatingsystem = sy-opsys EXCEPTIONS no_permission = 1 command_not_found = 2 parameters_too_long = 3 security_risk = 4 wrong_check_call_interface = 5 x_error = 6 too_many_parameters = 7 parameter_expected = 8 illegal_command = 9 communication_failure = 10 system_failure = 11 OTHERS = 12.
4. Below is where the magic occurs. Here I generate the zip file protected by password:
* In below concatenate I set the parameters to the ZIP command is going to be run in the server side. * -P means the zip goes with password. * LV_PASSWORD -> I set the password * L_FILE_ZIP -> is the zip file name (destination) * L_FILE -> is the file to be zipped (origin) CONCATENATE '-j' '-P' lv_password l_file_zip l_file INTO v_dir_input SEPARATED BY space. * C_EXTCOM is the command I created in SM49 before. In my case ZSD_ENCRYPT. * C_OPER is the operating system. In S4 Hana normally is Linux. CALL FUNCTION 'SXPG_COMMAND_EXECUTE' EXPORTING commandname = c_extcom additional_parameters = v_dir_input operatingsystem = c_oper TABLES exec_protocol = t_result EXCEPTIONS no_permission = 1 command_not_found = 2 parameters_too_long = 3 security_risk = 4 wrong_check_call_interface = 5 program_start_error = 6 program_termination_error = 7 x_error = 8 parameter_expected = 9 too_many_parameters = 10 illegal_command = 11 wrong_asynchronous_parameters = 12 cant_enq_tbtco_entry = 13 jobcount_generation_error = 14 OTHERS = 15.
Once you create the file on the server it is up to you how to continue. You could copy from the server to a local laptop or send by e-mail. This is up to the business request. Take into account that you can face authorization issues when running the command. Maybe you have to talk with basis or authorization guy. In this case, we run the command to zip a file but take into account that you could run any other command which is available in the op system (Linux in our case).
Sometimes we get challenges at work. At first time they could look impossible to solve but if we struggle a little bit and try to be creative many times we can reach to a good solution for the business.
Hope it be useful for you this small “hack”