import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; String zipFile = context.get("FileTargetPath").toString(); String srcFile = context.get("FileSourcePath").toString(); int length; FileOutputStream fos = null; ZipOutputStream zos = null; FileInputStream fis = null; try { byte[] buffer = new byte[1024]; fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(fos); File file = new File(srcFile); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].exists() && files[i].getName().endsWith("pdf")) { fis = new FileInputStream(files[i]); zos.putNextEntry(new ZipEntry(files[i].getName())); while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); if (fis != null) { fis.close(); } } } } catch (IOException ioe) { ioe.printStackTrace(); System.out.println("Error creating zip file" + ioe); throw ioe; } catch (Exception e) { throw new ServiceException(e); } finally { try{ if (fis != null) { fis.close(); } if (zos != null) { zos.close(); } if (fos != null) { fos.close(); } } catch (Exception e) { e.printStackTrace(); } }