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:updater.builder.SoftwarePatchBuilder.java

public static void catalog(CommandLine line, Options options) throws ParseException, Exception {
    if (!line.hasOption("key")) {
        throw new Exception("Please specify the key file to use using --key");
    }/*  ww  w .ja va 2 s . c  om*/
    if (!line.hasOption("output")) {
        throw new Exception("Please specify the path to output the XML file using --output");
    }

    String[] catalogArgs = line.getOptionValues("catalog");
    String keyArg = line.getOptionValue("key");
    String outputArg = line.getOptionValue("output");

    if (catalogArgs.length != 2) {
        throw new ParseException("Wrong arguments for 'catalog', expecting 2 arguments");
    }
    if (!catalogArgs[0].equals("e") && !catalogArgs[0].equals("d")) {
        throw new ParseException("Catalog mode should be either 'e' or 'd' but not " + catalogArgs[0]);
    }

    RSAKey rsaKey = RSAKey.read(Util.readFile(new File(keyArg)));

    System.out.println("Mode: " + (catalogArgs[0].equals("e") ? "encrypt" : "decrypt"));
    System.out.println("Catalog file: " + catalogArgs[1]);
    System.out.println("Key file: " + keyArg);
    System.out.println("Output file: " + outputArg);
    System.out.println();

    File in = new File(catalogArgs[1]);
    File out = new File(outputArg);
    BigInteger mod = new BigInteger(rsaKey.getModulus());

    if (catalogArgs[0].equals("e")) {
        BigInteger privateExp = new BigInteger(rsaKey.getPrivateExponent());

        RSAPrivateKey privateKey = CommonUtil.getPrivateKey(mod, privateExp);

        // compress
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(Util.readFile(in));
        gout.finish();
        byte[] compressedData = bout.toByteArray();

        // encrypt
        int blockSize = mod.bitLength() / 8;
        byte[] encrypted = Util.rsaEncrypt(privateKey, blockSize, blockSize - 11, compressedData);

        // write to file
        Util.writeFile(out, encrypted);
    } else {
        BigInteger publicExp = new BigInteger(rsaKey.getPublicExponent());
        RSAPublicKey publicKey = CommonUtil.getPublicKey(mod, publicExp);

        // decrypt
        int blockSize = mod.bitLength() / 8;
        byte[] decrypted = Util.rsaDecrypt(publicKey, blockSize, Util.readFile(in));

        // decompress
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ByteArrayInputStream bin = new ByteArrayInputStream(decrypted);
        GZIPInputStream gin = new GZIPInputStream(bin);

        int byteRead;
        byte[] b = new byte[1024];
        while ((byteRead = gin.read(b)) != -1) {
            bout.write(b, 0, byteRead);
        }
        byte[] decompressedData = bout.toByteArray();

        // write to file
        Util.writeFile(out, decompressedData);
    }

    System.out.println("Manipulation succeed.");
}

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

