Example usage for java.util.zip GZIPInputStream GZIPInputStream

List of usage examples for java.util.zip GZIPInputStream GZIPInputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream GZIPInputStream.

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:edu.gslis.ts.ThriftToTREC.java

/**
 * @param thriftFile//from  ww  w. j a v a2s  . co m
 */
public Map<String, String> filter(File infile, File outfile, String parser) {
    Map<String, String> results = new TreeMap<String, String>();
    try {
        InputStream in = null;

        if (infile.getName().endsWith(".gz"))
            in = new GZIPInputStream(new FileInputStream(infile));
        else if (infile.getName().endsWith("xz"))
            in = new XZInputStream(new FileInputStream(infile));
        else
            in = new FileInputStream(infile);

        TTransport inTransport = new TIOStreamTransport(new BufferedInputStream(in));
        TBinaryProtocol inProtocol = new TBinaryProtocol(inTransport);
        inTransport.open();

        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outfile, false), "UTF-8");
        try {
            Charset charset = Charset.forName("UTF-8");
            CharsetDecoder decoder = charset.newDecoder();

            // Run through items in the thrift file
            while (true) {
                final StreamItem item = new StreamItem();
                item.read(inProtocol);
                if (item.body == null || item.body.clean_visible == null) {
                    continue;
                }

                String streamId = "";
                if (item.stream_id != null) {
                    streamId = item.stream_id;
                }

                String dateTime = "";
                long epochTime = 0;
                if (item.stream_time != null && item.stream_time.zulu_timestamp != null) {
                    dateTime = item.stream_time.zulu_timestamp;
                    DateTimeFormatter dtf = ISODateTimeFormat.dateTime();
                    epochTime = dtf.parseMillis(dateTime);
                }

                String source = "";
                if (item.source != null) {
                    source = item.source;
                }

                String url = "";
                if (item.abs_url != null) {
                    url = decoder.decode(item.abs_url).toString();
                }

                Map<String, List<Sentence>> parsers = item.body.sentences;
                List<Sentence> sentenceParser = parsers.get(parser);

                String sentencesText = "";
                int sentenceNum = 0;
                if (sentenceParser != null && sentenceParser.size() > 0) {

                    for (Sentence s : sentenceParser) {
                        List<Token> tokens = s.tokens;
                        String sentence = "";
                        for (Token token : tokens) {
                            String tok = token.token;
                            sentence += tok + " ";
                        }
                        sentencesText += sentenceNum + " " + sentence + "\n";
                        sentenceNum++;
                    }
                }

                try {
                    String hourDayDir = outfile.getName().replace(".txt", "");
                    out.write("<DOC>\n");
                    out.write("<DOCNO>" + streamId + "</DOCNO>\n");
                    out.write("<SOURCE>" + source + "</SOURCE>\n");
                    out.write("<URL>" + url + "</URL>\n");
                    out.write("<DATETIME>" + dateTime + "</DATETIME>\n");
                    out.write("<HOURDAYDIR>" + hourDayDir + "</HOURDAYDIR>\n");
                    out.write("<EPOCH>" + epochTime + "</EPOCH>\n");
                    out.write("<TEXT>\n" + sentencesText + "\n</TEXT>\n");
                    out.write("</DOC>\n");
                } catch (Exception e) {
                    System.out.println("Error processing " + infile.getAbsolutePath() + " " + item.stream_id);
                    e.printStackTrace();
                }

            }
        } catch (TTransportException te) {
            if (te.getType() == TTransportException.END_OF_FILE) {
            } else {
                throw te;
            }
        }
        inTransport.close();
        out.close();

    } catch (Exception e) {
        System.out.println("Error processing " + infile.getName());
        e.printStackTrace();
    }
    return results;
}

From source file:com.googlecode.jgenhtml.JGenHtmlUtils.java

/**
 * Unzips a file./*  w  w  w  .  j  a v  a2s  .c om*/
 * @param gzippedFile A gzipped file.
 * @return The gunzipped version of the file.
 * @throws IOException If you screw up.
 */
