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:com.metamx.druid.loading.S3DataSegmentPuller.java

@Override
public void getSegmentFiles(final DataSegment segment, final File outDir) throws SegmentLoadingException {
    final S3Coords s3Coords = new S3Coords(segment);

    log.info("Pulling index at path[%s] to outDir[%s]", s3Coords, outDir);

    if (!isObjectInBucket(s3Coords)) {
        throw new SegmentLoadingException("IndexFile[%s] does not exist.", s3Coords);
    }//from   w  w  w.  jav  a2  s.c o m

    if (!outDir.exists()) {
        outDir.mkdirs();
    }

    if (!outDir.isDirectory()) {
        throw new ISE("outDir[%s] must be a directory.", outDir);
    }

    try {
        S3Utils.retryS3Operation(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                long startTime = System.currentTimeMillis();
                S3Object s3Obj = null;

                try {
                    s3Obj = s3Client.getObject(s3Coords.bucket, s3Coords.path);

                    InputStream in = null;
                    try {
                        in = s3Obj.getDataInputStream();
                        final String key = s3Obj.getKey();
                        if (key.endsWith(".zip")) {
                            CompressionUtils.unzip(in, outDir);
                        } else if (key.endsWith(".gz")) {
                            final File outFile = new File(outDir, toFilename(key, ".gz"));
                            ByteStreams.copy(new GZIPInputStream(in), Files.newOutputStreamSupplier(outFile));
                        } else {
                            ByteStreams.copy(in,
                                    Files.newOutputStreamSupplier(new File(outDir, toFilename(key, ""))));
                        }
                        log.info("Pull of file[%s] completed in %,d millis", s3Obj,
                                System.currentTimeMillis() - startTime);
                        return null;
                    } catch (IOException e) {
                        FileUtils.deleteDirectory(outDir);
                        throw new IOException(String.format("Problem decompressing object[%s]", s3Obj), e);
                    } finally {
                        Closeables.closeQuietly(in);
                    }
                } finally {
                    S3Utils.closeStreamsQuietly(s3Obj);
                }
            }
        });
    } catch (Exception e) {
        throw new SegmentLoadingException(e, e.getMessage());
    }
}

From source file:android.tether.system.WebserviceTask.java

public boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
    if (android.os.Environment.getExternalStorageState()
            .equals(android.os.Environment.MEDIA_MOUNTED) == false) {
        return false;
    }/*from  w  w  w.j av  a  2  s . c  o  m*/
    File bluetoothDir = new File(BLUETOOTH_FILEPATH);
    if (bluetoothDir.exists() == false) {
        bluetoothDir.mkdirs();
    }
    if (this.downloadFile(downloadFileUrl, "", destinationFilename) == true) {
        try {
            FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
            FileInputStream fis = new FileInputStream(destinationFilename);
            GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
            int count;
            byte buf[] = new byte[8192];
            while ((count = gzin.read(buf, 0, 8192)) != -1) {
                //System.out.write(x);
                out.write(buf, 0, count);
            }
            out.flush();
            out.close();
            gzin.close();
            File inputFile = new File(destinationFilename);
            inputFile.delete();
        } catch (IOException e) {
            return false;
        }
        return true;
    } else
        return false;
}

From source file:fr.eoidb.util.AndroidUrlDownloader.java

@Override
public InputStream urlToInputStream(Context context, String url) throws DownloadException {

    if (!isNetworkAvailable(context)) {
        throw new DownloadException("No internet connection!");
    }/*from   w  w  w .  j  a v a 2  s .com*/

    try {
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used.
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        HttpClient httpclient = new DefaultHttpClient(httpParameters);

        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Accept-Encoding", "gzip");

        HttpResponse response;

        response = httpclient.execute(httpget);
        Log.v(LOG_TAG, response.getStatusLine().toString());

        HttpEntity entity = response.getEntity();

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            String message = "Error " + statusCode + " while retrieving url " + url;
            Log.w("AndroidUrlDownloader", message);
            throw new DownloadException(message);
        }

        if (entity != null) {

            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                Log.v(LOG_TAG, "Accepting gzip for url : " + url);
                instream = new GZIPInputStream(instream);
            }

            return instream;
        }

    } catch (IllegalStateException e) {
        throw new DownloadException(e);
    } catch (IOException e) {
        throw new DownloadException(e);
    }

    return null;
}

From source file:com.zimbra.cs.service.util.ItemDataFile.java

public static void list(InputStream is, Set<MailItem.Type> types, String cset, PrintStream os)
        throws IOException {
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    TarEntry te;/*from  w  w  w  . j a  v  a2s . com*/
    TarInputStream tis = new TarInputStream(new GZIPInputStream(is), cset == null ? "UTF-8" : cset);

    os.format("%-13s %17s %10s %6s %s\n", "TYPE", "DATE", "SIZE", "METASZ", "PATH");
    try {
        TarEntry idEntry = null;

        while ((te = tis.getNextEntry()) != null) {
            if (te.getName().endsWith(".meta")) {
                if (idEntry != null && !skip(types, MailItem.Type.of((byte) idEntry.getMajorDeviceId()))) {
                    os.format("%-13s %17s %10s %6d %s\n", idEntry.getGroupName(),
                            df.format(idEntry.getModTime()), 0, idEntry.getSize(),
                            idEntry.getName().substring(0, idEntry.getName().indexOf(".meta")));
                }
                idEntry = te;
            } else {
                if (!skip(types, MailItem.Type.of((byte) te.getMajorDeviceId()))) {
                    os.format("%-13s %17s %10s %6d %s\n", te.getGroupName(), df.format(te.getModTime()),
                            te.getSize(), idEntry == null ? 0 : idEntry.getSize(), te.getName());
                }
                idEntry = null;
            }
        }
        if (idEntry != null && !skip(types, MailItem.Type.of((byte) idEntry.getMajorDeviceId()))) {
            os.format("%-13s %17s %10s %6d %s\n", idEntry.getGroupName(), df.format(idEntry.getModTime()), 0,
                    idEntry.getSize(), idEntry.getName().substring(0, idEntry.getName().indexOf(".meta")));
        }
    } finally {
        tis.close();
    }
}

From source file:com.alibaba.openapi.client.rpc.AbstractHttpResponseParser.java

public void parseResponse(HttpResponse httpResponse, InputStream istream, Response response,
        InvokeContext invokeContext) {/*from   w  w w  .  j  a v  a 2  s  . com*/
    try {
        if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(response.getEncoding())) {
            LoggerHelper.getClientLogger().finer(" translate InputStream to GZIPInputStream ");
            istream = new GZIPInputStream(istream);
        }
        final Reader reader = new InputStreamReader(istream, response.getCharset());
        LoggerHelper.getClientLogger().finer("Response status code :" + response.getStatusCode());
        if (response.getStatusCode() == 200) {
            Object result = parseBody(reader, invokeContext.getResultType());
            response.setResult(result);
        } else {
            Throwable exception = buildException(reader, response.getStatusCode());
            response.setException(exception);
        }
        //Todo ???
    } catch (IOException e) {
        response.setException(e);
    } catch (RuntimeException e) {
        response.setException(e);
    } catch (ParseException e) {
        response.setException(e);
    }
}

From source file:de.escidoc.core.test.examples.RetrieveCompressedExamplesIT.java