public static final byte[] decompress(byte[] bytes) {
    if (bytes == null)
        throw new NullPointerException("byte[] bytes");

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[bytes.length];
    int length;/*from   ww w.  java 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:org.apache.myfaces.shared.util.StateUtils.java

public static final byte[] decompress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] bytes");
    }/*  w w w.  j  av  a  2  s. c  om*/

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[bytes.length];
    int length;

    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:org.gephi.io.importer.api.ImportUtils.java

/**
 * Uncompress a GZIP file./*from   w  w w.  jav  a 2  s  .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:backtype.storm.utils.Utils.java

public static byte[] gunzip(byte[] data) {
    try {//  w  ww.  j  a  v  a 2  s . co  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream in = new GZIPInputStream(bis);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) >= 0) {
            bos.write(buffer, 0, len);
        }
        in.close();
        bos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.alder.fotobuchconvert.ifolor.Decryptor.java

private byte[] loadCompressedBinaryData(InputStream is) throws IOException, UnsupportedEncodingException {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];

    int bytesRead;
    // load md5 sum
    MessageDigest md;/*  w  w  w .  j a va  2 s  . com*/
    try {
        md = MessageDigest.getInstance(DIGEST_MD52);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    byte[] md5 = new byte[md.getDigestLength()];
    bytesRead = is.read(md5, 0, md5.length);
    if (bytesRead != md.getDigestLength())
        throw new IOException(UNEXPECTED_END_OF_FILE);

    // load size
    bytesRead = is.read(buffer, 0, 4);
    if (bytesRead != 4)
        throw new IOException(UNEXPECTED_END_OF_FILE);
    int dataSize = (((int) buffer[3] & 0xFF) << 24) | (((int) buffer[2] & 0xFF) << 16)
            | (((int) buffer[1] & 0xFF) << 8) | ((int) buffer[0] & 0xFF);
    log.debug("uncompressed file size: " + dataSize);

    GZIPInputStream gzipIs = new GZIPInputStream(is);
    while ((bytesRead = gzipIs.read(buffer)) != -1) {
        md.update(buffer, 0, bytesRead);
        output.write(buffer, 0, bytesRead);
        if (dumpData)
            System.out.print(new String(buffer, 0, bytesRead, CHARSET_cp1252));
    }
    if (dumpData)
        System.out.println();

    buffer = md.digest();
    for (int i = 0; i < buffer.length; i++)
        if (buffer[i] != md5[i])
            throw new IOException("MD5 mismatch");

    if (output.size() != dataSize)
        throw new IOException(String.format("File size mismatch: %d instead of %d", output.size(), dataSize));

    log.debug("MD5 ok");

    return output.toByteArray();
}

From source file:ca.farrelltonsolar.classic.PVOutputUploader.java

private Bundle deserializeBundle(byte[] data) {
    Bundle bundle = null;//from ww  w .j a  va  2s  .com
    final Parcel parcel = Parcel.obtain();
    try {
        final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        final GZIPInputStream zis = new GZIPInputStream(new ByteArrayInputStream(data));
        int len = 0;
        while ((len = zis.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        zis.close();
        parcel.unmarshall(byteBuffer.toByteArray(), 0, byteBuffer.size());
        parcel.setDataPosition(0);
        bundle = parcel.readBundle();
    } catch (IOException ex) {
        Log.w(getClass().getName(), String.format("deserializeBundle failed ex: %s", ex));
        bundle = null;
    } finally {
        parcel.recycle();
    }
    return bundle;
}

From source file:org.physical_web.physicalweb.Utils.java

/**
 * Out-of-place Gunzips from src to dest.
 * @param src file containing gzipped information.
 * @param dest file to place decompressed information.
 * @return File that has decompressed information.
 *//* www . j a v a 2  s.  c  om*/
public static File gunzip(File src, File dest) {

    byte[] buffer = new byte[1024];

    try {

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

        FileOutputStream out = new FileOutputStream(dest);

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

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

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

From source file:sequential.analysis.SequentialAnalysis.java

public void unGunzipFile(String compressedFile, String decompressedFile) {
    byte[] buffer = new byte[1024];
    try {/*from  www. j  ava 2  s .c  om*/
        FileInputStream fileIn = new FileInputStream(compressedFile);
        GZIPInputStream gZIPInputStream = new GZIPInputStream(fileIn);
        FileOutputStream fileOutputStream = new FileOutputStream(decompressedFile);
        int bytes_read;
        while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, bytes_read);
        }
        gZIPInputStream.close();
        fileOutputStream.close();
        getCsvData(decompressedFile);
        System.out.println("Success!");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:org.tuckey.web.filters.urlrewriteviacontainer.WebappHttpIT.java

/**
 * inflate a gzipped inputstream and return it as a string.
 *///from   w  w w.  j a  v  a2  s.c  o  m
private String inflateGzipToString(InputStream is) throws IOException {
    GZIPInputStream gis = new GZIPInputStream(is);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    while (true) {
        int bytesRead = gis.read(buffer);
        if (bytesRead == -1)
            break;
        os.write(buffer, 0, bytesRead);
    }
    return os.toString();
}