public static File gunzip(File gzippedFile) throws IOException {
    OutputStream out = null;
    GZIPInputStream gzipInputStream = null;
    File gunzippedFile = new File(System.getProperty("java.io.tmpdir") + File.separatorChar
            + gzippedFile.getName().replace(".gz", ""));
    try {
        InputStream in = new FileInputStream(gzippedFile);
        gzipInputStream = new GZIPInputStream(in);
        out = new FileOutputStream(gunzippedFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } finally {
        if (gzipInputStream != null) {
            gzipInputStream.close();
        }
        if (out != null) {
            out.close();
        }
    }
    return gunzippedFile;
}

From source file:org.tallison.cc.CCGetter.java

private void execute(Path indexFile, Path rootDir, Path statusFile) throws IOException {

    int count = 0;
    BufferedWriter writer = Files.newBufferedWriter(statusFile, StandardCharsets.UTF_8);
    InputStream is = null;/* ww w  . j a v  a2s .co m*/
    try {
        if (indexFile.endsWith(".gz")) {
            is = new BufferedInputStream(new GZIPInputStream(Files.newInputStream(indexFile)));
        } else {
            is = new BufferedInputStream(Files.newInputStream(indexFile));
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line = reader.readLine();
            while (line != null) {
                processRow(line, rootDir, writer);
                if (++count % 100 == 0) {
                    logger.info(indexFile.getFileName().toString() + ": " + count);
                }
                line = reader.readLine();
            }

        }
    } finally {
        IOUtils.closeQuietly(is);
        try {
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:net.daboross.bukkitdev.skywars.world.providers.ProtobufStorageProvider.java

@Override
public void loadArena(final SkyArenaConfig arena, final boolean forceReload) throws IOException {
    if (forceReload || cache.containsKey(arena.getArenaName())) {
        plugin.getLogger().log(Level.WARNING, "Updating arena blocks cache for arena ''{0}''.",
                arena.getArenaName());/* w  w  w  . j a v a2  s . com*/
    }
    boolean createdNewCache = false;
    Path cachePath = plugin.getArenaPath().resolve(arena.getArenaName() + ".blocks");
    BlockStorage.BlockArea area = null;
    if (!forceReload) {
        try (InputStream inputStream = new FileInputStream(cachePath.toFile())) {
            try (GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {
                area = BlockStorage.BlockArea.parseFrom(gzipInputStream);
            }
        } catch (FileNotFoundException ignored) {
        }
    }
    if (area == null) {
        try {
            area = createCache(arena);
            createdNewCache = true;
        } catch (IllegalStateException ex1) {
            if (ex1.getMessage().contains("Origin location not listed in configuration")) {
                try (InputStream inputStream = plugin
                        .getResourceAsStream("arenas/" + arena.getArenaName() + ".blocks")) {
                    try (GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {
                        area = BlockStorage.BlockArea.parseFrom(gzipInputStream);
                    }
                    plugin.getLogger().log(Level.INFO, "Loaded pre-built blocks cache file for arena {0}.",
                            arena.getArenaName());
                } catch (FileNotFoundException ex) {
                    throw new IOException(
                            "No origin listed in configuration, but no blocks file found in SkyWars jar file either!",
                            ex);
                }
            } else {
                throw ex1;
            }
        }
        try (OutputStream outputStream = new FileOutputStream(cachePath.toFile())) {
            try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) {
                area.writeTo(gzipOutputStream);
            }
        }
    }

    // We turn the BlockStorage.BlockArea into a StoredBlockArea here, not above, because StoredBlockArea can't write to a file.
    MemoryBlockArea memoryBlockArea = new MemoryBlockArea(area);

    if (createdNewCache || arena.getChestConfiguration() == null) {
        loadChests(arena, memoryBlockArea);
    }
    cache.put(arena.getArenaName(), memoryBlockArea);
}

From source file:gov.wa.wsdot.android.wsdot.service.FerriesSchedulesSyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;/*from   ww w  . j a  v a 2 s.  co m*/
    long now = System.currentTimeMillis();
    boolean shouldUpdate = true;
    String responseString = "";
    DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy h:mm a");

    /** 
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    try {
        cursor = resolver.query(Caches.CONTENT_URI, new String[] { Caches.CACHE_LAST_UPDATED },
                Caches.CACHE_TABLE_NAME + " LIKE ?", new String[] { "ferries_schedules" }, null);

        if (cursor != null && cursor.moveToFirst()) {
            long lastUpdated = cursor.getLong(0);
            //long deltaMinutes = (now - lastUpdated) / DateUtils.MINUTE_IN_MILLIS;
            //Log.d(DEBUG_TAG, "Delta since last update is " + deltaMinutes + " min");
            shouldUpdate = (Math.abs(now - lastUpdated) > (30 * DateUtils.MINUTE_IN_MILLIS));
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // Ability to force a refresh of camera data.
    boolean forceUpdate = intent.getBooleanExtra("forceUpdate", false);

    if (shouldUpdate || forceUpdate) {
        List<Integer> starred = new ArrayList<Integer>();

        starred = getStarred();

        try {
            URL url = new URL(FERRIES_SCHEDULES_URL);
            URLConnection urlConn = url.openConnection();

            BufferedInputStream bis = new BufferedInputStream(urlConn.getInputStream());
            GZIPInputStream gzin = new GZIPInputStream(bis);
            InputStreamReader is = new InputStreamReader(gzin);
            BufferedReader in = new BufferedReader(is);

            String jsonFile = "";
            String line;

            while ((line = in.readLine()) != null)
                jsonFile += line;
            in.close();

            JSONArray items = new JSONArray(jsonFile);
            List<ContentValues> schedules = new ArrayList<ContentValues>();

            int numItems = items.length();
            for (int i = 0; i < numItems; i++) {
                JSONObject item = items.getJSONObject(i);
                ContentValues schedule = new ContentValues();
                schedule.put(FerriesSchedules.FERRIES_SCHEDULE_ID, item.getInt("RouteID"));
                schedule.put(FerriesSchedules.FERRIES_SCHEDULE_TITLE, item.getString("Description"));
                schedule.put(FerriesSchedules.FERRIES_SCHEDULE_DATE, item.getString("Date"));
                schedule.put(FerriesSchedules.FERRIES_SCHEDULE_ALERT, item.getString("RouteAlert"));
                schedule.put(FerriesSchedules.FERRIES_SCHEDULE_UPDATED, dateFormat
                        .format(new Date(Long.parseLong(item.getString("CacheDate").substring(6, 19)))));

                if (starred.contains(item.getInt("RouteID"))) {
                    schedule.put(FerriesSchedules.FERRIES_SCHEDULE_IS_STARRED, 1);
                }

                schedules.add(schedule);
            }

            // Purge existing travel times covered by incoming data
            resolver.delete(FerriesSchedules.CONTENT_URI, null, null);
            // Bulk insert all the new travel times
            resolver.bulkInsert(FerriesSchedules.CONTENT_URI,
                    schedules.toArray(new ContentValues[schedules.size()]));
            // Update the cache table with the time we did the update
            ContentValues values = new ContentValues();
            values.put(Caches.CACHE_LAST_UPDATED, System.currentTimeMillis());
            resolver.update(Caches.CONTENT_URI, values, Caches.CACHE_TABLE_NAME + "=?",
                    new String[] { "ferries_schedules" });

            responseString = "OK";
        } catch (Exception e) {
            Log.e(DEBUG_TAG, "Error: " + e.getMessage());
            responseString = e.getMessage();
        }

    } else {
        responseString = "NOP";
    }

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("gov.wa.wsdot.android.wsdot.intent.action.FERRIES_SCHEDULES_RESPONSE");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("responseString", responseString);
    sendBroadcast(broadcastIntent);

}

From source file:byps.BWire.java

/**
 * Reads a ByteBuffer from an InputStream
 * Closes the InputStream.//  w w  w .  j av a2s  .  c  om
 * @param is
 * @return
 * @throws IOException
 */
public static ByteBuffer bufferFromStream(InputStream is, Boolean gzip) throws IOException {
    if (is == null)
        return null;
    try {
        ByteBuffer ibuf = ByteBuffer.allocate(10 * 1000);

        if (gzip != null) {
            if (gzip) {
                is = new GZIPInputStream(is);
            }
        } else {
            if (!is.markSupported())
                is = new BufferedInputStream(is, 2);
            is.mark(2);
            int magic = is.read() | (is.read() << 8);
            is.reset();
            if (magic == GZIPInputStream.GZIP_MAGIC) {
                is = new GZIPInputStream(is);
            }
        }

        ReadableByteChannel rch = Channels.newChannel(is);
        while (rch.read(ibuf) != -1) {
            if (ibuf.remaining() == 0) {
                ByteBuffer nbuf = ByteBuffer.allocate(ibuf.capacity() * 2);
                ibuf.flip();
                nbuf.put(ibuf);
                ibuf = nbuf;
            }
        }

        ibuf.flip();
        return ibuf;
    } finally {
        is.close();
    }
}

From source file:com.esofthead.mycollab.shell.view.CommunitySliderContent.java

public SyndFeed getSyndFeedForUrl(String url) throws IOException, IllegalArgumentException, FeedException {
    SyndFeed feed = null;//from www .  j  a v a 2 s.c o m
    InputStream is = null;

    try {
        URLConnection openConnection = new URL(url).openConnection();
        is = new URL(url).openConnection().getInputStream();
        if ("gzip".equals(openConnection.getContentEncoding())) {
            is = new GZIPInputStream(is);
        }
        InputSource source = new InputSource(is);
        SyndFeedInput input = new SyndFeedInput();
        feed = input.build(source);

    } catch (Exception e) {
        LOG.error("Exception occured when building the feed object out of the url", e);
    } finally {
        if (is != null)
            is.close();
    }

    return feed;
}

From source file:com.comcast.cdn.traffic_control.traffic_router.neustar.data.NeustarDatabaseUpdater.java

public boolean update() {
    if (neustarDataUrl == null || neustarDataUrl.isEmpty()) {
        LOGGER.error(//from w  w w.j a v  a2  s.  com
                "Cannot get latest neustar data 'neustar.polling.url' needs to be set in environment or properties file");
        return false;
    }

    File tmpDir = createTmpDir(neustarDatabaseDirectory);
    if (tmpDir == null) {
        return false;
    }

    try (CloseableHttpResponse response = getRemoteDataResponse(URI.create(neustarDataUrl))) {
        if (response.getStatusLine().getStatusCode() == 304) {
            LOGGER.info("Neustar database unchanged at " + neustarDataUrl);
            return false;
        }

        if (response.getStatusLine().getStatusCode() != 200) {
            LOGGER.error("Failed downloading remote neustar database from " + neustarDataUrl + " "
                    + response.getStatusLine().getReasonPhrase());
        }

        if (!enoughFreeSpace(tmpDir, response, neustarDataUrl)) {
            return false;
        }

        try (GZIPInputStream gzipStream = new GZIPInputStream(response.getEntity().getContent())) {
            if (!tarExtractor.extractTo(tmpDir, gzipStream)) {
                LOGGER.error("Failed to decompress remote content from " + neustarDataUrl);
                return false;
            }
        }

        LOGGER.info("Replacing neustar files in " + neustarDatabaseDirectory.getAbsolutePath()
                + " with those in " + tmpDir.getAbsolutePath());

        if (!filesMover.updateCurrent(neustarDatabaseDirectory, tmpDir, neustarOldDatabaseDirectory)) {
            LOGGER.error("Failed updating neustar files");
            return false;
        }

        if (!verifyNewDatabase(tmpDir)) {
            return false;
        }
    } catch (Exception e) {
        LOGGER.error("Failed getting remote neustar data: " + e.getMessage());
    } finally {
        httpClient.close();

        if (!filesMover.purgeDirectory(tmpDir) || !tmpDir.delete()) {
            LOGGER.error("Failed purging temporary directory " + tmpDir.getAbsolutePath());
        }
    }

    return true;
}

From source file:edu.upf.nets.mercury.geoip.GeoIpDatabase.java

/**
 * Processes the GZIP data and loads it into the current database.
 * @param data The GeoIP data./*  w  w  w  .  jav  a2 s  .  c om*/
 */
private void process(byte[] data) throws GeoIpException {
    // Create an input stream from the byte array data.
    InputStream inputStream = new ByteArrayInputStream(data);

    try {
        // Create a GZIP input stream.
        GZIPInputStream zipStream = new GZIPInputStream(inputStream);
        // Create a temporary file for this database.
        File file = File.createTempFile(this.name, ".tmp");
        file.deleteOnExit();
        // Copy the data from.
        IOUtils.copy(zipStream, new FileOutputStream(file));
        // Location service.
        this.service = new LookupService(file);
    } catch (IOException exception) {
        throw new GeoIpException("Could not UNZIP the received data file.", exception);
    }
}

From source file:edu.umd.umiacs.clip.tools.io.GZIPFiles.java

protected static List<String> readAllLinesFromResource(String path) {
    List<String> lines = new ArrayList<>();
    try (BufferedReader bis = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(System.class.getResourceAsStream(path)),
                    UTF_8.newDecoder().onMalformedInput(IGNORE)))) {
        String line;//ww w.  j a  v a2s.  com
        while ((line = bis.readLine()) != null) {
            line = line.trim();
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return lines;
}