SAP提供了一个类CL_ABAP_ZIP来创建.zip扩展名的压缩文件。
代码:
先将文件通过cl_gui_frontend_services=>gui_upload以BIN的文件类型上载,然后通过cl_abap_zip中的方法add()
save()压缩,最后下载到本地。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | REPORT ztest_zip. TYPES:BEGIN OF bin_file, nameTYPEstring, sizeTYPE i, dataTYPE solix_tab, END OF bin_file . DATA: lv_filename TYPEstring, wa_bindata TYPE bin_file, it_bindata TYPESTANDARD TABLE OF bin_file, oref_zip TYPEREF TO cl_abap_zip, lv_zip_xstring TYPExstring, lv_xstring TYPExstring, lv_path TYPEstring, it_filetab TYPE filetable, lv_retcode TYPE i, v_usr TYPE i, v_zip_size TYPE i, it_zip_bin_dataTYPESTANDARD TABLE OF raw255, v_dest_filepathTYPEstring. DATA lv_filesizeTYPE i. DATA wa_filetabLIKELINEOF it_filetab. "create object CREATE OBJECT oref_zip. "upload cl_gui_frontend_services=>file_open_dialog( EXPORTING window_title ='Select files that you want to ZIP' multiselection ='X' CHANGING file_table = it_filetab rc = lv_retcode user_action = v_usr). LOOP AT it_filetabINTO wa_filetab. lv_filename= wa_filetab-filename. cl_gui_frontend_services=>gui_upload( EXPORTING filename = lv_filename filetype ='BIN' IMPORTING filelength = wa_bindata-size CHANGING data_tab = wa_bindata-data). CALL FUNCTION'SO_SPLIT_FILE_AND_PATH' EXPORTING full_name = lv_filename IMPORTING stripped_name= wa_bindata-name EXCEPTIONS x_error =1 OTHERS =2. APPEND wa_bindataTO it_bindata. ENDLOOP. LOOP AT it_bindataINTO wa_bindata. CALL FUNCTION'SCMS_BINARY_TO_XSTRING' EXPORTING input_length= wa_bindata-size IMPORTING buffer = lv_xstring TABLES binary_tab = wa_bindata-data. oref_zip->add(name = wa_bindata-name content= lv_xstring). ENDLOOP . lv_zip_xstring= oref_zip->save(). CALL FUNCTION'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lv_zip_xstring IMPORTING output_length= v_zip_size TABLES binary_tab = it_zip_bin_data. cl_gui_frontend_services=>file_save_dialog( EXPORTING window_title ='SELECT THE LOCATION TO SAVE THE FILE' file_filter ='(*.ZIP)|*.ZIP|' CHANGING filename = lv_filename path = lv_path fullpath = v_dest_filepath). cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = v_zip_size filename = v_dest_filepath filetype ='BIN' IMPORTING filelength = lv_filesize CHANGING data_tab = it_zip_bin_data). |
以上。