List of usage examples for org.apache.commons.compress.compressors CompressorException getMessage
public String getMessage()
From source file:it.geosolutions.tools.compress.file.test.ExtractorTest.java
@Test public void extractorGZip() throws FileNotFoundException, IOException { try {/*www . j a v a2 s. c om*/ Extractor.extractGzip(compressed, tar); if (!tar.exists()) { Assert.fail("Failed to uncompress the gzip file"); } else if (tar.length() != tarCompare.length()) { Assert.fail("Failed to compare the resulting tar file"); } } catch (CompressorException e) { LOGGER.log(Level.ERROR, e.getMessage(), e); Assert.fail(e.getLocalizedMessage()); } }
From source file:darks.codec.wrap.zip.CommonsCompress.java
/** * {@inheritDoc}// w w w . j a va2 s .c om */ @Override public void compress(InputStream input, OutputStream out) throws Exception { CompressorOutputStream cos = null; try { cos = factory.createCompressorOutputStream(type, out); byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { cos.write(buf, 0, len); } cos.flush(); } catch (CompressorException e) { throw new Exception("Fail to compress data by commons compress. Cause " + e.getMessage(), e); } finally { IoHelper.closeIO(cos); } }
From source file:darks.codec.wrap.zip.CommonsCompress.java
/** * {@inheritDoc}/*from w ww . j av a 2s. c o m*/ */ @Override public void uncompress(InputStream input, OutputStream out) throws Exception { CompressorInputStream cin = null; try { cin = factory.createCompressorInputStream(type, input); byte[] buf = new byte[1024]; int len; while ((len = cin.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } catch (CompressorException e) { throw new Exception("Fail to decompress data by commons compress. Cause " + e.getMessage(), e); } finally { IoHelper.closeIO(cin); } }
From source file:com.cloud.storage.template.VhdProcessor.java
private boolean checkCompressed(String fileName) throws IOException { FileInputStream fin = null;//w w w .j ava 2s . c o m BufferedInputStream bin = null; CompressorInputStream cin = null; try { fin = new FileInputStream(fileName); bin = new BufferedInputStream(fin); cin = new CompressorStreamFactory().createCompressorInputStream(bin); } catch (CompressorException e) { s_logger.warn(e.getMessage()); return false; } catch (FileNotFoundException e) { s_logger.warn(e.getMessage()); return false; } finally { if (cin != null) cin.close(); else if (bin != null) bin.close(); } return true; }
From source file:com.cloud.storage.template.VhdProcessor.java
protected long getTemplateVirtualSize(File file) throws IOException { byte[] currentSize = new byte[8]; byte[] cookie = new byte[8]; byte[] creatorApp = new byte[4]; BufferedInputStream fileStream = new BufferedInputStream(new FileInputStream(file)); InputStream strm = fileStream; boolean isCompressed = checkCompressed(file.getAbsolutePath()); if (isCompressed) { try {/*from w w w . ja v a 2s . c om*/ strm = new CompressorStreamFactory().createCompressorInputStream(fileStream); } catch (CompressorException e) { s_logger.info("error opening compressed VHD file " + file.getName()); return file.length(); } } try { //read the backup footer present at the top of the VHD file strm.read(cookie); if (!new String(cookie).equals(vhdIdentifierCookie)) { strm.close(); return file.length(); } long skipped = strm.skip(vhdFooterCreatorAppOffset - vhdCookieOffset); if (skipped == -1) { throw new IOException("Unexpected end-of-file"); } long read = strm.read(creatorApp); if (read == -1) { throw new IOException("Unexpected end-of-file"); } skipped = strm.skip(vhdFooterCurrentSizeOffset - vhdFooterCreatorVerOffset - vhdCookieOffset); if (skipped == -1) { throw new IOException("Unexpected end-of-file"); } read = strm.read(currentSize); if (read == -1) { throw new IOException("Unexpected end-of-file"); } } catch (IOException e) { s_logger.warn("Error reading virtual size from VHD file " + e.getMessage() + " VHD: " + file.getName()); return file.length(); } finally { if (strm != null) { strm.close(); } } return NumbersUtil.bytesToLong(currentSize); }
From source file:org.apache.logging.log4j.core.appender.rolling.RollingAppenderSizeTest.java
@Test public void testAppender() throws Exception { final Path path = Paths.get(DIR, "rollingtest.log"); if (Files.exists(path) && createOnDemand) { Assert.fail(String.format("Unexpected file: %s (%s bytes)", path, Files.getAttribute(path, "size"))); }/* w w w. j a v a 2s.com*/ for (int i = 0; i < 500; ++i) { logger.debug("This is test message number " + i); } try { Thread.sleep(100); } catch (final InterruptedException ie) { // Ignore the error. } final File dir = new File(DIR); assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0); final File[] files = dir.listFiles(); assertNotNull(files); assertThat(files, hasItemInArray(that(hasName(that(endsWith(fileExtension)))))); final FileExtension ext = FileExtension.lookup(fileExtension); if (ext == null || FileExtension.ZIP == ext || FileExtension.PACK200 == ext) { return; // Apache Commons Compress cannot deflate zip? TODO test decompressing these formats } // Stop the context to make sure all files are compressed and closed. Trying to remedy failures in CI builds. if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this); } for (final File file : files) { if (file.getName().endsWith(fileExtension)) { CompressorInputStream in = null; try (FileInputStream fis = new FileInputStream(file)) { try { in = new CompressorStreamFactory().createCompressorInputStream(ext.name().toLowerCase(), fis); } catch (final CompressorException ce) { ce.printStackTrace(); fail("Error creating intput stream from " + file.toString() + ": " + ce.getMessage()); } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); assertNotNull("No input stream for " + file.getName(), in); try { IOUtils.copy(in, baos); } catch (final Exception ex) { ex.printStackTrace(); fail("Unable to decompress " + file.getAbsolutePath()); } final String text = new String(baos.toByteArray(), Charset.defaultCharset()); final String[] lines = text.split("[\\r\\n]+"); for (final String line : lines) { assertTrue(line.contains( "DEBUG o.a.l.l.c.a.r.RollingAppenderSizeTest [main] This is test message number")); } } finally { Closer.close(in); } } } }
From source file:org.apache.logging.log4j.core.appender.rolling.RollingAppenderTempCompressedFilePatternTest.java
@Test public void testAppender() throws Exception { final File dirTmp = new File(DIR_TMP); dirTmp.mkdirs();/*from w w w.jav a 2 s .c o m*/ try (final WatchService watcher = FileSystems.getDefault().newWatchService()) { WatchKey key = dirTmp.toPath().register(watcher, StandardWatchEventKinds.ENTRY_CREATE); final List<String> messages = new ArrayList<>(); for (int i = 0; i < 500; ++i) { final String message = "This is test message number " + i; messages.add(message); logger.debug(message); if (i % 100 == 0) { Thread.sleep(500); } } if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this); } final File dir = new File(DIR); assertTrue("Directory not created", dir.exists()); final File[] files = dir.listFiles(); assertNotNull(files); int gzippedFiles = 0; for (final File file : files) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream in = null; final FileExtension ext = FileExtension.lookupForFile(file.getName()); try { try (FileInputStream fis = new FileInputStream(file)) { if (ext != null) { gzippedFiles++; try { in = new CompressorStreamFactory() .createCompressorInputStream(ext.name().toLowerCase(), fis); } catch (final CompressorException ce) { ce.printStackTrace(); fail("Error creating intput stream from " + file.toString() + ": " + ce.getMessage()); } } else { in = new FileInputStream(file); } assertNotNull("No input stream for " + file.getName(), in); try { IOUtils.copy(in, baos); } catch (final Exception ex) { ex.printStackTrace(); fail("Unable to decompress " + file.getAbsolutePath()); } } } finally { Closer.close(in); } final String text = new String(baos.toByteArray(), Charset.defaultCharset()); final String[] lines = text.split("[\\r\\n]+"); for (final String line : lines) { messages.remove(line); } } assertTrue("Log messages lost : " + messages.size(), messages.isEmpty()); assertTrue("Files not rolled : " + files.length, files.length > 2); assertTrue("Files gzipped not rolled : " + gzippedFiles, gzippedFiles > 0); int temporaryFilesCreated = 0; key = watcher.take(); for (final WatchEvent<?> event : key.pollEvents()) { final WatchEvent<Path> ev = (WatchEvent<Path>) event; final Path filename = ev.context(); if (filename.toString().endsWith(".tmp")) { temporaryFilesCreated++; } } assertTrue("No temporary file created during compression", temporaryFilesCreated > 0); assertTrue("Temporarys file created not equals to compressed files " + temporaryFilesCreated + "/" + gzippedFiles, gzippedFiles == temporaryFilesCreated); } }
From source file:org.apache.lucene.benchmark.byTask.feeds.ContentSource.java
/** * Returns an {@link InputStream} over the requested file. This method * attempts to identify the appropriate {@link InputStream} instance to return * based on the file name (e.g., if it ends with .bz2 or .bzip, return a * 'bzip' {@link InputStream}).//from w ww . ja v a2 s . com */ protected InputStream getInputStream(File file) throws IOException { // First, create a FileInputStream, as this will be required by all types. // Wrap with BufferedInputStream for better performance InputStream is = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE); String fileName = file.getName(); int idx = fileName.lastIndexOf('.'); int type = OTHER; if (idx != -1) { Integer typeInt = (Integer) extensionToType.get(fileName.substring(idx)); if (typeInt != null) { type = typeInt.intValue(); } } switch (type) { case BZIP: try { // According to BZip2CompressorInputStream's code, it reads the first // two file header chars ('B' and 'Z'). It is important to wrap the // underlying input stream with a buffered one since // Bzip2CompressorInputStream uses the read() method exclusively. is = csFactory.createCompressorInputStream("bzip2", is); } catch (CompressorException e) { IOException ioe = new IOException(e.getMessage()); ioe.initCause(e); throw ioe; } break; default: // Do nothing, stay with FileInputStream } return is; }
From source file:org.apache.tika.parser.pkg.CompressorParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { // At the end we want to close the compression stream to release // any associated resources, but the underlying document stream // should not be closed if (stream.markSupported()) { stream = new CloseShieldInputStream(stream); } else {//from w w w . j a v a2s . co m // Ensure that the stream supports the mark feature stream = new BufferedInputStream(new CloseShieldInputStream(stream)); } CompressorInputStream cis; try { CompressorParserOptions options = context.get(CompressorParserOptions.class, new CompressorParserOptions() { public boolean decompressConcatenated(Metadata metadata) { return false; } }); TikaCompressorStreamFactory factory = new TikaCompressorStreamFactory( options.decompressConcatenated(metadata), memoryLimitInKb); cis = factory.createCompressorInputStream(stream); } catch (CompressorException e) { if (e.getMessage() != null && e.getMessage().startsWith("MemoryLimitException:")) { throw new TikaMemoryLimitException(e.getMessage()); } throw new TikaException("Unable to uncompress document stream", e); } MediaType type = getMediaType(cis); if (!type.equals(MediaType.OCTET_STREAM)) { metadata.set(CONTENT_TYPE, type.toString()); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); try { Metadata entrydata = new Metadata(); String name = metadata.get(Metadata.RESOURCE_NAME_KEY); if (name != null) { if (name.endsWith(".tbz")) { name = name.substring(0, name.length() - 4) + ".tar"; } else if (name.endsWith(".tbz2")) { name = name.substring(0, name.length() - 5) + ".tar"; } else if (name.endsWith(".bz")) { name = name.substring(0, name.length() - 3); } else if (name.endsWith(".bz2")) { name = name.substring(0, name.length() - 4); } else if (name.endsWith(".xz")) { name = name.substring(0, name.length() - 3); } else if (name.endsWith(".zlib")) { name = name.substring(0, name.length() - 5); } else if (name.endsWith(".pack")) { name = name.substring(0, name.length() - 5); } else if (name.length() > 0) { name = GzipUtils.getUncompressedFilename(name); } entrydata.set(Metadata.RESOURCE_NAME_KEY, name); } // Use the delegate parser to parse the compressed document EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context); if (extractor.shouldParseEmbedded(entrydata)) { extractor.parseEmbedded(cis, xhtml, entrydata, true); } } finally { cis.close(); } xhtml.endDocument(); }
From source file:org.basinmc.maven.plugins.bsdiff.DiffMojo.java
/** * {@inheritDoc}//from w ww. j a va2s. com */ @Override public void execute() throws MojoExecutionException, MojoFailureException { final Path sourcePath = this.getSourcePath(); String fileExtension = "bsdiff." + this.compression; String fileName = this.outputFile.getName(); if (fileName.endsWith(".bsdiff")) { fileName += fileExtension.substring(6); } else { fileName += fileExtension; } File outputFile = this.outputFile.toPath().resolveSibling(fileName).toFile(); try (InputStream sourceStream = new FileInputStream(sourcePath.toFile())) { try (InputStream targetStream = new FileInputStream(this.target)) { byte[] sourceBytes = ByteStreams.toByteArray(sourceStream); byte[] targetBytes = ByteStreams.toByteArray(targetStream); if (!Files.isDirectory(outputFile.toPath().getParent())) { Files.createDirectories(outputFile.toPath().getParent()); } try (OutputStream outputStream = new FileOutputStream(outputFile)) { this.getLog().info("Generating binary diff"); DiffSettings settings = new DefaultDiffSettings(this.compression); Diff.diff(sourceBytes, targetBytes, outputStream, settings); } } } catch (CompressorException ex) { throw new MojoFailureException("Failed to compress diff: " + ex.getMessage(), ex); } catch (InvalidHeaderException ex) { throw new MojoFailureException("Invalid header: " + ex.getMessage(), ex); } catch (IOException ex) { throw new MojoFailureException("Failed to read/write source, target or output file: " + ex.getMessage(), ex); } if (this.attach) { this.getLog().info("Attaching binary diff"); if (this.classifier != null && !this.classifier.isEmpty()) { this.projectHelper.attachArtifact(this.project, fileExtension, this.classifier, outputFile); return; // TODO: Setting to attach both? } this.projectHelper.attachArtifact(this.project, fileExtension, outputFile); } }