static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip, false); } else { byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); try{ zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } }
addFolderToZip("", sourceFile.getAbsolutePath(), out, includeFullPathName);