@Test
public void testRetrieveCompressedExampleOrganizationalUnits() throws Exception {

    for (int i = 0; i < EXAMPLE_OU_IDS.length; ++i) {

        String ou = handleXmlResult(ouClient.retrieve(EXAMPLE_OU_IDS[i]));

        String response = null;/*from ww  w. j a v  a 2  s  .  c o m*/

        String url = "http://localhost:8080" + Constants.ORGANIZATIONAL_UNIT_BASE_URI + "/" + EXAMPLE_OU_IDS[i];

        HttpClient client = new HttpClient();

        try {
            if (PropertiesProvider.getInstance().getProperty("http.proxyHost") != null
                    && PropertiesProvider.getInstance().getProperty("http.proxyPort") != null) {
                ProxyHost proxyHost = new ProxyHost(
                        PropertiesProvider.getInstance().getProperty("http.proxyHost"),
                        Integer.parseInt(PropertiesProvider.getInstance().getProperty("http.proxyPort")));

                client.getHostConfiguration().setProxyHost(proxyHost);
            }
        } catch (final Exception e) {
            throw new RuntimeException("[ClientBase] Error occured loading properties! " + e.getMessage(), e);
        }

        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Accept-Encoding", "gzip");

        InputStream responseBody = null;
        try {

            int statusCode = client.executeMethod(getMethod);

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException("getMethod failed:" + getMethod.getStatusLine());
            }
            responseBody = new GZIPInputStream(getMethod.getResponseBodyAsStream());

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(responseBody, getMethod.getResponseCharSet()));
            StringBuffer result = new StringBuffer();

            char[] buffer = new char[4 * 1024];
            int charsRead;
            while ((charsRead = bufferedReader.read(buffer)) != -1) {
                result.append(buffer, 0, charsRead);
            }

            response = result.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occured in retrieving compressed example! " + e.getMessage(), e);
        }

        assertXmlEquals("Compressed document not the same like the uncompressed one", ou, response);
    }
}

From source file:net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.ZipReadTask.java

/**
 * @see java.lang.Runnable#run()/*from www.  j a v  a 2 s.c  om*/
 */
public void run() {

    // Update task status
    setStatus(TaskStatus.PROCESSING);
    logger.info("Started opening compressed file " + file);

    try {

        // Name of the uncompressed file
        String newName = file.getName();
        if (newName.toLowerCase().endsWith(".zip") || newName.toLowerCase().endsWith(".gz")) {
            newName = FilenameUtils.removeExtension(newName);
        }

        // Create decompressing stream
        FileInputStream fis = new FileInputStream(file);
        InputStream is;
        long decompressedSize = 0;
        switch (fileType) {
        case ZIP:
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry entry = zis.getNextEntry();
            newName = entry.getName();
            decompressedSize = entry.getSize();
            if (decompressedSize < 0)
                decompressedSize = 0;
            is = zis;
            break;
        case GZIP:
            is = new GZIPInputStream(fis);
            decompressedSize = (long) (file.length() * 1.5); // Ballpark a
                                                             // decompressedFile
                                                             // size so the
                                                             // GUI can show
                                                             // progress
            if (decompressedSize < 0)
                decompressedSize = 0;
            break;
        default:
            setErrorMessage("Cannot decompress file type: " + fileType);
            setStatus(TaskStatus.ERROR);
            return;
        }

        tmpDir = Files.createTempDir();
        tmpFile = new File(tmpDir, newName);
        logger.finest("Decompressing to file " + tmpFile);
        tmpFile.deleteOnExit();
        tmpDir.deleteOnExit();
        FileOutputStream ous = new FileOutputStream(tmpFile);

        // Decompress the contents
        copy = new StreamCopy();
        copy.copy(is, ous, decompressedSize);

        // Close the streams
        is.close();
        ous.close();

        if (isCanceled())
            return;

        // Find the type of the decompressed file
        RawDataFileType fileType = RawDataFileTypeDetector.detectDataFileType(tmpFile);
        logger.finest("File " + tmpFile + " type detected as " + fileType);

        if (fileType == null) {
            setErrorMessage("Could not determine the file type of file " + newName);
            setStatus(TaskStatus.ERROR);
            return;
        }

        // Run the import module on the decompressed file
        RawDataFileWriter newMZmineFile = MZmineCore.createNewFile(newName);
        decompressedOpeningTask = RawDataImportModule.createOpeningTask(fileType, project, tmpFile,
                newMZmineFile);

        if (decompressedOpeningTask == null) {
            setErrorMessage("File type " + fileType + " of file " + newName + " is not supported.");
            setStatus(TaskStatus.ERROR);
            return;
        }

        // Run the underlying task
        decompressedOpeningTask.run();

        // Delete the temporary folder
        tmpFile.delete();
        tmpDir.delete();

        if (isCanceled())
            return;

    } catch (Throwable e) {
        logger.log(Level.SEVERE, "Could not open file " + file.getPath(), e);
        setErrorMessage(ExceptionUtils.exceptionToString(e));
        setStatus(TaskStatus.ERROR);
        return;
    }

    logger.info("Finished opening compressed file " + file);

    // Update task status
    setStatus(TaskStatus.FINISHED);

}

