List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorInputStream read
public int read(byte b[]) throws IOException
b. From source file:com.graphhopper.tools.Bzip2.java
public static void main(String[] args) throws IOException { if (args.length == 0) { throw new IllegalArgumentException("You need to specify the bz2 file!"); }//from w ww.j a v a2s. com String fromFile = args[0]; if (!fromFile.endsWith(".bz2")) { throw new IllegalArgumentException("You need to specify a bz2 file! But was:" + fromFile); } String toFile = Helper.pruneFileEnd(fromFile); FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); try { final byte[] buffer = new byte[1024 * 8]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } } finally { out.close(); bzIn.close(); } }
From source file:com.yahoo.ycsb.db.RedisClient.java
public static String decompress(String st) { if (compress != null && compress.equals("y")) { if (compressAlgo != null && (compressAlgo.equals("lz4") || compressAlgo.equals("lz4hc"))) { try { int split = st.indexOf('|'); final int decompressedLength = Integer.parseInt(st.substring(0, split)); LZ4FastDecompressor decompressor = lz4factory.fastDecompressor(); byte[] restored = new byte[decompressedLength]; byte[] compressed = st.substring(split + 1, st.length()).getBytes("ISO-8859-1"); decompressor.decompress(compressed, 0, restored, 0, decompressedLength); String ret = new String(restored, "ISO-8859-1"); return ret; } catch (Exception e) { e.printStackTrace();/*from www . j av a2 s . c o m*/ } } else if (compressAlgo != null && compressAlgo.equals("bzip2")) { try { InputStream in = new StringBufferInputStream(st); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); byte[] data = st.getBytes("ISO-8859-1"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int n = 0; while (-1 != (n = bzIn.read(data))) { baos.write(data, 0, n); } bzIn.close(); return baos.toString(); } catch (Exception e) { e.printStackTrace(); } } else if (compressAlgo != null && compressAlgo.equals("snappy")) { try { byte[] uncompressed = Snappy.uncompress(st.getBytes("ISO-8859-1")); String ret = new String(uncompressed, "ISO-8859-1"); return ret; } catch (Exception e) { e.printStackTrace(); } } } return st; }
From source file:aarddict.Volume.java
static String decompressBz2(byte[] bytes) throws IOException { BZip2CompressorInputStream in = new BZip2CompressorInputStream(new ByteArrayInputStream(bytes)); int n = 0;/*from ww w . jav a 2 s . co m*/ ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 5); byte[] buf = new byte[1024]; try { while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } } finally { in.close(); out.close(); } return utf8(out.toByteArray()); }
From source file:adams.core.io.Bzip2Utils.java
/** * Decompresses the specified archive.//from w ww. ja v a2 s. com * <br><br> * See <a href="https://commons.apache.org/compress/examples.html" target="_blank">Apache commons/compress</a>. * * @param archiveFile the archive file to decompress * @param buffer the buffer size to use * @param outputFile the destination file * @return the error message, null if everything OK */ public static String decompress(File archiveFile, int buffer, File outputFile) { String result; byte[] buf; int len; FileInputStream fis; BZip2CompressorInputStream in; BufferedOutputStream out; String msg; in = null; out = null; fis = null; result = null; try { // does file already exist? if (outputFile.exists()) System.err.println("WARNING: overwriting '" + outputFile + "'!"); // create GZIP file buf = new byte[buffer]; fis = new FileInputStream(archiveFile.getAbsolutePath()); in = new BZip2CompressorInputStream(new BufferedInputStream(fis)); out = new BufferedOutputStream(new FileOutputStream(outputFile), buffer); // Transfer bytes from the file to the GZIP file while ((len = in.read(buf)) > 0) out.write(buf, 0, len); } catch (Exception e) { msg = "Failed to decompress '" + archiveFile + "': "; System.err.println(msg); e.printStackTrace(); result = msg + e; } finally { FileUtils.closeQuietly(fis); FileUtils.closeQuietly(in); FileUtils.closeQuietly(out); } return result; }
From source file:example.TestLineRecordReader.java
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip) throws IOException { int MAX_DATA_SIZE = 1024 * 1024; byte[] data = new byte[MAX_DATA_SIZE]; FileInputStream fis = new FileInputStream(testFileUrl.getFile()); int count;/* ww w . j av a 2 s .c om*/ if (bzip) { BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis); count = bzIn.read(data); bzIn.close(); } else { count = fis.read(data); } fis.close(); assertTrue("Test file data too big for buffer", count < data.length); return new String(data, 0, count, "UTF-8").split("\n"); }
From source file:com.github.nethad.clustermeister.integration.JPPFTestNode.java
private File extractBzip2(File bzip2File, File tarFile) throws FileNotFoundException, IOException { FileInputStream fin = null;/*from w w w . ja v a2 s. c o m*/ BZip2CompressorInputStream bzIn = null; try { fin = new FileInputStream(bzip2File); BufferedInputStream in = new BufferedInputStream(fin); FileOutputStream out = new FileOutputStream(tarFile); bzIn = new BZip2CompressorInputStream(in); final byte[] buffer = new byte[8192]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); bzIn.close(); } finally { try { fin.close(); } catch (IOException ex) { // throw new RuntimeException(ex); } try { bzIn.close(); } catch (IOException ex) { // throw new RuntimeException(ex); } } return tarFile; }
From source file:edu.monash.merc.system.remote.HttpHpaFileGetter.java
public boolean importHPAXMLBZ2(String remoteFile, String localFile) { //use httpclient to get the remote file HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(remoteFile); BZip2CompressorInputStream bzIn = null; FileOutputStream fos = null;//from w w w . ja va 2 s .c o m try { HttpResponse response = httpClient.execute(httpGet); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream in = entity.getContent(); bzIn = new BZip2CompressorInputStream(in); File aFile = new File(localFile); fos = new FileOutputStream(aFile); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0; while ((len = bzIn.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.flush(); } } else { throw new DMRemoteException("can't get the file from " + remoteFile); } } catch (Exception ex) { throw new DMRemoteException(ex); } finally { try { if (fos != null) { fos.close(); } if (bzIn != null) { bzIn.close(); } httpClient.getConnectionManager().shutdown(); } catch (Exception e) { //ignore whatever caught } } return true; }
From source file:org.ala.harvester.WikipediaImageHarvester.java
@Override public void start(int infosourceId, int timeGap) throws Exception { //name index/*from w w w . j a va 2 s. c o m*/ CBIndexSearch nameIndex = new CBIndexSearch("/data/lucene/namematching"); //download the images file from DBPedia if (!downloaded) { System.out.println("Downloading NT triple dump from DBPedia..." + dbpediaImagesFile); File bzipFile = new File("/data/images_en.nt.bz2"); FileUtils.copyURLToFile(new URL(dbpediaImagesFile), bzipFile); System.out.println("Downloaded."); //decompress System.out.println("Decompressing....."); FileInputStream in = new FileInputStream(bzipFile); FileOutputStream out = new FileOutputStream("/data/images_en.nt"); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); final byte[] buffer = new byte[1048576]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); bzIn.close(); System.out.println("Decompressed."); } //iterate through each line BufferedReader reader = new BufferedReader(new FileReader("/data/images_en.nt")); String line = reader.readLine(); while (line != null) { // System.out.println("LINE: " + line); if (line.contains("depiction")) { String[] parts = line.split(" "); String dbpediaUrl = parts[0].replaceAll(">", "").replaceAll("<", ""); String resourceName = parts[0].substring(parts[0].lastIndexOf('/') + 1); String nameToMatch = resourceName.replace(">", "").replaceAll("_", " ").trim(); // println(nameToMatch) try { //name must be a bionomial or trinomial if (nameToMatch.contains("-") || nameToMatch.contains(" ")) { //only match things that look like binomials or trinomials NameSearchResult nsr = null; try { nameIndex.searchForRecord(nameToMatch, null); } catch (HomonymException he) { } if (nsr == null) { //search for common name nsr = nameIndex.searchForCommonName(nameToMatch); } if (nsr != null && (RankType.SPECIES.equals(nsr.getRank()) || RankType.SUBSPECIES.equals(nsr.getRank())) && nsr.getLsid() != null && nsr.getLsid().contains("biodiversity.org.au") && nsr.getRankClassification().getScientificName() != null && nsr.getRankClassification().getScientificName().contains(" ")) { //validate the match String dbpediaPage = WebUtils.getUrlContentAsString(dbpediaUrl); if (dbpediaPage.contains("http://dbpedia.org/ontology/genus") || dbpediaPage.contains("http://dbpedia.org/ontology/species") || dbpediaPage.contains("http://dbpedia.org/property/genus") || dbpediaPage.contains("http://dbpedia.org/property/species") || dbpediaPage.contains("http://dbpedia.org/property/binomial") || dbpediaPage.contains("http://dbpedia.org/ontology/phylum")) { System.out.println("URL: " + dbpediaUrl + ", matched string: " + nameToMatch + ", to " + nsr.getRank().toString() + ": " + nsr.getRankClassification().getScientificName()); //TODO //download image full res image //download wikipedia page for image e.g. http://en.wikipedia.org/wiki/File:Kangur.rudy.drs.jpg //retrieve creator, rights, licence, date //save to repository String wikiPageUrl = getWikiPageUrl(dbpediaPage); if (wikiPageUrl != null && !"".equals(wikiPageUrl)) { List<String> imagePageUrlList = getImagePageUrlList(wikiPageUrl); harvestImagePages(imagePageUrlList, nsr, infosourceId); } } else { System.out.println( "False positive for " + "http://en.wikipedia.org/wiki/" + resourceName); } } } } catch (Exception e) { e.printStackTrace(); } } line = reader.readLine(); } System.out.println("Finished."); }
From source file:org.gephi.desktop.importer.DesktopImportControllerUI.java
/** * Uncompress a Bzip2 file.//ww w . ja va 2 s.c om */ private static File getBzipFile(FileObject in, File out, boolean isTar) throws IOException { // Stream buffer final int BUFF_SIZE = 8192; final byte[] buffer = new byte[BUFF_SIZE]; BZip2CompressorInputStream inputStream = null; FileOutputStream outStream = null; try { FileInputStream is = new FileInputStream(in.getPath()); inputStream = new BZip2CompressorInputStream(is); outStream = new FileOutputStream(out.getAbsolutePath()); if (isTar) { // Read Tar header int remainingBytes = readTarHeader(inputStream); // Read content ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE); byte[] tmpCache = new byte[BUFF_SIZE]; int nRead, nGet; while ((nRead = inputStream.read(tmpCache)) != -1) { if (nRead == 0) { continue; } bb.put(tmpCache); bb.position(0); bb.limit(nRead); while (bb.hasRemaining() && remainingBytes > 0) { nGet = Math.min(bb.remaining(), BUFF_SIZE); nGet = Math.min(nGet, remainingBytes); bb.get(buffer, 0, nGet); outStream.write(buffer, 0, nGet); remainingBytes -= nGet; } bb.clear(); } } else { int len; while ((len = inputStream.read(buffer)) > 0) { outStream.write(buffer, 0, len); } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } finally { if (inputStream != null) { inputStream.close(); } if (outStream != null) { outStream.close(); } } return out; }
From source file:org.gephi.io.importer.api.ImportUtils.java
/** * Uncompress a Bzip2 file.//from w w w.j a v a 2s . c o m */ public static File getBzipFile(FileObject in, File out, boolean isTar) throws IOException { // Stream buffer final int BUFF_SIZE = 8192; final byte[] buffer = new byte[BUFF_SIZE]; BZip2CompressorInputStream inputStream = null; FileOutputStream outStream = null; try { FileInputStream is = new FileInputStream(in.getPath()); inputStream = new BZip2CompressorInputStream(is); outStream = new FileOutputStream(out.getAbsolutePath()); if (isTar) { // Read Tar header int remainingBytes = readTarHeader(inputStream); // Read content ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE); byte[] tmpCache = new byte[BUFF_SIZE]; int nRead, nGet; while ((nRead = inputStream.read(tmpCache)) != -1) { if (nRead == 0) { continue; } bb.put(tmpCache); bb.position(0); bb.limit(nRead); while (bb.hasRemaining() && remainingBytes > 0) { nGet = Math.min(bb.remaining(), BUFF_SIZE); nGet = Math.min(nGet, remainingBytes); bb.get(buffer, 0, nGet); outStream.write(buffer, 0, nGet); remainingBytes -= nGet; } bb.clear(); } } else { int len; while ((len = inputStream.read(buffer)) > 0) { outStream.write(buffer, 0, len); } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } finally { if (inputStream != null) { inputStream.close(); } if (outStream != null) { outStream.close(); } } return out; }