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.lee.sdk.utils.NetUtils.java

/**
 * ?server?Content-Encoding?inputstream.content-encodinggzipGzipInputStream
 * ?inputStream/*from ww w . ja  v  a  2  s.  c  om*/
 * 
 * @param resEntity {@link HttpEntity}
 * @return InputStream or null
 * @throws IOException {@link IOException}
 */
public static InputStream getSuitableInputStream(HttpEntity resEntity) throws IOException {
    if (resEntity == null) {
        return null;
    }
    InputStream inputStream = resEntity.getContent();
    if (inputStream != null) {
        Header header = resEntity.getContentEncoding();
        if (header != null) {
            String contentEncoding = header.getValue();
            if (!TextUtils.isEmpty(contentEncoding) && contentEncoding.toLowerCase().indexOf("gzip") > -1) {
                inputStream = new GZIPInputStream(inputStream);
            }
        }
    }
    return inputStream;
}

From source file:hudson.model.UsageStatisticsTest.java

/**
 * Makes sure that the stat data can be decrypted safely.
 *//*from ww  w . j  a  v  a  2s. c  o  m*/
public void testRoundtrip() throws Exception {
    // key pair for testing
    String privateKey = "30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001028180784deaacdea8bd31f2d44578601954be3f714b93c2d977dbd76efb8f71303e249ad12dbeb2d2a1192a1d7923a6010768d7e06a3597b3df83de1d5688eb0f0e58c76070eddd696682730c93890dc727564c65dc8416bfbde5aad4eb7a97ed923efb55a291daf3c00810c0e43851298472fd539aab355af8cedcf1e9a0cbead661024100c498375102b068806c71dec838dc8dfa5624fb8a524a49cffadc19d10689a8c9c26db514faba6f96e50a605122abd3c9af16e82f2b7565f384528c9f31ea5947024100aceafd31d7f4872a873c7e5fe88f20c2fb086a053c6970026b3ce364768e2033100efb1ad8f2010fe53454a29decedc23a8a0c8df347742b1f13e11bd3a284b9024100931321470cd0f6cd24d4278bf8e61f9d69b6ef2bf3163a944aa340f91c7ffdf33aeea22b18cc43514af6714a21bb148d6cdca14530a8fa65acd7a8f62bfc9b5f024067452059f8438dc61466488336fce3f00ec483ad04db638dce45daf850e5a8cd5635dc39b87f2fab32940247ec5167ddabe06e870858104500967ac687aa73e102407e3b7997503e18d8d0f094d5e0bd5d57cb93cb39a2fc42cec1ea9a1562786438b61139e45813204d72c919f5397e139ad051d98e4d0f8a06d237f42c0d8440fb";
    String publicKey = "30819f300d06092a864886f70d010101050003818d003081890281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001";

    String data = new UsageStatistics(publicKey).getStatData();
    System.out.println(data);

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PrivateKey priv = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Util.fromHexString(privateKey)));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, priv);

    byte[] cipherText = Base64.decode(data.toCharArray());
    InputStreamReader r = new InputStreamReader(
            new GZIPInputStream(
                    new CombinedCipherInputStream(new ByteArrayInputStream(cipherText), cipher, "AES", 1024)),
            "UTF-8");
    JSONObject o = JSONObject.fromObject(IOUtils.toString(r));
    System.out.println(o);
    assertEquals(1, o.getInt("stat"));
}

From source file:hr.fer.zemris.vhdllab.platform.remoting.HttpClientRequestExecutor.java

@Override
protected InputStream decorateInputStream(InputStream is) throws IOException {
    return new GZIPInputStream(is);
}

From source file:di.uniba.it.tee2.wiki.WikipediaDumpIterator.java

