Example usage for java.util.zip GZIPInputStream read

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

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

public static byte[] decompress(byte[] bytes) {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int length;//ww  w.j  a v a 2  s  .  c  o m

    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        byte[] moreBytes = baos.toByteArray();
        baos.close();
        bais.close();
        gis.close();
        baos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:com.sap.nwcloudmanager.api.LogAPI.java

public static void downloadLog(final Context context, final String appId, final String logName,
        final DownloadLogResponseHandler downloadLogResponseHandler) {

    NetWeaverCloudConfig config = NetWeaverCloudConfig.getInstance();
    getHttpClient().setBasicAuth(config.getUsername(), config.getPassword());
    String[] contentTypes = { "application/x-gzip" };
    getHttpClient().get("https://monitoring." + config.getHost() + "/log/api_basic/logs/" + config.getAccount()
            + "/" + appId + "/web/" + logName, null, new BinaryHttpResponseHandler(contentTypes) {

                /*//w w  w  .j a va 2s. c  om
                 * (non-Javadoc)
                 * 
                 * @see
                 * com.loopj.android.http.BinaryHttpResponseHandler#onSuccess(byte
                 * [])
                 */
                @Override
                public void onSuccess(byte[] arg0) {

                    GZIPInputStream gzis = null;

                    try {
                        gzis = new GZIPInputStream(new ByteArrayInputStream(arg0));

                        StringBuilder string = new StringBuilder();
                        byte[] data = new byte[4028];
                        int bytesRead;
                        while ((bytesRead = gzis.read(data)) != -1) {
                            string.append(new String(data, 0, bytesRead));
                        }

                        File downloadsFile = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
                        if (downloadsFile.exists()) {
                            downloadsFile = new File(
                                    context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                                            .getAbsolutePath() + "/" + logName);
                            FileOutputStream fos = new FileOutputStream(downloadsFile);
                            fos.write(string.toString().getBytes());
                            fos.close();
                        }

                        downloadLogResponseHandler.onSuccess(downloadsFile.getAbsolutePath());

                    } catch (IOException e) {
                        Log.e(e.getMessage());
                    } finally {
                        try {
                            if (gzis != null) {
                                gzis.close();
                            }
                        } catch (IOException e) {
                        }
                    }
                }

                @Override
                public void onFailure(Throwable arg0) {
                    downloadLogResponseHandler.onFailure(arg0, arg0.getMessage());
                }

            });
}

From source file:uk.ac.ebi.embl.api.validation.helper.FileUtils.java

public static String getdeCompressedFile(String file) throws IOException {
    if (file == null) {
        return null;
    }/* w  w w.j  a  va2 s  .  com*/
    File zipFile = new File(file);
    if (!zipFile.exists()) {
        return null;
    }

    if (!zipFile.getName().matches("^.+\\.gz$")) {
        return file;
    }
    byte[] buffer = new byte[1024];
    GZIPInputStream zis = new GZIPInputStream(new FileInputStream(zipFile));
    File tempFile = new File(zipFile.getParent() + File.separator
            + zipFile.getName().substring(0, zipFile.getName().lastIndexOf(".gz")));
    if (!tempFile.exists()) {
        tempFile.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(tempFile);
    int len;
    while ((len = zis.read(buffer)) > 0) {
        fos.write(buffer, 0, len);
    }

    fos.close();

    zis.close();

    return tempFile.getAbsolutePath();
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Decompresses the GZIP file back to its original form.
 *
 * @param aGzipPathFileName The GZIP file name.
 * @param anOutputPathFileName The output file name.
 *
 * @throws IOException Related to opening the file streams and
 * related read/write operations./*from  www  .  j a v a 2  s . co  m*/
 */
public static void ungzipFile(String aGzipPathFileName, String anOutputPathFileName) throws IOException {
    int byteCount;

    FileInputStream fileInputStream = new FileInputStream(aGzipPathFileName);
    GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
    FileOutputStream fileOutputStream = new FileOutputStream(anOutputPathFileName);

    byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    byteCount = gzipInputStream.read(ioBuf);
    while (byteCount > 0) {
        fileOutputStream.write(ioBuf, 0, byteCount);
        byteCount = gzipInputStream.read(ioBuf);
    }

    fileOutputStream.close();
    gzipInputStream.close();
}

From source file:it.marcoberri.mbfasturl.action.Commons.java

/**
 * //from w w w . j a va 2  s  .  c  om
 * @param log
 * @param path
 * @param url
 */
public static void downloadGeoIp2(Logger log, String path, String url) {

    long ts = System.currentTimeMillis();
    final String action = "downloadGeoIp2";

    try {
        org.apache.commons.io.FileUtils.forceMkdir(new File(path));
    } catch (final IOException ex) {
    }

    final String fileName = url.substring(url.lastIndexOf("/"), url.length());

    HttpUtil.downloadData(url, path + "/" + fileName);

    try {
        final File f = new File(path + "/" + fileName);

        final GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(f));

        final byte[] buffer = new byte[1024];
        final FileOutputStream out = new FileOutputStream(
                new File(path + "/" + fileName.replaceAll(".gz", "")));
        int len;
        while ((len = gzis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        gzis.close();
        out.close();

        writeEventLog(action, true, "File size:" + f.length(), "Download Resources",
                (System.currentTimeMillis() - ts));

    } catch (final IOException ex) {

        log.fatal(ex);
        writeEventLog(action, false, ex.getMessage(), "Download Resources", (System.currentTimeMillis() - ts));
    }

}

From source file:eu.diacron.crawlservice.app.Util.java

private static String unzipWarcFile(String input_gzip_file) {

    String output_file = input_gzip_file.split(".gz")[0];

    byte[] buffer = new byte[1024];

    try {//from  www.j a  va2  s. c  o m

        GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(input_gzip_file));

        FileOutputStream out = new FileOutputStream(output_file);

        int len;
        while ((len = gzis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        gzis.close();
        out.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return output_file;
}

From source file:org.apache.helix.tools.ZkGrep.java

/**
 * Gunzip a file//from w  w  w  .  j  a  v a 2  s. co m
 * @param zipFile
 * @return
 */
static File gunzip(File zipFile) {
    File outputFile = new File(stripGzSuffix(zipFile.getAbsolutePath()));

    byte[] buffer = new byte[1024];

    try {

        GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(zipFile));
        FileOutputStream out = new FileOutputStream(outputFile);

        int len;
        while ((len = gzis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        gzis.close();
        out.close();

        return outputFile;
    } catch (IOException e) {
        LOG.error("fail to gunzip file: " + zipFile, e);
    }

    return null;
}

From source file:com.navercorp.pinpoint.web.filter.Base64.java

public static byte[] decode(String s, int options) {
    byte[] bytes;
    try {/*ww w .jav  a  2s .  c o m*/
        bytes = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException var21) {
        bytes = s.getBytes();
    }

    bytes = decode(bytes, 0, bytes.length, options);
    if (bytes != null && bytes.length >= 4) {
        int head = bytes[0] & 255 | bytes[1] << 8 & '\uff00';
        if (35615 == head) {
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            try {
                gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));
                byte[] buffer = new byte[2048];

                int length;
                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                }

                bytes = baos.toByteArray();
            } catch (IOException var22) {
                ;
            } finally {
                try {
                    baos.close();
                } catch (Exception var20) {
                    LOG.error("error closing ByteArrayOutputStream", var20);
                }

                if (gzis != null) {
                    try {
                        gzis.close();
                    } catch (Exception var19) {
                        LOG.error("error closing GZIPInputStream", var19);
                    }
                }

            }
        }
    }

    return bytes;
}

From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java

/**
 * @param infileName/*  w w  w  .j a va 2s .c om*/
 * @param outFileName
 * @param changeListener
 * @return
 */
public static boolean uncompressFile(final String infileName, final String outFileName) {
    File file = new File(infileName);
    if (file.exists()) {
        long fileSize = file.length();
        if (fileSize > 0) {
            try {
                GZIPInputStream fis = new GZIPInputStream(new FileInputStream(infileName));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFileName));

                byte[] bytes = new byte[BUFFER_SIZE * 10];

                while (true) {
                    int len = fis.read(bytes);
                    if (len > 0) {
                        fos.write(bytes, 0, len);
                    } else {
                        break;
                    }
                }

                fis.close();
                fos.close();

                return true;

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            // file doesn't exist
        }
    } else {
        // file doesn't exist
    }
    return false;
}

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

/**
 * Unzips a file.//from   w  ww .j av  a2  s.  c  o m
 * @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;
}