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.hupo.psi.mi.psicquic.ws.IndexBasedPsicquicRestServiceTest.java

@Test
public void testGetByQuery_bin() throws Exception {
    ResponseImpl response = (ResponseImpl) service.getByQuery("FANCD1", "tab25-bin", "0",
            String.valueOf(Integer.MAX_VALUE), "n");

    PsicquicStreamingOutput pso = ((GenericEntity<PsicquicStreamingOutput>) response.getEntity()).getEntity();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    pso.write(baos);/*from   w  w w.  j a  v a2  s .c  o  m*/

    // gunzip the output
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    GZIPInputStream gzipInputStream = new GZIPInputStream(bais);

    ByteArrayOutputStream mitabOut = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0)
        mitabOut.write(buf, 0, len);

    gzipInputStream.close();
    mitabOut.close();

    Assert.assertEquals(12, mitabOut.toString().split("\n").length);
}

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

@Override
public void readFields(DataInput in) throws IOException {
    super.readFields(in);
    int length = in.readInt();
    byte[] serializedData = new byte[length];
    in.readFully(serializedData);//from   w w  w. j av  a  2 s  . c  om
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
    GZIPInputStream gzis = new GZIPInputStream(bais);

    byte[] buffer = new byte[8];
    gzis.read(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    int width = bbuffer.getInt();
    int height = bbuffer.getInt();
    // Reallocate memory only if needed
    if (width != this.getWidth() || height != this.getHeight())
        frequencies = new float[width][height];
    buffer = new byte[getHeight() * 4];
    for (int x = 0; x < getWidth(); x++) {
        int size = 0;
        while (size < buffer.length) {
            size += gzis.read(buffer, size, buffer.length - size);
        }
        bbuffer = ByteBuffer.wrap(buffer);
        for (int y = 0; y < getHeight(); y++) {
            frequencies[x][y] = bbuffer.getFloat();
        }
    }
}

From source file:org.gephi.desktop.importer.DesktopImportControllerUI.java

/**
 * Uncompress a GZIP file.//  w ww.jav  a 2  s.c  om
 */
private 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:com.android.tradefed.util.FileUtil.java

public static void extractGzip(File tarGzipFile, File destDir, String destName)
        throws FileNotFoundException, IOException, ArchiveException {
    GZIPInputStream zipIn = null;
    BufferedOutputStream buffOut = null;
    try {/*w w w. j  a va  2s . c  o  m*/
        File destUnzipFile = new File(destDir.getAbsolutePath(), destName);
        zipIn = new GZIPInputStream(new FileInputStream(tarGzipFile));
        buffOut = new BufferedOutputStream(new FileOutputStream(destUnzipFile));
        int b = 0;
        byte[] buf = new byte[8192];
        while ((b = zipIn.read(buf)) != -1) {
            buffOut.write(buf, 0, b);
        }
    } finally {
        if (zipIn != null)
            zipIn.close();
        if (buffOut != null)
            buffOut.close();
    }
}

From source file:org.onebusaway.webapp.actions.admin.bundles.SyncBundleAction.java

private void unzipBundle(String tmpDir, String bundleFileName) throws IOException {
    byte[] buffer = new byte[1024];

    GZIPInputStream zipIn = new GZIPInputStream(new FileInputStream(tmpDir + File.separator + bundleFileName));
    FileOutputStream out = new FileOutputStream(tmpDir + File.separator + "unzippedBundle");

    int len;//from   w  w w  .  j  av  a2s  . c  o m
    while ((len = zipIn.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }

    zipIn.close();
    out.close();

    // Now to untar the unzipped file
    File tarFile = new File(tmpDir + File.separator + "unzippedBundle");
    File untarredFile = new File(tmpDir + File.separator + "untarredBundle");
    //File untarredFile = new File(tmpDir);
    try {
        List<File> fileList = (new FileUtility()).unTar(tarFile, untarredFile);
    } catch (ArchiveException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return;
}

From source file:com.iisigroup.cap.utils.CapSerialization.java

/**
 * decompress byte array data with GZIP.
 * // w ww.  j a  v a  2  s. co  m
 * @param input
 *            the input compressed data
 * @return the decompress data
 * @throws java.io.IOException
 */
public byte[] decompress(byte[] input) throws java.io.IOException {

    byte[] buf = new byte[2048];
    byte[] result = null;
    java.io.ByteArrayInputStream bain = null;
    ByteArrayOutputStream baout = null;
    GZIPInputStream gzipin = null;
    try {
        bain = new java.io.ByteArrayInputStream(input);
        gzipin = new GZIPInputStream(bain);
        baout = new ByteArrayOutputStream();
        int size;
        while ((size = gzipin.read(buf)) != -1) {
            baout.write(buf, 0, size);
        }
        result = baout.toByteArray();
        return result;
    } finally {
        IOUtils.closeQuietly(bain);
        IOUtils.closeQuietly(baout);
        IOUtils.closeQuietly(gzipin);
    }

}

From source file:org.owasp.dependencycheck.data.update.CpeUpdater.java

/**
 * Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified.
 *
 * @param file the archive file// w w  w .j  ava 2  s .com
 * @throws FileNotFoundException thrown if the file does not exist
 * @throws IOException thrown if there is an error extracting the file.
 */
private void extractGzip(File file) throws FileNotFoundException, IOException {
    //TODO - move this to a util class as it is duplicative of (copy of) code in the DownloadTask
    final String originalPath = file.getPath();
    final File gzip = new File(originalPath + ".gz");
    if (gzip.isFile() && !gzip.delete()) {
        gzip.deleteOnExit();
    }
    if (!file.renameTo(gzip)) {
        throw new IOException("Unable to rename '" + file.getPath() + "'");
    }
    final File newfile = new File(originalPath);

    final byte[] buffer = new byte[4096];

    GZIPInputStream cin = null;
    FileOutputStream out = null;
    try {
        cin = new GZIPInputStream(new FileInputStream(gzip));
        out = new FileOutputStream(newfile);

        int len;
        while ((len = cin.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    } finally {
        if (cin != null) {
            try {
                cin.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (gzip.isFile()) {
            FileUtils.deleteQuietly(gzip);
        }
    }
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

public boolean removePage(String pageUuid) throws IOException {
    boolean removed = false;
    String sitemapIndex = metadata.getSitemapIndex(pageUuid);
    if (sitemapIndex == null) {
        log.warn("Page {} does not exist in sitemap", pageUuid);
        return removed;
    }//from  ww w  .  ja  v a 2  s.c  o  m

    String sitemapZip = SITEMAP_NAME_PREFIX + sitemapIndex + ".xml.gz";
    File sizemapZipFile = new File(mapResourcesRoot.getFile(), sitemapZip);
    if (!sizemapZipFile.exists()) {
        throw new IOException("Remove pageUuid " + pageUuid + " from sitemap failed becuase sitemap not found");
    }

    PipedInputStream bis = new PipedInputStream();
    PipedOutputStream bos = new PipedOutputStream(bis);
    InputStream zipfile = new FileInputStream(sizemapZipFile);
    GZIPInputStream gzipstream = new GZIPInputStream(zipfile);
    byte[] bytes = new byte[1024 * 1000];
    int len = 0;
    while ((len = gzipstream.read(bytes)) > 0) {
        bos.write(bytes, 0, len);
    }

    IOUtils.closeQuietly(zipfile);
    IOUtils.closeQuietly(gzipstream);

    String pageUrl = metadata.getPageUrl(pageUuid);
    String body = IOUtils.toString(bis);
    int loc = body.indexOf("<loc>" + pageUrl + "</loc>");
    if (loc != -1) {
        int start = StringUtils.lastIndexOf(body, "<url>", loc);
        int end = StringUtils.indexOf(body, "</url>", loc);
        if (start != -1 && end != -1) {
            //remove this URL
            body = StringUtils.substring(body, start, end + 6);
            zipToFile(sizemapZipFile, body.getBytes());
            removed = true;
        }
    }

    metadata.removePageMap(pageUuid);

    return removed;
}

From source file:eu.domibus.ebms3.common.CompressionService.java

/**
 * Decompress given GZIP Stream. Separated from {@link eu.domibus.ebms3.common.CompressionService#compress(java.io.InputStream, java.util.zip.GZIPOutputStream)}
 * just for a better overview even though they share the same logic (except finish() ).
 *
 * @param sourceStream Stream of compressed data
 * @param targetStream Stream of uncompressed data
 * @throws IOException//w w w .j a v  a 2s . c  om
 */
private void decompress(final GZIPInputStream sourceStream, final OutputStream targetStream)
        throws IOException {

    final byte[] buffer = new byte[1024];

    try {
        int i;
        while ((i = sourceStream.read(buffer)) > 0) {
            targetStream.write(buffer, 0, i);
        }

        sourceStream.close();
        targetStream.close();

    } catch (IOException e) {
        CompressionService.LOG.error(
                "I/O exception during gzip compression. method: doDecompress(GZIPInputStream, OutputStream");
        throw e;
    }
}

From source file:TextFileHandler.java

/**
 * Retrieve a String from a .gz file/* w w w.  j  a v a2 s  .  com*/
 * @param fileName e.g. bar.xml.gz
 * @return
 */
public String openGZipFile(String fileName) {
    try {
        GZIPInputStream in = new GZIPInputStream(new FileInputStream(fileName));
        StringBuffer buf = new StringBuffer();
        byte[] b = new byte[1024];
        int length;
        while ((length = in.read(b)) > 0) {
            String s = new String(b);
            buf.append(s);
        }
        return buf.toString().trim();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return null;
}