public WikipediaDumpIterator(File xmlFile, String encoding)
        throws XMLStreamException, FileNotFoundException, CompressorException, IOException {
    MediaWikiParserFactory parserFactory = new MediaWikiParserFactory(WikiConstants.Language.english);
    parserFactory.setTemplateParserClass(FlushTemplates.class);
    parserFactory.setShowImageText(false);
    parserFactory.setShowMathTagContent(false);
    parser = parserFactory.createParser();
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    if (xmlFile.getName().endsWith(".bz2")) {
        logger.log(Level.INFO, "Trying to open Wikipedia compress dump (bzip2)...");
        BZip2CompressorInputStream compressIS = new BZip2CompressorInputStream(
                new BufferedInputStream(new FileInputStream(xmlFile)));
        xmlStreamReader = inputFactory.createXMLStreamReader(compressIS, encoding);
    } else if (xmlFile.getName().endsWith(".gz")) {
        logger.log(Level.INFO, "Trying to open Wikipedia compress dump (gzip)...");
        GZIPInputStream compressIS = new GZIPInputStream(new BufferedInputStream(new FileInputStream(xmlFile)));
        xmlStreamReader = inputFactory.createXMLStreamReader(compressIS, encoding);
    } else {/*from  w w  w. ja v  a  2  s  .  c  o  m*/
        logger.log(Level.INFO, "Trying to open Wikipedia plain text dump...");
        xmlStreamReader = inputFactory
                .createXMLStreamReader(new BufferedInputStream(new FileInputStream(xmlFile)), encoding);
    }
}

From source file:com.mingsoft.util.proxy.Result.java

/**
 * ?gzip?//from  w ww.j a  v a 2  s.  c  o m
 * 
 * @param charSet
 *            ?
 * @return InputStreamReade;
 */
public String getContentForGzip(String charset) {
    if (httpEntity.getContentEncoding().getValue().indexOf("gzip") > -1) {
        try {
            GZIPInputStream gzipis = new GZIPInputStream(httpEntity.getContent());

            InputStreamReader isr = new InputStreamReader(gzipis, charset); // ?????
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            String tempbf;
            StringBuffer sb = new StringBuffer();
            while ((tempbf = br.readLine()) != null) {
                sb.append(tempbf);
                sb.append("\r\n");
            }
            gzipis.close();
            isr.close();
            return sb.toString();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.codemarvels.ant.aptrepotask.AptRepoTask.java

public void execute() {
    if (repoDir == null) {
        log("repoDir attribute is empty !", LogLevel.ERR.getLevel());
        throw new RuntimeException("Bad attributes for apt-repo task");
    }/*  ww  w . jav a  2 s  .c  om*/
    log("repo dir: " + repoDir);
    File repoFolder = new File(repoDir);
    if (!repoFolder.exists()) {
        repoFolder.mkdirs();
    }
    File[] files = repoFolder.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            if (pathname.getName().endsWith(FILE_DEB_EXT)) {
                return true;
            }
            return false;
        }
    });
    Packages packages = new Packages();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        PackageEntry packageEntry = new PackageEntry();
        packageEntry.setSize(file.length());
        packageEntry.setSha1(Utils.getDigest("SHA-1", file));
        packageEntry.setSha256(Utils.getDigest("SHA-256", file));
        packageEntry.setMd5sum(Utils.getDigest("MD5", file));
        String fileName = file.getName();
        packageEntry.setFilename(fileName);
        log("found deb: " + fileName);
        try {
            ArchiveInputStream control_tgz;
            ArArchiveEntry entry;
            TarArchiveEntry control_entry;
            ArchiveInputStream debStream = new ArchiveStreamFactory().createArchiveInputStream("ar",
                    new FileInputStream(file));
            while ((entry = (ArArchiveEntry) debStream.getNextEntry()) != null) {
                if (entry.getName().equals("control.tar.gz")) {
                    ControlHandler controlHandler = new ControlHandler();
                    GZIPInputStream gzipInputStream = new GZIPInputStream(debStream);
                    control_tgz = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream);
                    while ((control_entry = (TarArchiveEntry) control_tgz.getNextEntry()) != null) {
                        log("control entry: " + control_entry.getName(), LogLevel.DEBUG.getLevel());
                        if (control_entry.getName().trim().equals(CONTROL_FILE_NAME)) {
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            IOUtils.copy(control_tgz, outputStream);
                            String content_string = outputStream.toString("UTF-8");
                            outputStream.close();
                            controlHandler.setControlContent(content_string);
                            log("control cont: " + outputStream.toString("utf-8"), LogLevel.DEBUG.getLevel());
                            break;
                        }
                    }
                    control_tgz.close();
                    if (controlHandler.hasControlContent()) {
                        controlHandler.handle(packageEntry);
                    } else {
                        throw new RuntimeException("no control content found for: " + file.getName());
                    }
                    break;
                }
            }
            debStream.close();
            packages.addPackageEntry(packageEntry);
        } catch (Exception e) {
            String msg = FAILED_TO_CREATE_APT_REPO + " " + file.getName();
            log(msg, e, LogLevel.ERR.getLevel());
            throw new RuntimeException(msg, e);
        }
    }
    try {
        File packagesFile = new File(repoDir, PACKAGES_GZ);
        packagesWriter = new BufferedWriter(
                new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(packagesFile))));
        packagesWriter.write(packages.toString());
        DefaultHashes hashes = Utils.getDefaultDigests(packagesFile);
        ReleaseInfo pinfo = new ReleaseInfo(PACKAGES_GZ, packagesFile.length(), hashes);
        Release release = new Release();
        release.addInfo(pinfo);
        final File releaseFile = new File(repoDir, RELEASE);
        FileUtils.fileWrite(releaseFile, release.toString());
    } catch (IOException e) {
        throw new RuntimeException("writing files failed", e);
    } finally {
        if (packagesWriter != null) {
            try {
                packagesWriter.close();
            } catch (IOException e) {
                throw new RuntimeException("writing files failed", e);
            }
        }
    }
}

