0

I am currently working on JAX-RS, where I'm trying to send and receive Zip files. Currently for sending the zip file in response, I'm able to achieve it using the below code (verified the zip downloading by entering the URL in the browser), but I'm not clear about writing the code logic to read the Zip file from response.

Please help me to achieve this functionality.

@GET
@Produces({"application/zip"})
@Path("getProduct")
public Response getProduct() {
    String METHODNAME = "getProduct";
     if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
            LOGGER.entering(CLASSNAME, METHODNAME);
     }
     File file;
     try {
         Properties prop = getProp();
         file = new File(prop.getProperty("ZipLocation")+prop.getProperty("ProductZip"));
         byte[] buffer = new byte[5120];
         FileOutputStream fos = new FileOutputStream(file);
         ZipOutputStream zos = new ZipOutputStream(fos);
         ZipEntry ze= new ZipEntry(prop.getProperty("ProductOutfile"));
         zos.putNextEntry(ze);
         FileInputStream in = new FileInputStream("C:\\Documents\\ProductExtract.xml");
         int len;
         while ((len = in.read(buffer)) > 0) {
             zos.write(buffer, 0, len);
         }
         in.close();
         zos.closeEntry();
         zos.close();
     } catch (FileNotFoundException ex) {
            LOGGER.logp(Level.SEVERE, CLASSNAME, METHODNAME, ex.getMessage(), ex);
            return Response.status(204).entity(ex.getMessage()).build();
     } catch (Exception ex) {
        LOGGER.logp(Level.SEVERE, CLASSNAME, METHODNAME, ex.getMessage(), ex);
        return Response.status(204).entity(ex.getMessage()).build();
     }
     return Response.ok(file, "application/zip").header("Content-Disposition", "attachment; filename=\""+file.getName()+"\"")
            .header("Content-Type", "application/zip").header("Set-Cookie", "fileDownload=true; path=/").build();
}

Kindly let me know how can I achieve reading the zip file from response of the above code.

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
Gautam R
  • 326
  • 1
  • 4
  • 14

1 Answers1

4

Once you are using the JAX-RS Client API, your client code can be like:

Client client = ClientBuilder.newClient();
InputStream is = client.target("http://localhost:8080")
                       .path("api").path("getProduct")
                       .request().accept("application/zip")
                       .get(InputStream.class);

ZipInputStream zis = new ZipInputStream(is);

Then read the content of the ZipInputStream.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • if I do `zis.read()` or `zis.read(byte)` i'm getting -1 as the value, which is like the stream is empty. But when I make a call from POSTMAN i'm getting the response from the web-service, also checked by entering the URL in a browser i'm able to download the zip file. – Gautam R Sep 15 '16 at 13:39
  • @GautamR `-1` has another meaning. Check the [documentation](https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipInputStream.html#read-byte:A-int-int-). – cassiomolin Sep 15 '16 at 13:43
  • I just find this in the document. **Returns: the actual number of bytes read, or -1 if the end of the entry is reached** When I do a `read()` and get -1 at the start itself can mean that the stream is empty. – Gautam R Sep 15 '16 at 14:14
  • @GautamR What do you want to do with the ZIP on client side? List the content? Write to the disk? Extract the files? – cassiomolin Sep 15 '16 at 14:28
  • I need to extract the zip file and write it as an XML file on the disk and then process it. – Gautam R Sep 15 '16 at 14:49
  • @GautamR This [answer](http://stackoverflow.com/a/23870468/1426227) may be useful for you. – cassiomolin Sep 15 '16 at 15:17
  • My current implementation is exactly the same as this. But its still failing. – Gautam R Sep 15 '16 at 15:32