IOUtil { public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException { String fileName = file.getName(); try (FileInputStream fos = new FileInputStream(file)) { ZipEntry zipEntry = new ZipEntry(fileName); zipEntry.setSize(file.length()); zipEntry.setTime(System.currentTimeMillis()); zipOs.putNextEntry(zipEntry); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = fos.read(buf)) > 0) { zipOs.write(buf, 0, bytesRead); } zipOs.closeEntry(); fos.close(); } } private  IOUtil(); static String readStringFromStream(InputStream stream); static InputStream writeStringToStream(String string); static OutputStream getOutputStream(File file); static void copy(InputStream input, OutputStream output); static void copyFileToDirectory(File file, File dir); static void fileFindReplace(File file, String find, String replace); static File writeStreamToFile(InputStream inStream); static File writeStreamToFile(InputStream inStream, boolean gzip); static InputStream getFileStream(File file); static void addFileToZipOutputStream(File file, ZipOutputStream zipOs);  }
@Test public void testAddFileToZipOutputStream() throws Exception { String content = "content"; InputStream contentStream = IOUtil.writeStringToStream(content); File file = IOUtil.writeStreamToFile(contentStream); file.deleteOnExit(); assertNotNull(file); assertTrue(file.exists()); File zipFile = File.createTempFile("test", "zip"); zipFile.deleteOnExit(); ZipOutputStream zip = new ZipOutputStream(IOUtil.getOutputStream(zipFile)); IOUtil.addFileToZipOutputStream(file, zip); zip.close(); ZipInputStream is = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry = is.getNextEntry(); Assert.assertEquals(file.getName(), entry.getName()); String finalContent = IOUtil.readStringFromStream(is); Assert.assertEquals(content, finalContent); }