‎2010 Jun 03 12:48 PM
Hi All,
I have a requirement wherein i have to zip a xml file in abap, convert it to base64 encoded string and send it to JAVA layer.
I'm using the class CL_ABAP_ZIP for zipping the xml string and FM "SCMS_BASE64_ENCODE_STR" to convert the zipped data to base64 encoded string.
But on the JAVA layer we get an exception while unzipping this data.
Has anybody come across a similar situtation.
Please help.
Regards,
Ankit Agrawal
‎2010 Jun 03 1:57 PM
Example
REPORT Z_PAP_UP_ZIP_DL.
DATA: L_ZIPPER TYPE REF TO cl_abap_zip.
DATA: FILEX type XSTRING.
DATA: FILENAME type string.
DATA: PATH type string.
DATA: zip type xstring.
DATA: FILE_N_TAB type FILETABLE.
DATA: FULL_PATH type string.
DATA: FILE_LENGTH type i.
DATA: FILE_TAB type w3mimetabtype.
DATA: WA_INT type int4.
DATA: PATH_TABLE TYPE TABLE of char1024.
"-----------------------------------------------------------------------------
" Load the file
"-----------------------------------------------------------------------------
"Which file to upload
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
CHANGING
FILE_TABLE = FILE_N_TAB
RC = WA_INT
.
"load the (first) file from the frontend the user has selected.
LOOP at FILE_N_TAB into FULL_PATH.
"get the file
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
EXPORTING
FILENAME = FULL_PATH
FILETYPE = 'BIN'
IMPORTING
FILELENGTH = FILE_LENGTH
CHANGING
DATA_TAB = file_tab.
exit.
endloop.
"create xstring from table
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
INPUT_LENGTH = FILE_LENGTH
IMPORTING
BUFFER = FILEX
TABLES
BINARY_TAB = file_tab
"get the name of the file. we take entry after the last '\' ...windows.
SPLIT FULL_PATH AT '\' INTO TABLE PATH_TABLE.
DESCRIBE TABLE PATH_TABLE LINES WA_INT.
READ TABLE PATH_TABLE INTO FILENAME INDEX WA_INT.
‎2010 Jun 03 1:58 PM
Continue.........
"-------------------------------------------------------------------------------
" ZIP the file
"-------------------------------------------------------------------------------
"create our zipper object
CREATE OBJECT L_ZIPPER.
"add file to zip
CALL METHOD L_ZIPPER->ADD
EXPORTING
NAME = filename
CONTENT = filex
.
"save zip
CALL METHOD L_ZIPPER->SAVE
RECEIVING
ZIP = zip
.
"-------------------------------------------------------------------------------
" Save the file
"-------------------------------------------------------------------------------
"convert to table
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = zip
IMPORTING
OUTPUT_LENGTH = file_length
TABLES
BINARY_TAB = file_tab
.
"File save dialog
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
CHANGING
FILENAME = filename
PATH = path
FULLPATH = full_path
.
"Save the file
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
EXPORTING
BIN_FILESIZE = file_length
FILENAME = full_path
FILETYPE = 'BIN'
CHANGING
DATA_TAB = file_tab
.
‎2010 Jun 03 9:52 PM
Ankit, you should at least tell us if you can read the ZIP file on your personal computer? and what exact error messages you get at JAVA side?
‎2010 Jun 07 9:00 AM
Hi Sandra,
On the Java side we get "Not in GZIP format" error.
The code that is used on JAVA side for decoding and unzipping is as under:
byte[] decoded = Base64.decode(inputXML);
GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(decoded));
InputSource inSrc = new InputSource(stream);
return inSrc;
Regards,
Ankit Agrawal
‎2010 Jun 07 10:25 AM
As I could read in [wikipedia - gzip|http://en.wikipedia.org/wiki/Gzip]:
> gzip is not to be confused with the ZIP archive format, which also uses DEFLATE.
So, could you try the CL_ABAP_GZIP class instead of ZIP. See this code sample: [ABAP Class to gzip and gunzip|http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/c6b79908-0a01-0010-8ca0-b77aff37726d]
‎2010 Jun 07 8:34 PM
An alternative so the solution proposed by Sandra would be to adjust your coding on the Java side to handle the zip files. You could utilize for example the [java.util.zip.ZipInputStream|http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipInputStream.html] class.
The main difference between gzip and zip from a functional perspective is that the first one compresses only single files, whereas the zip format can compress multiple files into one archive (thus gzip is often combined with tar, which creates an uncompressed archive of multiple files). With the zip format you'd deal with [java.util.zip.ZipEntry|http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipEntry.html] classes to get to the content of each file.
So if you anticipate that you possibly might have at some point a requirement to handle more than one file, I'd stick with the zip format and adjust the Java coding. As always, there's lots of examples on the internet, see for example the article [compressing and decompressing data using Java API's|http://java.sun.com/developer/technicalArticles/Programming/compression/].