主要用到了xsting和binary之间的转换以及gui的操作
代码如下
*&---------------------------------------------------------------------* *& Report zpr_zip_file *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT zpr_zip_file. START-OF-SELECTION. "选择文件 DATA lv_file_table TYPE filetable. DATA lv_rc TYPE i. DATA lv_user_action TYPE i. DATA lv_file_encoding TYPE abap_encoding. cl_gui_frontend_services=>file_open_dialog( EXPORTING window_title = '选择一个Excel文件' default_extension = 'xlsx' file_filter = cl_gui_frontend_services=>filetype_all multiselection = abap_false CHANGING file_encoding = lv_file_encoding file_table = lv_file_table rc = lv_rc user_action = lv_user_action EXCEPTIONS OTHERS = 1 ). IF lv_rc = 1. DATA(lv_file) = lv_file_table[ 1 ]. ENDIF. DATA lv_file_path TYPE string. DATA lv_file_name TYPE string. "拆分文件 cl_bcs_utilities=>split_path( EXPORTING iv_path = lv_file IMPORTING ev_path = lv_file_path ev_name = lv_file_name ). "上传文件 IF lv_file IS NOT INITIAL. DATA lv_fileLength TYPE i. DATA lv_fileheader TYPE xstring. DATA lv_file_data TYPE STANDARD TABLE OF solix. cl_gui_frontend_services=>gui_upload( EXPORTING filetype = 'BIN' filename = CONV string( lv_file ) IMPORTING filelength = lv_fileLength header = lv_fileheader CHANGING data_tab = lv_file_data ). ENDIF. DATA lv_file_xstring TYPE xstring. CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_fileLength IMPORTING buffer = lv_file_xstring TABLES binary_tab = lv_file_data EXCEPTIONS failed = 1 OTHERS = 2. "压缩文件 DATA(lo_zip) = NEW cl_abap_zip( ). lo_zip->support_unicode_names = abap_true."防止中文乱码 lo_zip->add( name = lv_file_name content = lv_file_xstring ). DATA(lo_zip_file) = lo_zip->save( ). DATA: lv_len TYPE i. DATA lt_bin TYPE solix_tab. CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lo_zip_file IMPORTING output_length = lv_len TABLES binary_tab = lt_bin. DATA lv_save_filename TYPE string. DATA lv_save_filepath TYPE string. DATA lv_save_filefullPath TYPE string. DATA lv_save_action TYPE i. "打开保存窗口 cl_gui_frontend_services=>file_save_dialog( EXPORTING window_title = '保存文件' default_extension = 'zip' default_file_name = 'test.zip' file_filter = cl_gui_frontend_services=>filetype_all prompt_on_overwrite = abap_true CHANGING filename = lv_save_filename path = lv_save_filepath fullpath = lv_save_filefullPath user_action = lv_save_action EXCEPTIONS cntl_error = 1 " Control error error_no_gui = 2 " No GUI available not_supported_by_gui = 3 " GUI does not support this invalid_default_file_name = 4 " Invalid default file name OTHERS = 5 ). IF lv_save_action = cl_gui_frontend_services=>action_ok. "下载 cl_gui_frontend_services=>gui_download( EXPORTING filetype = 'BIN' filename = lv_save_filefullPath IMPORTING filelength = lv_fileLength CHANGING data_tab = lt_bin ). ENDIF.
问题点:
如果文件是中文文件名,则可能遇到乱码的问题,原因是cl_abap_zip类的add方法中对文件名进行了编码
这里可以看到support_unicode_names默认是false,则进行的是CP437的编码,针对中文进行437编码时无法编码的字符责备替换为了#,所以我们在调用add方法时,需要将support_unicode_names设置为true,避免中文变为#号
标签:上传,压缩,lv,ABAP,file,TYPE,save,DATA,gui From: https://www.cnblogs.com/Lingcc/p/18321448