import java.io.*; import java.util.zip.*; // edit the below file name parameter to specify name of file that shall be created inside compressed zip file String fileName = "abc.txt"; OutputStream os = null; ZipOutputStream out = null; try { os = service.getOutputStream(); out = new ZipOutputStream(new BufferedOutputStream(os)); byte[] b = new byte[4096]; int i = 0; ZipEntry entry = new ZipEntry(fileName); out.putNextEntry(entry); while((i = inputStream.read(b))!= -1) { out.write( b, 0, i ); } out.closeEntry(); out.flush(); out.finish(); } catch (Exception e) { service.getLogger().error("Error while compressing the file :: " + e.getMessage(), e); throw e; } finally { try { if (out != null) { out.close(); } } catch (Exception e) { //ignore } try { if (os != null) { os.close(); } } catch (Exception e) { //ignore } }