private static void doZipDir(File dir, ZipOutputStream out) throws IOException { File[] children = dir.listFiles(); if (children == null) { throw new IllegalStateException("Fail to list files of directory " + dir.getAbsolutePath()); } for (File child : children) { doZip(child.getName(), child, out); } }
private static void doZip(String entryName, File file, ZipOutputStream out) throws IOException { if (file.isDirectory()) { entryName += "/"; ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); out.closeEntry(); File[] files = file.listFiles(); // java.io.File#listFiles() returns null if object is a directory (not possible here) or if // an I/O error occurs (weird!) if (files == null) { throw new IllegalStateException("Fail to list files of directory " + file.getAbsolutePath()); } for (File f : files) { doZip(entryName + f.getName(), f, out); } } else { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { doZip(entryName, in, out); } } }
private static void zip(File file, ZipOutputStream out) throws IOException { for (File child : file.listFiles()) { String name = child.getName(); doZip(name, child, out); } }
private static void doZipDir(File dir, ZipOutputStream out) throws IOException { File[] children = dir.listFiles(); if (children == null) { throw new IllegalStateException("Fail to list files of directory " + dir.getAbsolutePath()); } for (File child : children) { doZip(child.getName(), child, out); } }
private static void doZip(String entryName, File file, ZipOutputStream out) throws IOException { if (file.isDirectory()) { entryName += '/'; ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); out.closeEntry(); File[] files = file.listFiles(); for (int i = 0, len = files.length; i < len; i++) { doZip(entryName + files[i].getName(), files[i], out); } } else { InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); doZip(entryName, in, out); } finally { IOUtils.closeQuietly(in); } } }
private static void doZip(String entryName, File file, ZipOutputStream out) throws IOException { if (file.isDirectory()) { entryName += "/"; ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); out.closeEntry(); File[] files = file.listFiles(); // java.io.File#listFiles() returns null if object is a directory (not possible here) or if // an I/O error occurs (weird!) if (files == null) { throw new IllegalStateException("Fail to list files of directory " + file.getAbsolutePath()); } for (File f : files) { doZip(entryName + f.getName(), f, out); } } else { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { doZip(entryName, in, out); } } }