protected File copyZipLocally(String fileName, File unzipDir) { InputStream is = null; FileOutputStream os = null; try { is = sourceDir.getInputStream(fileName, true); if (is != null) { File localZipFile = new File(unzipDir, UUID.randomUUID().toString() + ".zip"); os = new FileOutputStream(localZipFile); IOUtils.copy(is, os); return localZipFile; } else { String msg = String.format("Failed to open %s.", fileName); throw new IoException(msg); } } catch (IOException e) { throw new IoException(e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Override public void handle( Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) { if (inputMessage instanceof TextMessage) { List<String> files = ((TextMessage) inputMessage).getPayload(); ArrayList<String> filePaths = new ArrayList<String>(); for (String fileName : files) { log(LogLevel.INFO, "Preparing to extract file : %s", fileName); FileInfo sourceZipFile = sourceDir.listFile(fileName); if (mustExist && sourceZipFile == null) { throw new IoException(String.format("Could not find file to extract: %s", fileName)); } if (sourceZipFile != null) { File unzipDir = new File(LogUtils.getLogDir(), "unzip"); unzipDir.mkdirs(); File localZipFile = copyZipLocally(fileName, unzipDir); ZipFile zipFile = getNewZipFile(localZipFile); InputStream in = null; OutputStream out = null; try { String targetDirNameResolved = resolveParamsAndHeaders(targetRelativePath, inputMessage); if (targetSubDir) { targetDirNameResolved = targetDirNameResolved + "/" + FilenameUtils.removeExtension( new FileInfo(fileName, false, 0, 0).getName()); } for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) { ZipEntry entry = e.nextElement(); if (!entry.isDirectory() && (extractEmptyFiles || entry.getSize() > 0)) { String relativePathToEntry = targetDirNameResolved + "/" + entry.getName(); if (overwrite || targetDir.listFile(relativePathToEntry) == null) { info("Unzipping %s", entry.getName()); out = targetDir.getOutputStream(relativePathToEntry, false); in = zipFile.getInputStream(entry); IOUtils.copy(in, out); filePaths.add(relativePathToEntry); } else if (!overwrite) { info( "Not unzipping %s. It already exists and the override property is not enabled", entry.getName()); } } } } catch (IOException e) { throw new IoException(e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); IOUtils.closeQuietly(zipFile); FileUtils.deleteQuietly(localZipFile); } if (deleteOnComplete) { sourceDir.delete(fileName); } log(LogLevel.INFO, "Extracted %s", fileName); getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber); } } if (filePaths.size() > 0) { callback.sendTextMessage(null, filePaths); } } }