List of usage examples for java.util.zip GZIPInputStream close
public void close() throws IOException
From source file:net.sf.ehcache.constructs.web.PageInfoTest.java
private byte[] ungzip3(final byte[] gzip) throws IOException { int size = 0; int counter = 0; GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzip)); int bytesRead = 0; byte[] buffer = new byte[500000]; byte[] tempBuffer = new byte[4096]; counter = 0;/* w w w . j a v a 2s . c om*/ while (bytesRead != -1) { bytesRead = gzipInputStream.read(tempBuffer); if (bytesRead != -1) { System.arraycopy(tempBuffer, 0, buffer, counter, bytesRead); counter += bytesRead; } } gzipInputStream.close(); size = counter; byte[] unzipped = new byte[size]; System.arraycopy(buffer, 0, unzipped, 0, counter); return unzipped; }
From source file:net.sf.ehcache.constructs.web.PageInfoTest.java
/** * A high performance implementation, although not as fast as gunzip3. * gunzips 100000 of ungzipped content in 9ms on the reference machine. * It does not use a fixed size buffer and is therefore suitable for arbitrary * length arrays.//from w w w . j a va 2s. com * @param gzipped * @return * @throws IOException */ public byte[] ungzip1(final byte[] gzipped) throws IOException { final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length); final byte[] buffer = new byte[4096]; int bytesRead = 0; while (bytesRead != -1) { bytesRead = inputStream.read(buffer, 0, 4096); if (bytesRead != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); } } byte[] ungzipped = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); return ungzipped; }
From source file:org.gephi.io.importer.api.ImportUtils.java
/** * Uncompress a GZIP file./*from w w w . j a v a2s . co m*/ */ public static File getGzFile(FileObject in, File out, boolean isTar) throws IOException { // Stream buffer final int BUFF_SIZE = 8192; final byte[] buffer = new byte[BUFF_SIZE]; GZIPInputStream inputStream = null; FileOutputStream outStream = null; try { inputStream = new GZIPInputStream(new FileInputStream(in.getPath())); outStream = new FileOutputStream(out); 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:grails.plugin.cache.web.PageInfo.java
/** * A highly performant ungzip implementation. Do not refactor this without * taking new timings. See ElementTest for timings * * @param gzipped the gzipped content// w w w . j a v a 2 s. c om * @return an ungzipped byte[] * @throws IOException */ protected byte[] ungzip(final byte[] gzipped) throws IOException { GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length); byte[] buffer = new byte[FOUR_KB]; int bytesRead = 0; while (bytesRead != -1) { bytesRead = inputStream.read(buffer, 0, FOUR_KB); if (bytesRead != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); } } byte[] ungzipped = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); return ungzipped; }
From source file:edu.wisc.ssec.mcidasv.data.cyclone.AtcfStormDataSource.java
/** * _more_/*from w w w . j a v a 2s. c om*/ * * @param stormInfo * _more_ * @param tracks * _more_ * @param trackFile * _more_ * @param waysToUse * _more_ * @param throwError * _more_ * * * @return _more_ * @throws Exception * _more_ */ private boolean readTracks(StormInfo stormInfo, StormTrackCollection tracks, String trackFile, Hashtable<String, Boolean> waysToUse, boolean throwError) throws Exception { long t1 = System.currentTimeMillis(); byte[] bytes = readFile(trackFile, true); long t2 = System.currentTimeMillis(); // System.err.println("read time:" + (t2 - t1)); boolean isZip = trackFile.endsWith(".gz"); if ((bytes == null) && isZip) { String withoutGZ = trackFile.substring(0, trackFile.length() - 3); bytes = readFile(withoutGZ, true); isZip = false; } if (bytes == null) { if (throwError) { throw new BadDataException("Unable to read track file:" + trackFile); } return false; } if (isZip) { GZIPInputStream zin = new GZIPInputStream(new ByteArrayInputStream(bytes)); bytes = IOUtil.readBytes(zin); zin.close(); } GregorianCalendar convertCal = new GregorianCalendar(DateUtil.TIMEZONE_GMT); convertCal.clear(); String trackData = new String(bytes); List lines = StringUtil.split(trackData, "\n", true, true); SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHH"); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); Hashtable trackMap = new Hashtable(); Real altReal = new Real(RealType.Altitude, 0); // System.err.println("obs:" + lines.size()); /* * Hashtable okWays = new Hashtable(); okWays.put(WAY_CARQ, ""); * okWays.put(WAY_WRNG, ""); okWays.put(WAY_BEST, ""); okWays.put("ETA", * ""); okWays.put("NGX", ""); okWays.put("BAMS", ""); */ Hashtable seenDate = new Hashtable(); initParams(); int xcnt = 0; for (int i = 0; i < lines.size(); i++) { String line = (String) lines.get(i); if (i == 0) { // System.err.println(line); } List toks = StringUtil.split(line, ",", true); /* * System.err.println(toks.size() + " " + BASEIDX); * if(toks.size()<BASEIDX-1) { System.err.println("bad line:" + * line); continue; } else { System.err.println("good line:" + * line); } */ // BASIN,CY,YYYYMMDDHH,TECHNUM,TECH,TAU,LatN/S,LonE/W,VMAX,MSLP,TY,RAD,WINDCODE,RAD1,RAD2,RAD3,RAD4,RADP,RRP,MRD,GUSTS,EYE,SUBREGION,MAXSEAS,INITIALS,DIR,SPEED,STORMNAME,DEPTH,SEAS,SEASCODE,SEAS1,SEAS2,SEAS3,SEAS4 // AL, 01, 2007050612, , BEST, 0, 355N, 740W, 35, 1012, EX, 34, NEQ, // 0, 0, 0, 120, // AL, 01, 2007050812, 01, CARQ, -24, 316N, 723W, 55, 0, DB, 34, // AAA, 0, 0, 0, 0, String dateString = (String) toks.get(IDX_YYYYMMDDHH); String wayString = (String) toks.get(IDX_TECH); // if (okWays.get(wayString) == null) { // continue; // } boolean isBest = wayString.equals(WAY_BEST); boolean isWarning = wayString.equals(WAY_WRNG); boolean isCarq = wayString.equals(WAY_CARQ); int category = ((IDX_TY < toks.size()) ? getCategory((String) toks.get(IDX_TY)) : CATEGORY_XX); if (category != CATEGORY_XX) { // System.err.println("cat:" + category); } String fhour = (String) toks.get(IDX_TAU); int forecastHour = new Integer(fhour).intValue(); // A hack - we've seen some atfc files that have a 5 character // forecast hour // right padded with "00", eg., 01200 if ((fhour.length() == 5) && (forecastHour > 100)) { forecastHour = forecastHour / 100; } if (isWarning || isCarq) { forecastHour = -forecastHour; } // Check for unique dates for this way String dttmkey = wayString + "_" + dateString + "_" + forecastHour; if (seenDate.get(dttmkey) != null) { continue; } seenDate.put(dttmkey, dttmkey); Date dttm = fmt.parse(dateString); convertCal.setTime(dttm); String key; Way way = getWay(wayString, null); if (!isBest && (waysToUse != null) && (waysToUse.size() > 0) && (waysToUse.get(wayString) == null)) { continue; } if (isBest) { key = wayString; } else { key = wayString + "_" + dateString; convertCal.add(Calendar.HOUR_OF_DAY, forecastHour); } dttm = convertCal.getTime(); StormTrack track = (StormTrack) trackMap.get(key); if (track == null) { way = (isBest ? Way.OBSERVATION : way); track = new StormTrack(stormInfo, addWay(way), new DateTime(dttm), obsParams); trackMap.put(key, track); tracks.addTrack(track); } String latString = (String) toks.get(IDX_LAT); String lonString = (String) toks.get(IDX_LON); String t = latString + " " + lonString; boolean south = latString.endsWith("S"); boolean west = lonString.endsWith("W"); double latitude = Double.parseDouble(latString.substring(0, latString.length() - 1)) / 10.0; double longitude = Double.parseDouble(lonString.substring(0, lonString.length() - 1)) / 10.0; if (south) { latitude = -latitude; } if (west) { longitude = -longitude; } EarthLocation elt = new EarthLocationLite(new Real(RealType.Latitude, latitude), new Real(RealType.Longitude, longitude), altReal); List<Real> attributes = new ArrayList<Real>(); double windspeed = ((IDX_VMAX < toks.size()) ? getDouble((String) toks.get(IDX_VMAX)) : Double.NaN); double pressure = ((IDX_MSLP < toks.size()) ? getDouble((String) toks.get(IDX_MSLP)) : Double.NaN); attributes.add(PARAM_STORMCATEGORY.getReal((double) category)); attributes.add(PARAM_MINPRESSURE.getReal(pressure)); attributes.add(PARAM_MAXWINDSPEED_KTS.getReal(windspeed)); StormTrackPoint stp = new StormTrackPoint(elt, new DateTime(dttm), forecastHour, attributes); track.addPoint(stp); } return true; }
From source file:ucar.unidata.data.storm.AtcfStormDataSource.java
/** * _more_//from w w w .j a v a 2 s. com * * @param stormInfo _more_ * @param tracks _more_ * @param trackFile _more_ * @param waysToUse _more_ * @param throwError _more_ * * * @return _more_ * @throws Exception _more_ */ private boolean readTracks(StormInfo stormInfo, StormTrackCollection tracks, String trackFile, Hashtable<String, Boolean> waysToUse, boolean throwError) throws Exception { long t1 = System.currentTimeMillis(); byte[] bytes = readFile(trackFile, true); long t2 = System.currentTimeMillis(); // System.err.println("read time:" + (t2 - t1)); boolean isZip = trackFile.endsWith(".gz"); if ((bytes == null) && isZip) { String withoutGZ = trackFile.substring(0, trackFile.length() - 3); bytes = readFile(withoutGZ, true); isZip = false; } if (bytes == null) { if (throwError) { throw new BadDataException("Unable to read track file:" + trackFile); } return false; } if (isZip) { GZIPInputStream zin = new GZIPInputStream(new ByteArrayInputStream(bytes)); bytes = IOUtil.readBytes(zin); zin.close(); } GregorianCalendar convertCal = new GregorianCalendar(DateUtil.TIMEZONE_GMT); convertCal.clear(); String trackData = new String(bytes); List lines = StringUtil.split(trackData, "\n", true, true); SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHH"); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); Hashtable trackMap = new Hashtable(); Real altReal = new Real(RealType.Altitude, 0); // System.err.println("obs:" + lines.size()); /* Hashtable okWays = new Hashtable(); okWays.put(WAY_CARQ, ""); okWays.put(WAY_WRNG, ""); okWays.put(WAY_BEST, ""); okWays.put("ETA", ""); okWays.put("NGX", ""); okWays.put("BAMS", "");*/ Hashtable seenDate = new Hashtable(); initParams(); int xcnt = 0; // check if the tech num is included // some actf files do not have tech num for some reasom. List toks1 = StringUtil.split(lines.get(1), ",", true); String techNum = (String) toks1.get(IDX_TECHNUM); if (techNum.length() > 0 && !techNum.matches("\\d+")) { IDX_TECH = IDX_TECH - 1; IDX_TAU = IDX_TAU - 1; IDX_LAT = IDX_LAT - 1; IDX_LON = IDX_LON - 1; IDX_VMAX = IDX_VMAX - 1; IDX_MSLP = IDX_MSLP - 1; IDX_TY = IDX_TY - 1; } for (int i = 0; i < lines.size(); i++) { String line = (String) lines.get(i); if (i == 0) { // System.err.println(line); } List toks = StringUtil.split(line, ",", true); /* System.err.println(toks.size() + " " + BASEIDX); if(toks.size()<BASEIDX-1) { System.err.println("bad line:" + line); continue; } else { System.err.println("good line:" + line); } */ //BASIN,CY,YYYYMMDDHH,TECHNUM,TECH,TAU,LatN/S,LonE/W,VMAX,MSLP,TY,RAD,WINDCODE,RAD1,RAD2,RAD3,RAD4,RADP,RRP,MRD,GUSTS,EYE,SUBREGION,MAXSEAS,INITIALS,DIR,SPEED,STORMNAME,DEPTH,SEAS,SEASCODE,SEAS1,SEAS2,SEAS3,SEAS4 //AL, 01, 2007050612, , BEST, 0, 355N, 740W, 35, 1012, EX, 34, NEQ, 0, 0, 0, 120, //AL, 01, 2007050812, 01, CARQ, -24, 316N, 723W, 55, 0, DB, 34, AAA, 0, 0, 0, 0, String dateString = (String) toks.get(IDX_YYYYMMDDHH); String wayString = (String) toks.get(IDX_TECH); // if (okWays.get(wayString) == null) { // continue; // } boolean isBest = wayString.equals(WAY_BEST); boolean isWarning = wayString.equals(WAY_WRNG); boolean isCarq = wayString.equals(WAY_CARQ); int category = ((IDX_TY < toks.size()) ? getCategory((String) toks.get(IDX_TY)) : CATEGORY_XX); if (category != CATEGORY_XX) { // System.err.println("cat:" + category); } String fhour = (String) toks.get(IDX_TAU); int forecastHour = new Integer(fhour).intValue(); //A hack - we've seen some atfc files that have a 5 character forecast hour //right padded with "00", eg., 01200 if ((fhour.length() == 5) && (forecastHour > 100)) { forecastHour = forecastHour / 100; } if (isWarning || isCarq) { forecastHour = -forecastHour; } //Check for unique dates for this way String dttmkey = wayString + "_" + dateString + "_" + forecastHour; if (seenDate.get(dttmkey) != null) { continue; } seenDate.put(dttmkey, dttmkey); Date dttm = fmt.parse(dateString); convertCal.setTime(dttm); String key; Way way = getWay(wayString, null); if (!isBest && (waysToUse != null) && (waysToUse.size() > 0) && (waysToUse.get(wayString) == null)) { continue; } if (isBest) { key = wayString; } else { key = wayString + "_" + dateString; convertCal.add(Calendar.HOUR_OF_DAY, forecastHour); } dttm = convertCal.getTime(); StormTrack track = (StormTrack) trackMap.get(key); if (track == null) { way = (isBest ? Way.OBSERVATION : way); track = new StormTrack(stormInfo, addWay(way), new DateTime(dttm), obsParams); trackMap.put(key, track); tracks.addTrack(track); } String latString = (String) toks.get(IDX_LAT); String lonString = (String) toks.get(IDX_LON); String t = latString + " " + lonString; boolean south = latString.endsWith("S"); boolean west = lonString.endsWith("W"); double latitude = Double.parseDouble(latString.substring(0, latString.length() - 1)) / 10.0; double longitude = Double.parseDouble(lonString.substring(0, lonString.length() - 1)) / 10.0; if (south) { latitude = -latitude; } if (west) { longitude = -longitude; } EarthLocation elt = new EarthLocationLite(new Real(RealType.Latitude, latitude), new Real(RealType.Longitude, longitude), altReal); List<Real> attributes = new ArrayList<Real>(); double windspeed = ((IDX_VMAX < toks.size()) ? getDouble((String) toks.get(IDX_VMAX)) : Double.NaN); double pressure = ((IDX_MSLP < toks.size()) ? getDouble((String) toks.get(IDX_MSLP)) : Double.NaN); attributes.add(PARAM_STORMCATEGORY.getReal((double) category)); attributes.add(PARAM_MINPRESSURE.getReal(pressure)); attributes.add(PARAM_MAXWINDSPEED_KTS.getReal(windspeed)); StormTrackPoint stp = new StormTrackPoint(elt, new DateTime(dttm), forecastHour, attributes); track.addPoint(stp); } return true; }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * /*ww w . j a v a 2 s .c o m*/ * Extract a GZip file to a tar * @param in_file * the input bz2 file to extract * @param out_file * the output tar file to extract to */ public static void extractGzip(File in_file, File out_file) throws CompressorException { FileOutputStream out = null; GZIPInputStream zIn = null; FileInputStream fis = null; BufferedInputStream bis = null; try { out = new FileOutputStream(out_file); fis = new FileInputStream(in_file); bis = new BufferedInputStream(fis, Conf.getBufferSize()); zIn = new GZIPInputStream(bis); byte[] buffer = new byte[Conf.getBufferSize()]; int count = 0; while ((count = zIn.read(buffer, 0, Conf.getBufferSize())) != -1) { out.write(buffer, 0, count); } } catch (IOException ioe) { String msg = "Problem uncompressing Gzip " + ioe.getMessage() + " "; throw new CompressorException(msg + in_file.getAbsolutePath()); } finally { try { if (bis != null) bis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (fis != null) fis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (out != null) out.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (zIn != null) zIn.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } } }
From source file:org.opensextant.util.TextUtils.java
/** * * @param gzData// w w w . ja v a 2 s . c o m * byte array containing gzipped buffer * @param charset * character set decoding for text * @return buffer of uncompressed, decoded string * @throws IOException * on error with decompression or text encoding */ public static String uncompress(byte[] gzData, String charset) throws IOException { GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzData)); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[ONEKB]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); } gzipInputStream.close(); out.close(); return new String(out.toByteArray(), charset); }
From source file:com.panet.imeta.core.xml.XMLHandler.java
/** * Convert a XML encoded binary string back to binary format * // w ww .j av a2s. c o m * @param string * the (Byte64/GZip) encoded string * @return the decoded binary (byte[]) object * @throws IOException * In case there is a decoding error */ public static byte[] stringToBinary(String string) throws IOException { byte[] bytes; if (string == null) { bytes = new byte[] {}; } else { bytes = Base64.decodeBase64(string.getBytes()); } if (bytes.length > 0) { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gzip = new GZIPInputStream(bais); BufferedInputStream bi = new BufferedInputStream(gzip); byte[] result = new byte[] {}; byte[] extra = new byte[1000000]; int nrExtra = bi.read(extra); while (nrExtra >= 0) { // add it to bytes... // int newSize = result.length + nrExtra; byte[] tmp = new byte[newSize]; for (int i = 0; i < result.length; i++) tmp[i] = result[i]; for (int i = 0; i < nrExtra; i++) tmp[result.length + i] = extra[i]; // change the result result = tmp; nrExtra = bi.read(extra); } bytes = result; gzip.close(); } return bytes; }
From source file:org.opensextant.xtext.collectors.ArchiveNavigator.java
/** * * @param theFile/*from ww w . j a v a 2s . c om*/ * @param fname * @return TAR file path for result. * @throws IOException on I/O failure */ private File gunzipAsTAR(File theFile, String fname) throws IOException { GZIPInputStream gzipInputStream = null; OutputStream out = null; try { gzipInputStream = new GZIPInputStream(new FileInputStream(theFile)); // TODO: more testing on this particular case: gunzip *.gz *.tgz *.tar.gz -- a mix of tar and gunzip String outFilename = getWorkingDir() + '/' + fname + ".tar"; File outFile = new File(outFilename); out = new BufferedOutputStream(new FileOutputStream(outFilename)); byte[] buf = new byte[1024]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); } return outFile; } finally { gzipInputStream.close(); if (out != null) { out.close(); } } }