/** * @param uri The URI of the file to identify * @param request The Identification Request * @throws CommandExecutionException When an exception happens during execution * @throws CommandExecutionException When an exception happens during archive file input/output */ public final void identify(final URI uri, final IdentificationRequest request) throws CommandExecutionException { final String newPath = makeContainerURI("gzip", request.getFileName()); setSlash1(""); final URI newUri = URI.create(GzipUtils.getUncompressedFilename(uri.toString())); final RequestIdentifier identifier = new RequestIdentifier(newUri); final RequestMetaData metaData = new RequestMetaData(SIZE, TIME, uri.getPath()); final GZipIdentificationRequest gzRequest = new GZipIdentificationRequest(metaData, identifier, getTmpDir()); GzipCompressorInputStream gzin = null; try { gzin = new GzipCompressorInputStream(new FileInputStream(request.getSourceFile()), true); expandContainer(gzRequest, gzin, newPath); } catch (IOException ioe) { System.err.println(ioe + " (" + newPath + ")"); // continue after corrupt archive } finally { if (gzin != null) { try { gzin.close(); } catch (IOException ioe) { throw new CommandExecutionException(ioe.getMessage(), ioe); } } } }
/** * Downloads the given file specified via url to the given canonicalDestination. * * @param urlSource String * @param urlDestination String * @throws Exception */ @Override public void downloadFile(String urlSource, String urlDestination) throws Exception { // sanity check if (urlSource == null || urlSource.length() == 0 || urlDestination == null || urlDestination.length() == 0) { throw new IllegalArgumentException( "downloadFile(): urlSource or urlDestination argument is null..."); } // URLs for given parameters URL source = new URL(urlSource); URL destination = new URL(urlDestination); // we have a compressed file if (GzipUtils.isCompressedFilename(urlSource)) { // downlod to temp destination File tempDestinationFile = org.apache.commons.io.FileUtils.getFile( org.apache.commons.io.FileUtils.getTempDirectory(), new File(source.getFile()).getName()); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), " + urlSource + ", this may take a while..."); } org.apache.commons.io.FileUtils.copyURLToFile(source, tempDestinationFile); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), gunzip: we have compressed file, decompressing..."); } // decompress the file gunzip(tempDestinationFile.getCanonicalPath()); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), gunzip complete..."); } // move temp/decompressed file to final destination File destinationFile = new File(destination.getFile()); if (destinationFile.exists()) { org.apache.commons.io.FileUtils.forceDelete(destinationFile); } org.apache.commons.io.FileUtils.moveFile( org.apache.commons.io.FileUtils.getFile( GzipUtils.getUncompressedFilename(tempDestinationFile.getCanonicalPath())), destinationFile); // lets cleanup after ourselves - remove compressed file tempDestinationFile.delete(); } // uncompressed file, download directry to urlDestination else { if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), " + urlSource + ", this may take a while..."); } org.apache.commons.io.FileUtils.copyURLToFile( source, org.apache.commons.io.FileUtils.getFile(destination.getFile())); } }
/** * Helper function to gunzip file. gzipFile param is canonical path. * * @param gzipFile String */ private static void gunzip(String gzipFile) throws Exception { // setup our gzip inputs tream FileOutputStream fos = null; String outFilePath = GzipUtils.getUncompressedFilename(gzipFile); GZIPInputStream gis = new GZIPInputStream(new FileInputStream(gzipFile)); try { // unzip into file less the .gz fos = new FileOutputStream(outFilePath); IOUtils.copy(gis, fos); } finally { // close up our streams IOUtils.closeQuietly(gis); if (fos != null) IOUtils.closeQuietly(fos); } }