List of usage examples for org.apache.commons.io.filefilter MagicNumberFileFilter MagicNumberFileFilter
public MagicNumberFileFilter(String magicNumber)
Constructs a new MagicNumberFileFilter and associates it with the magic number to test for in files.
From source file:org.openbel.framework.core.df.cache.CacheUtil.java
/** * Copies the <tt>resource</tt> to <tt>copy</tt>. Decompression is * performed if the resource file is identified as a GZIP-encoded file. * * @param resource {@link File}, the resource to copy * @param resourceLocation {@link String}, the resource location url * @param copy {@link File}, the file to copy to * @return {@link File}, the copied file * @throws ResourceDownloadError Thrown if an IO error copying the resource *///from w ww . j a v a2 s . co m private static File copyWithDecompression(final File resource, final String resourceLocation, final File copy) throws ResourceDownloadError { GZIPInputStream gzipis = null; FileOutputStream fout = null; try { MagicNumberFileFilter mnff = new MagicNumberFileFilter(GZIP_MAGIC_NUMBER); if (mnff.accept(resource)) { gzipis = new GZIPInputStream(new FileInputStream(resource)); byte[] buffer = new byte[8192]; fout = new FileOutputStream(copy); int length; while ((length = gzipis.read(buffer, 0, 8192)) != -1) { fout.write(buffer, 0, length); } } else { copyFile(resource, copy); } } catch (IOException e) { String msg = e.getMessage(); ResourceDownloadError r = new ResourceDownloadError(resourceLocation, msg); r.initCause(e); throw r; } finally { // clean up all I/O resources closeQuietly(fout); closeQuietly(gzipis); } return copy; }