Author: bpoussin Date: 2013-01-04 15:57:11 +0100 (Fri, 04 Jan 2013) New Revision: 2456 Url: http://nuiton.org/projects/nuiton-utils/repository/revisions/2456 Log: add new method to uncompress zip file from stream Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ZipUtil.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ZipUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ZipUtil.java 2013-01-04 14:56:12 UTC (rev 2455) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ZipUtil.java 2013-01-04 14:57:11 UTC (rev 2456) @@ -97,6 +97,21 @@ } /** + * Uncompress zipped stream in targetDir. + * + * + * @param stream the zip source stream, stream is closed before return + * @param targetDir the destination directory + * @return return last entry name + * @throws IOException if any problem while uncompressing + * @since 2.7 + */ + public static String uncompress(InputStream stream, File targetDir) throws IOException { + String result = uncompressAndRename(stream, targetDir, null, null); + return result; + } + + /** * Uncompress zipped file in targetDir, and rename uncompressed file if * necessary. If renameFrom or renameTo is null no renaming is done * <p/> @@ -115,36 +130,63 @@ File targetDir, String renameFrom, String renameTo) throws IOException { + return uncompressAndRename(new FileInputStream(file), targetDir, renameFrom, renameTo); + } + + + /** + * Uncompress zipped stream in targetDir, and rename uncompressed file if + * necessary. If renameFrom or renameTo is null no renaming is done + * <p/> + * file in zip use / to separate directory and not begin with / + * each directory ended with / + * + * @param stream the zip source stream, stream is closed before return + * @param targetDir the destination directory + * @param renameFrom pattern to permit rename file before uncompress it + * @param renameTo new name for file if renameFrom is applicable to it + * you can use $1, $2, ... if you have '(' ')' in renameFrom + * @return return last entry name + * @throws IOException if any problem while uncompressing + * @since 2.7 + */ + public static String uncompressAndRename(InputStream stream, + File targetDir, + String renameFrom, + String renameTo) throws IOException { String result = ""; - ZipInputStream in = new ZipInputStream(new FileInputStream(file)); - ZipEntry entry; - while ((entry = in.getNextEntry()) != null) { - String name = entry.getName(); - if (renameFrom != null && renameTo != null) { - name = name.replaceAll(renameFrom, renameTo); - if (log.isDebugEnabled()) { - log.debug("rename " + entry.getName() + " -> " + name); + ZipInputStream in = new ZipInputStream(new BufferedInputStream(stream)); + try { + ZipEntry entry; + while ((entry = in.getNextEntry()) != null) { + String name = entry.getName(); + if (renameFrom != null && renameTo != null) { + name = name.replaceAll(renameFrom, renameTo); + if (log.isDebugEnabled()) { + log.debug("rename " + entry.getName() + " -> " + name); + } } - } - result = name; - File target = new File(targetDir, name); - if (entry.isDirectory()) { - FileUtil.createDirectoryIfNecessary(target); - } else { - FileUtil.createDirectoryIfNecessary(target.getParentFile()); - OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); - try { - byte[] buffer = new byte[BUFFER_SIZE]; - int len; - while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1) { - out.write(buffer, 0, len); + result = name; + File target = new File(targetDir, name); + if (entry.isDirectory()) { + FileUtil.createDirectoryIfNecessary(target); + } else { + FileUtil.createDirectoryIfNecessary(target.getParentFile()); + OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); + try { + byte[] buffer = new byte[BUFFER_SIZE]; + int len; + while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1) { + out.write(buffer, 0, len); + } + } finally { + out.close(); } - } finally { - out.close(); } } + } finally { + in.close(); } - in.close(); return result; }
participants (1)
-
bpoussin@users.nuiton.org