From source file:com.linkedin.thirdeye.hadoop.util.ThirdeyeAvroUtils.java

private static DataFileStream<GenericRecord> getAvroReader(Path avroFile) throws IOException {
    FileSystem fs = FileSystem.get(new Configuration());
    if (avroFile.getName().endsWith("gz")) {
        return new DataFileStream<GenericRecord>(new GZIPInputStream(fs.open(avroFile)),
                new GenericDatumReader<GenericRecord>());
    } else {/*from  w w  w.  j a v  a 2s .co  m*/
        return new DataFileStream<GenericRecord>(fs.open(avroFile), new GenericDatumReader<GenericRecord>());
    }
}

From source file:CASUAL.communicationstools.heimdall.odin.OdinFile.java

/**
 * Opens an Odin file and verifies MD5sum
 *
 * @param odinFile file to be opened and verified
 * @throws CorruptOdinFileException Odin checks did not pass
 * @throws FileNotFoundException {@inheritDoc}
 * @throws IOException {@inheritDoc}/*from  www .  j  av a 2s.  c o m*/
 * @throws NoSuchAlgorithmException {@inheritDoc}
 * @throws org.apache.commons.compress.archivers.ArchiveException
 * {@inheritDoc}
 */
public OdinFile(File odinFile) throws FileNotFoundException, IOException, NoSuchAlgorithmException,
        CorruptOdinFileException, ArchiveException {
    this.odinFile = odinFile;
    this.odinStream = new BufferedInputStream(new FileInputStream(odinFile));

    String name = odinFile.getName();
    if (name.endsWith("tar")) {
        actualMd5 = "";
        type = 0;
    } else if (name.endsWith("tar.md5")) {
        actualMd5 = getActualAndExpectedOdinMd5();
        if (!expectedMd5.equals(actualMd5)) {
            throw new CorruptOdinFileException(odinFile.getCanonicalPath());
        }
        System.out.println("verified file " + odinFile.getCanonicalPath());
        type = 1;
    } else if (name.endsWith("tar.gz.md5")) {
        actualMd5 = getActualAndExpectedOdinMd5();
        if (!expectedMd5.equals(actualMd5)) {
            throw new CorruptOdinFileException(odinFile.getCanonicalPath());
        }
        System.out.println("verified file " + odinFile.getCanonicalPath());
        type = 2;
    } else {//(name.endsWith("tar.gz")) {
        actualMd5 = "";
        type = 3;
    }
    //open a tar.gz stream for tar.gz and tar.md5.gz
    if (type == 2 || type == 3) {
        GZIPInputStream gzis = new GZIPInputStream(odinStream);
        tarStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", gzis);
        //open a tar stream for .tar and tar.md5
    } else {
        tarStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar",
                odinStream);
    }

}

From source file:com.zotoh.core.io.StreamUte.java

/**
 * @param ins/*w w  w.  j a  v a2s.  co m*/
 * @return
 * @throws IOException
 */
public static InputStream gunzip(InputStream ins) throws IOException {
    return ins == null ? null : new GZIPInputStream(ins);
}