From source file:com.android.tradefed.util.TarUtil.java

/**
 * UnGZip a file: a tar.gz file will become a tar file.
 *
 * @param inputFile The {@link File} to ungzip
 * @param outputDir The directory where to put the ungzipped file.
 * @return a {@link File} pointing to the ungzipped file.
 * @throws FileNotFoundException/*  w ww  .j  ava2 s .co  m*/
 * @throws IOException
 */
public static File unGzip(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException {
    CLog.i(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
    // rename '-3' to remove the '.gz' extension.
    final File outputFile = new File(outputDir,
            inputFile.getName().substring(0, inputFile.getName().length() - 3));
    GZIPInputStream in = null;
    FileOutputStream out = null;
    try {
        in = new GZIPInputStream(new FileInputStream(inputFile));
        out = new FileOutputStream(outputFile);
        IOUtils.copy(in, out);
    } finally {
        StreamUtil.close(in);
        StreamUtil.close(out);
    }
    return outputFile;
}

From source file:com.norconex.importer.parser.impl.xfdl.XFDLParser.java

private void parse(BufferedReader reader, Writer out, ImporterMetadata metadata)
        throws IOException, ParserConfigurationException, SAXException {
    reader.mark(MAGIC_BASE64.length);/* w ww. ja  v  a2 s  .  c o m*/
    char[] signature = new char[MAGIC_BASE64.length];
    int num = reader.read(signature);
    reader.reset();
    if (num == -1) {
        return;
    }

    //--- Create XML DOM ---
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document dom = null;
    if (Arrays.equals(signature, MAGIC_BASE64)) {
        // skip first line
        reader.readLine();

        // un-encode first
        byte[] compressedContent = Base64.decodeBase64(IOUtils.toString(reader));
        // deal with compression
        InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressedContent));

        dom = docBuilder.parse(is);
        IOUtils.closeQuietly(is);
    } else {
        dom = docBuilder.parse(new InputSource(reader));
    }
    parseXML(dom, out, metadata);
}

From source file:es.ucm.fdi.util.archive.TarFormat.java

private InputStream getTarInputStream(File tarFile) throws IOException {
    boolean isTar = FileUtils.startMatches(new FileInputStream(tarFile), tarMagic, tarMagicOffset);
    boolean isGz = isTar ? false : FileUtils.startMatches(new FileInputStream(tarFile), gzMagic, 0);

    if (isTar) {/*from   w w w.j av a  2 s.  co  m*/
        return new FileInputStream(tarFile);
    } else if (isGz) {
        try (InputStream is = new GZIPInputStream(new FileInputStream(tarFile))) {
            isTar = FileUtils.startMatches(is, tarMagic, tarMagicOffset);
        }
        if (!isTar) {
            throw new IOException("Archive is Gz, but does not contain a Tar");
        }
        return new GZIPInputStream(new FileInputStream(tarFile));
    } else {
        throw new IOException("Archive is neither Tar nor Gz");
    }
}

From source file:nl.opengeogroep.filesetsync.client.SyncJobState.java

public static List<FileRecord> readCachedFileList(String name) throws IOException {
    try (GZIPInputStream gzIn = new GZIPInputStream(new FileInputStream(getFileListCacheFile(name)))) {
        return Protocol.decodeFilelist(gzIn);
    }/*w ww .j  a va2  s  . c  om*/
}