02-27-2013 3:58 PM
Hi SAP experts,
I have the function module below which I would like to use for zipping of the files.
I'm zipping file which are in UTF-8 encoding. The files are read in binary mode.
The generated zip file via the CL_ABAP_ZIP class is coruppted when I try to process it with 3th party tool written in java.
I'm getting following exception:
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:114)
at java.util.zip.ZipFile.<init>(ZipFile.java:75)
Any idea what am I doing wrong?
This is the code I'm using:
FUNCTION Z_ARIBA_ZIP_DIR.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_DIR) LIKE EPSF-EPSDIRNAM
*" REFERENCE(I_FILE_MASK) LIKE EPSF-EPSFILNAM
*" REFERENCE(I_ZIP_FILENAME) LIKE RLGRAP-FILENAME
*" REFERENCE(I_CCR_ZIP_FILENAME) LIKE RLGRAP-FILENAME
*" EXCEPTIONS
*" OPEN_FAILED
*" WRITE_FAILED
*" CLOSE_FAILED
*"----------------------------------------------------------------------
DATA: l_wa_string(4096) TYPE c,
msg(60).
DATA: l_sys_cp LIKE tcp00-cpcodepage,
l_tcp00 LIKE tcp00,
l_lan_cp LIKE tcp00-cpcodepage.
DATA:
lt_data TYPE TABLE OF x255,
ls_data LIKE LINE OF lt_data.
DATA:
lo_zip TYPE REF TO cl_abap_zip,
lo_ccr_zip TYPE REF TO cl_abap_zip,
lv_zip_file_content TYPE xstring,
lv_file_length TYPE i,
lv_create_zip TYPE CHAR1.
DATA:
lv_line TYPE xstring,
lv_data TYPE xstring.
DATA: lt_dir_list TYPE STANDARD TABLE OF epsfili INITIAL SIZE 0, "Files Table
ls_dir_list LIKE LINE OF lt_dir_list.
DATA: lv_file_path LIKE RLGRAP-FILENAME.
DATA: lv_filename TYPE string.
CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
EXPORTING
DIR_NAME = i_dir
FILE_MASK = i_file_mask
* IMPORTING
* DIR_NAME =
* FILE_COUNTER =
* ERROR_COUNTER =
TABLES
DIR_LIST = lt_dir_list[]
EXCEPTIONS
INVALID_EPS_SUBDIR = 1
SAPGPARAM_FAILED = 2
BUILD_DIRECTORY_FAILED = 3
NO_AUTHORIZATION = 4
READ_DIRECTORY_FAILED = 5
TOO_MANY_READ_ERRORS = 6
EMPTY_DIRECTORY_LIST = 7
OTHERS = 8
.
IF sy-subrc <> c_rc0.
* cannot list given folder
MESSAGE e899(bd) WITH 'CANNOT LIST FOLDER' i_dir RAISING open_failed.
ENDIF.
CREATE OBJECT lo_zip.
* open the zip file
OPEN DATASET i_zip_filename FOR OUTPUT MESSAGE msg IN BINARY MODE.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e890(ux) WITH i_zip_filename RAISING open_failed.
ENDIF.
ENDCATCH.
LOOP AT lt_dir_list INTO ls_dir_list.
IF ls_dir_list-name NS '.csv'.
CONTINUE.
ENDIF.
CLEAR lv_file_path.
CLEAR lv_data.
CLEAR lv_line.
CLEAR ls_data.
CLEAR lt_data.
IF ls_dir_list-name = 'CurrencyConversionRate.csv'.
CONCATENATE i_dir ls_dir_list-name INTO lv_file_path.
lv_filename = ls_dir_list-NAME.
OPEN DATASET lv_file_path FOR INPUT MESSAGE msg IN BINARY MODE.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e890(ux) WITH lv_file_path RAISING open_failed.
ENDIF.
ENDCATCH.
* read the file
DO.
READ DATASET lv_file_path INTO lv_line.
IF SY-SUBRC NE 0.
EXIT.
ELSE.
CONCATENATE lv_data lv_line INTO lv_data IN BYTE MODE.
ENDIF.
ENDDO.
* close the file
CLOSE DATASET lv_file_path.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e891(ux) WITH lv_file_path RAISING close_failed.
ENDIF.
ENDCATCH.
CREATE OBJECT lo_ccr_zip.
CALL METHOD lo_ccr_zip->add
EXPORTING
name = lv_filename "only name of the file - case sensitive
content = lv_data.
lv_zip_file_content = lo_ccr_zip->save( ).
CLEAR lo_ccr_zip.
OPEN DATASET i_ccr_zip_filename FOR OUTPUT MESSAGE msg IN BINARY MODE.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e890(ux) WITH i_ccr_zip_filename RAISING open_failed.
ENDIF.
ENDCATCH.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_file_content
IMPORTING
output_length = lv_file_length
TABLES
binary_tab = lt_data.
* write the zip file
LOOP AT lt_data INTO ls_data.
TRANSFER ls_data TO i_ccr_zip_filename.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e892(ux) WITH i_ccr_zip_filename RAISING write_failed.
ENDIF.
ENDCATCH.
ENDLOOP.
CLOSE DATASET i_ccr_zip_filename.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e891(ux) WITH i_ccr_zip_filename RAISING close_failed.
ENDIF.
ENDCATCH.
ELSE.
* ELSEIF
* ls_dir_list-name = 'PO2.csv' OR
* ls_dir_list-name = 'User.csv' OR ls_dir_list-name = 'Invoice2.csv' OR ls_dir_list-name = 'Supplier.csv'
* OR ls_dir_list-name = 'FlexDimension4.csv'
* OR ls_dir_list-name = 'FlexDimension5.csv' OR ls_dir_list-name = 'FlexDimension6.csv'
* OR ls_dir_list-name = 'FlexDimension1.csv' OR ls_dir_list-name = 'FlexDimension2.csv' OR ls_dir_list-name = 'FlexDimension3.csv'
* OR ls_dir_list-name = 'Account.csv' OR ls_dir_list-name = 'CompanySite.csv' OR ls_dir_list-name = 'Contract.csv'
* OR ls_dir_list-name = 'CostCenter.csv' OR ls_dir_list-name = 'CostCenterMgmt.csv'
* OR ls_dir_list-name = 'ERPCommodity.csv' OR ls_dir_list-name = 'Part.csv'
.
lv_create_zip = 'X'.
CONCATENATE i_dir ls_dir_list-name INTO lv_file_path.
lv_filename = ls_dir_list-NAME.
OPEN DATASET lv_file_path FOR INPUT MESSAGE msg IN BINARY MODE.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e890(ux) WITH lv_file_path RAISING open_failed.
ENDIF.
ENDCATCH.
* read the file
DO.
READ DATASET lv_file_path INTO lv_line.
IF SY-SUBRC NE 0.
EXIT.
ELSE.
CONCATENATE lv_data lv_line INTO lv_data IN BYTE MODE.
ENDIF.
ENDDO.
* close the file
CLOSE DATASET lv_file_path.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e891(ux) WITH lv_file_path RAISING close_failed.
ENDIF.
ENDCATCH.
CALL METHOD lo_zip->add
EXPORTING
name = lv_filename "only name of the file - case sensitive
content = lv_data.
ENDIF.
ENDLOOP.
IF lv_create_zip IS NOT INITIAL.
lv_zip_file_content = lo_zip->save( ).
CLEAR lo_zip.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_file_content
IMPORTING
output_length = lv_file_length
TABLES
binary_tab = lt_data.
* write the zip file
LOOP AT lt_data INTO ls_data.
TRANSFER ls_data TO i_zip_filename.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e892(ux) WITH i_zip_filename RAISING write_failed.
ENDIF.
ENDCATCH.
ENDLOOP.
ENDIF.
CLOSE DATASET i_zip_filename.
CATCH SYSTEM-EXCEPTIONS file_access_errors = c_rc4.
IF sy-subrc <> c_rc0.
MESSAGE e891(ux) WITH i_zip_filename RAISING close_failed.
ENDIF.
ENDCATCH.
* delete the zipped files
* LOOP AT lt_dir_list INTO ls_dir_list.
* IF ls_dir_list-name NS '.csv'.
* CONTINUE.
* ENDIF.
* CONCATENATE i_dir ls_dir_list-name INTO lv_file_path.
* DELETE DATASET lv_file_path.
* IF sy-subrc <> c_rc0.
* MESSAGE e899(bd) WITH 'CANNOT DELETE FILE' lv_file_path.
* ENDIF.
* ENDLOOP.
ENDFUNCTION.
02-28-2013 7:37 AM
Although the file is causing zip exception in java it can be unzipped in Windows and then when I zip it back the zip file is bigger and can be imported into the java application without problems. Any ideas what could be wrong and how can I make the zip working properly in ABAP?
Also I have tried to write simple java problem which is having the problem and throwing the ZipExcpetion.
The exception is thrown with Java 1.5, Java 1.6 but Java 1.7 is able to process the file properly.
Is there any way how I can generate the zip file via CL_ABAP_ZIP to be compatible with Java 1.5 as I'm not able to move to Java 1.7?
Thanks for the answers.
public static void main (String[] args) | |
{ | |
try { | |
ZipFile zipFile = new ZipFile(args[0]); | |
try { | |
Enumeration e = zipFile.entries(); | |
while (e.hasMoreElements()) { | |
ZipEntry entry = (ZipEntry)e.nextElement(); | |
System.out.append("\n" + entry.getName()); | |
} | |
} | |
finally { | |
zipFile.close(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} |
}
02-28-2013 8:35 AM
Hi Ondrej,
Check this Sample Code ... and take some logic may be you resolve your issue...
DATA: ZIP_TOOL TYPE REF TO CL_ABAP_ZIP,
FILENAME TYPE STRING VALUE 'g:\binfile.pdf', " Source File NAme
FILENAME_ZIP TYPE STRING VALUE 'g:\zipfile.zip'. " Zip File
DATA: T_DATA_TAB TYPE TABLE OF X255,
BIN_SIZE TYPE I,
BUFFER_X TYPE XSTRING,
BUFFER_ZIP TYPE XSTRING.
START-OF-SELECTION.
* **upload file from presentation server
CLEAR: T_DATA_TAB[],BIN_SIZE.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = FILENAME
FILETYPE = 'BIN'
IMPORTING
FILELENGTH = BIN_SIZE
* * HEADER =
TABLES
DATA_TAB = T_DATA_TAB.
* **get xstring
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
INPUT_LENGTH = BIN_SIZE
IMPORTING
BUFFER = BUFFER_X
TABLES
BINARY_TAB = T_DATA_TAB.
IF SY-SUBRC <> 0.
* * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* **create zip tool
CREATE OBJECT ZIP_TOOL.
* **add binary file
CALL METHOD ZIP_TOOL->ADD
EXPORTING
NAME = FILENAME "'binfile.pdf'
CONTENT = BUFFER_X.
* **get binary ZIP file
CALL METHOD ZIP_TOOL->SAVE
RECEIVING
ZIP = BUFFER_ZIP.
CLEAR: T_DATA_TAB[],BIN_SIZE.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = BUFFER_ZIP
IMPORTING
OUTPUT_LENGTH = BIN_SIZE
TABLES
BINARY_TAB = T_DATA_TAB.
**download ZIP file
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = BIN_SIZE
FILENAME = FILENAME_ZIP
FILETYPE = 'BIN'
TABLES
DATA_TAB = T_DATA_TAB.
Regard's
Smruti
10-21-2013 5:53 PM
Hi Ondrej,
your issues are caused by a mismatching data type:
DATA:
lt_data TYPE TABLE OF x255,
ls_data LIKE LINE OF lt_data
Use this instead of the code above:
DATA:
lt_data TYPE TABLE OF x,
ls_data LIKE LINE OF lt_data
The Header / Meta data from the zip file are incomplete and this caused your problem. With this longer data type it should work.
best regards
Lukas