@Test public void zip_all_files_in_directory() throws Exception { file.mkdirs(); final File target = new File(file, "target.zip"); final File dir1 = createDirectories("dir", "dir1"); final File dir12 = createDirectories("dir", "dir1", "dir2"); final File dir2 = createDirectories("dir", "dir2"); final File dir3 = createDirectories("dir", "dir3"); createFiles(dir1, "f1.txt", "f2.txt", "f3.txt"); createFiles(dir12, "f1.txt", "f4.txt"); createFiles(dir2); createFiles(dir3, "f1.txt"); FileUtil.zip(Collections.singletonList(new File(file, "dir")), target); Set<String> names = new HashSet<>(); try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(target))) { ZipEntry entry; while ((entry = zipInputStream.getNextEntry()) != null) { names.add(entry.getName()); } } Assert.assertEquals(CollectionUtils.asSet("f1.txt", "f2.txt", "f3.txt", "f1_1.txt", "f4.txt", "f1_2.txt"), names); }
public static void zip(List<File> sources, File target) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); final Set<String> takenNames = new HashSet<>(); final List<File> files = walk(sources); for (File file : files) { final String uniqueName = uniqueName(file, takenNames); zipSingleFile(file, uniqueName, out); takenNames.add(uniqueName); } } finally { if (out != null) { out.close(); } } }
FileUtil { public static void zip(List<File> sources, File target) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); final Set<String> takenNames = new HashSet<>(); final List<File> files = walk(sources); for (File file : files) { final String uniqueName = uniqueName(file, takenNames); zipSingleFile(file, uniqueName, out); takenNames.add(uniqueName); } } finally { if (out != null) { out.close(); } } } }
FileUtil { public static void zip(List<File> sources, File target) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); final Set<String> takenNames = new HashSet<>(); final List<File> files = walk(sources); for (File file : files) { final String uniqueName = uniqueName(file, takenNames); zipSingleFile(file, uniqueName, out); takenNames.add(uniqueName); } } finally { if (out != null) { out.close(); } } } private  FileUtil(); }
FileUtil { public static void zip(List<File> sources, File target) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); final Set<String> takenNames = new HashSet<>(); final List<File> files = walk(sources); for (File file : files) { final String uniqueName = uniqueName(file, takenNames); zipSingleFile(file, uniqueName, out); takenNames.add(uniqueName); } } finally { if (out != null) { out.close(); } } } private  FileUtil(); static void write(String text, File target); static Uri getUri(Context context, String authority, File file); static void saveBitmap(Bitmap bitmap, File targetFile, int quality); static void saveBitmap(Bitmap bitmap, File targetFile, Bitmap.CompressFormat format, int quality); static void zip(List<File> sources, File target); static List<File> walk(File source); static List<File> walk(List<File> sources); static String uniqueName(File source, Set<String> taken); static String getFileExtension(File file); static String getFileNameWithoutExtention(File file); }
FileUtil { public static void zip(List<File> sources, File target) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target))); final Set<String> takenNames = new HashSet<>(); final List<File> files = walk(sources); for (File file : files) { final String uniqueName = uniqueName(file, takenNames); zipSingleFile(file, uniqueName, out); takenNames.add(uniqueName); } } finally { if (out != null) { out.close(); } } } private  FileUtil(); static void write(String text, File target); static Uri getUri(Context context, String authority, File file); static void saveBitmap(Bitmap bitmap, File targetFile, int quality); static void saveBitmap(Bitmap bitmap, File targetFile, Bitmap.CompressFormat format, int quality); static void zip(List<File> sources, File target); static List<File> walk(File source); static List<File> walk(List<File> sources); static String uniqueName(File source, Set<String> taken); static String getFileExtension(File file); static String getFileNameWithoutExtention(File file);  }