public void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException { System.out.println("Writing '" + fileName + "' to zip file"); File file = new File(fileName); FileInputStream fis = new FileInputStream(file); zos.putNextEntry(new ZipEntry(new File(fileName).getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }
static void addDir(File dirObj, ZipOutputStream out) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; assert files != null; for (File file : files) { if (file.isDirectory()) { addDir(file, out); continue; } FileInputStream in = new FileInputStream(file.getPath()); System.out.println(" Adding: " + file.getPath()); out.putNextEntry(new ZipEntry(file.getPath())); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } }