cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP->JAVA : How to unzip a zip file sent by ABAP

aayush_dubey3
Explorer
0 Kudos

Hello,

We are using JCO to exchange the XML strings (File Sizes can range upto couple of MBs) between ABAP and JAVA. We were earlier using a STRING parameter for XML data exchange. This approach was resulting in huge performance overheads. As per our initial performance investigations, we have identified that time taken for XML exchange using JCO is the major bottlenecks.

Currently we are evaluating Zip - Exchange - Unzip mechanism for XML exchange.

We now try to ZIP the export content using following ABAP code:

      TRY.
          CALL METHOD cl_abap_gzip=>compress_text
            EXPORTING
              text_in  = lv_xmlstring
            IMPORTING
              gzip_out = ev_xmlstring.

This returns an XSTRING (zipped) which is lesser in size as compared to the earlier xml STRING.For zipping the XML String this code works fine. But on java end on using the following code (for unzipping):



 public static String unzipStringFromBytes( byte[] bytes ) throws IOException
  {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
   
    BufferedInputStream bufis = new BufferedInputStream(new InflaterInputStream(bis));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len;
    while( (len = bufis.read(buf)) > 0 )
    {
      bos.write(buf, 0, len);
    }
    String retval = bos.toString();
    bis.close();
    bufis.close();
    bos.close();
    return retval;
  }

We get the following error:


java.util.zip.DataFormatException: unknown compression method
	at java.util.zip.Inflater.inflateBytes(Native Method)
	at java.util.zip.Inflater.inflate(Inflater.java:215)

Please share java code snippet which we can use in our program to retrieve our initial XML string.

Best Regards,

Aayush

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

i took a sequence '0123456789' and gziped it with your ABAP code snipped.

Result is in HEX:

'33 30 34 32 36 31 35 33 B7 B0 04 00'

Than i took an good old gzip executable and gzipped a text file with the same sequence in compression mode 6(default of ABAP Method).

Result in HEX:

'1F 8B 08 08 BA B1 D1 45 00 0B 31 2E 74 78 74 00'

'33 30 34 32 36 31 35 33 B7 B0 04 00 C6 C7 84 A6' <-- quiet similar with ABAP gzip data

'0A 00 00 00'

It seems that some gzip headers and trailers are missing from ABAP Processor's output, i don't know why, maybe it will help you to fake the file headers's to your data section: http://www.gzip.org/zlib/rfc-gzip.html

Other topic is your Exception: java.util.zip.Inflater.inflate(Inflater.java:215)

Inflater is used for ZLIB compressed files, try to pass your InputStream to an Object of [java.util.zip.GZIPInputStream] like:

http://www.galileocomputing.de/openbook/javainsel6/javainsel_12_010.htm [Listing 12.29]

Let me know if it works and have fun.....

Former Member
0 Kudos

try to replace code line 3

BufferedInputStream bufis = new BufferedInputStream(new InflaterInputStream(bis));

to

BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));

Answers (1)

Answers (1)

aayush_dubey3
Explorer
0 Kudos

Hello All,

Thanks for your help Roman. Earlier i too found that GZIP is not working. We are using ZIP (CL_ABAP_ZIP) instead.

At java end the problem was solved by using the following code:


byte[] zippedBytes=(byte[]) exportParamValues.get("CONTENT_XML_ZIP");
	
	  ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zippedBytes));
	
	ZipEntry entry = in.getNextEntry();

   ByteArrayOutputStream out = new  ByteArrayOutputStream();
    BufferedOutputStream bout = new BufferedOutputStream(out);
    // Transfer bytes from the ZIP file to the output file
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        bout.write(buf, 0, len);
       }
    bout.flush();
    
 String xmlString = out.toString("UTF-16");

    out.close();
    in.close();

Hit and trial works ;).

Best Regards,

Aayush