/** * Zip the files. * * @param srcFiles The source of files. * @param zipFilePath The path of ZIP file. * @return {@code true}: success<br>{@code false}: fail * @throws IOException if an I/O error has occurred */ public static boolean zipFiles(final Collection<String> srcFiles, final String zipFilePath) throws IOException { return zipFiles(srcFiles, zipFilePath, null); }
/** * Unzip the file by keyword. * * @param zipFilePath The path of ZIP file. * @param destDirPath The path of destination directory. * @param keyword The keyboard. * @return the unzipped files * @throws IOException if unzip unsuccessfully */ public static List<File> unzipFileByKeyword(final String zipFilePath, final String destDirPath, final String keyword) throws IOException { return unzipFileByKeyword(XFile.getFileByPath(zipFilePath), XFile.getFileByPath(destDirPath), keyword); }
/** * Return the files' path in ZIP file. * * @param zipFile The ZIP file. * @return the files' path in ZIP file * @throws IOException if an I/O error has occurred */ public static List<String> getFilesPath(final File zipFile) throws IOException { if (zipFile == null) return null; List<String> paths = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { String entryName = ((ZipEntry) entries.nextElement()).getName().replace("\\", "/"); if (entryName.contains("../")) { Log.e("XZip", "entryName: " + entryName + " is dangerous!"); paths.add(entryName); } else { paths.add(entryName); } } zip.close(); return paths; }
/** * Return the files' comment in ZIP file. * * @param zipFile The ZIP file. * @return the files' comment in ZIP file * @throws IOException if an I/O error has occurred */ public static List<String> getComments(final File zipFile) throws IOException { if (zipFile == null) return null; List<String> comments = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = ((ZipEntry) entries.nextElement()); comments.add(entry.getComment()); } zip.close(); return comments; }