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.zimbra.cs.service.util.ItemDataFile.java

public static void extract(InputStream is, boolean meta, Set<MailItem.Type> types, String cset, String dir)
        throws IOException {
    byte[] buf = new byte[TarBuffer.DEFAULT_BLKSIZE];
    TarEntry te;//  w  w  w .jav  a2 s . c  o m
    TarInputStream tis = new TarInputStream(new GZIPInputStream(is), cset == null ? "UTF-8" : cset);

    if (dir == null)
        dir = ".";
    try {
        while ((te = tis.getNextEntry()) != null) {
            if (skip(types, MailItem.Type.of((byte) te.getMajorDeviceId()))) {
                continue;
            }

            File f = new File(dir + File.separator + te.getName());
            FileOutputStream out;

            if (!f.getParent().equals("."))
                f.getParentFile().mkdir();
            if (te.getName().endsWith(".meta")) {
                if (!meta)
                    continue;
                System.out.println(f);
                out = new FileOutputStream(f);
                ItemData id = new ItemData(getData(tis, te));
                out.write(id.encode(2).getBytes("UTF-8"));
            } else {
                int in;

                System.out.println(f);
                out = new FileOutputStream(f);
                while ((in = tis.read(buf)) != -1)
                    out.write(buf, 0, in);
            }
            out.close();
            f.setLastModified(te.getModTime().getTime());
        }
    } finally {
        tis.close();
    }
}

From source file:com.android.feedmeandroid.HTTPClient.java

public static ArrayList<JSONObject> SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {//  w  ww  .j  a  va 2s .c  om
        ArrayList<JSONObject> retArray = new ArrayList<JSONObject>();
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accepts", "application/json");
        httpPostRequest.setHeader("Content-Type", "application/json");
        // httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set
        // this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.v(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString = convertStreamToString(instream);
            Log.i(TAG, resultString);
            instream.close();
            resultString = resultString.substring(1, resultString.length() - 2); // remove wrapping "[" and
            // "]"
            String[] results = resultString.split("\\},\\{");
            for (int i = 0; i < results.length; i++) {
                JSONObject jsonObjRecv;
                String res = results[i];

                // Transform the String into a JSONObject
                if (i == 0 && results.length > 1) {
                    jsonObjRecv = new JSONObject(res + "}");
                } else if (i == results.length - 1 && results.length > 1) {
                    jsonObjRecv = new JSONObject("{" + res);
                } else {
                    jsonObjRecv = new JSONObject("{" + res + "}");
                }
                retArray.add(jsonObjRecv);

                // Raw DEBUG output of our received JSON object:
                Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");
            }

            return retArray;
        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:eu.unifiedviews.plugins.transformer.gzipper.GzipperTest.java

@Test
public void execute() throws Exception {
    GzipperConfig_V1 config = new GzipperConfig_V1();

    // Prepare DPU.
    Gzipper dpu = new Gzipper();
    dpu.configure((new ConfigurationBuilder()).setDpuConfiguration(config).toString());

    // Prepare test environment.
    TestEnvironment environment = new TestEnvironment();

    // Prepare data unit.
    WritableFilesDataUnit filesOutput = environment.createFilesOutput("filesOutput");
    WritableFilesDataUnit filesInput = environment.createFilesInput("filesInput");

    File inputFile = new File(URI.create(filesInput.addNewFile("LICENSE")));
    try (FileOutputStream fout = new FileOutputStream(inputFile)) {
        IOUtils.copy(Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE"), fout);
    }/* w  w  w .  j a v  a  2s.  c  o m*/
    try {
        // Run.
        environment.run(dpu);

        // Get file iterator.
        Set<FilesDataUnit.Entry> outputFiles = FilesHelper.getFiles(filesOutput);
        Assert.assertEquals(1, outputFiles.size());

        FilesDataUnit.Entry entry = outputFiles.iterator().next();

        byte[] outputContent = IOUtils.toByteArray(
                new GZIPInputStream(new FileInputStream(new File(new URI(entry.getFileURIString())))));
        byte[] expectedContent = IOUtils
                .toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE"));

        Assert.assertArrayEquals(expectedContent, outputContent);

        Assert.assertEquals("LICENSE.gz", VirtualPathHelpers.getVirtualPath(filesOutput, "LICENSE"));
    } finally {
        // Release resources.
        environment.release();
    }
}

From source file:cltestgrid.GetBlob.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String userAgent = req.getHeader("User-Agent");
    if (userAgent != null && userAgent.contains("Baiduspider")) {
        resp.setStatus(SC_MOVED_PERMANENTLY);
        resp.setHeader("Location", "http://www.baidu.com/search/spider.html");
        resp.setHeader("X-READ-ME",
                "Please honor robots.txt and don't waste our resources. http://cl-test-grid.appspot.com/robots.txt");
        return;// w ww. ja  v a  2s . co  m
    }

    final String key = req.getParameter("key");
    String filename = "/gs/cl-test-grid-logs/" + key;
    AppEngineFile file = new AppEngineFile(filename);
    FileReadChannel readChannel = null;
    InputStream inputStream = null;
    try {
        final boolean lock = false;
        readChannel = FileServiceFactory.getFileService().openReadChannel(file, lock);
        inputStream = Channels.newInputStream(readChannel);
        resp.setContentType("text/plain");
        // The log files are gzipped, but we can't serve gzipped content,
        // because GAE gzips servlet output itlself and so logs would end-up gzipped twice.
        // Therefore we need to ungzip the log.
        InputStream ungzipper = new GZIPInputStream(new BufferedInputStream(inputStream, 100 * 1024));
        IOUtils.copy(ungzipper, resp.getOutputStream());
        resp.getOutputStream().flush();
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(readChannel);
    }
}

From source file:com.amazonaws.services.kinesis.model.transform.PutRecordRequestMarshallerTest.java

@Ignore
@Test//from   w ww  .j a v a2 s .co  m
public void test() throws Exception {
    PutRecordRequest putRecordRequest = new PutRecordRequest();
    putRecordRequest.setStreamName("stream name");
    putRecordRequest.setSequenceNumberForOrdering("sequence number for ordering");
    putRecordRequest.setPartitionKey("partition key");
    String randomStr = RandomStringUtils.random(128 * 1024);
    putRecordRequest.setData(ByteBuffer.wrap(randomStr.getBytes(StringUtils.UTF8)));
    PutRecordRequestMarshaller marshaller = new PutRecordRequestMarshaller();
    Request<PutRecordRequest> request = marshaller.marshall(putRecordRequest);

    assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
    byte[] content = IOUtils.toByteArray(request.getContent());
    assertEquals("content length", request.getHeaders().get("Content-Length"), String.valueOf(content.length));
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
    String str = IOUtils.toString(gis);
    Map<String, String> map = JsonUtils.jsonToMap(str);
    assertEquals("StreamName", "stream name", map.get("StreamName"));
    String data = new String(BinaryUtils.fromBase64(map.get("Data")), StringUtils.UTF8);
    assertEquals("same data", randomStr, data);
}

From source file:com.cyc.tool.distributedrepresentations.Word2VecSpaceFromFile.java

/**
 * Create a W2V space in a DB.//  w  w w .  j  av  a  2  s  .  com
 * 
 * @param w2vZipFile
 * @throws FileNotFoundException
 * @throws IOException
 */
protected final void createW2VinDB(String w2vZipFile) throws FileNotFoundException, IOException {
    try (DataInputStream data_in = new DataInputStream(
            new GZIPInputStream(new FileInputStream(new File(w2vZipFile))))) {
        getWordsAndSize(data_in);
        if (vectors.size() == words) {
            System.out.println("Word2Vec is in DB");
        } else {
            System.out.println("DB Size:" + vectors.size());

            System.out.println("Want to read Word Count: " + words);
            System.out.println("Size:" + getSize());
            for (int w = 0; w < words; w++) {
                float[] v = new float[getSize()];
                String key = getVocabString(data_in);
                System.out.println(w + ":\t" + key);

                IntStream.range(0, getSize()).forEach(i -> v[i] = getFloat(data_in));
                vectors.put(key, normVector(v));
                if (w % 100000 == 1) {
                    db.commit();
                }
            }
            db.commit();
            db.compact();
        }
    }
}

From source file:com.liferay.util.transport.MulticastDatagramHandler.java

protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
    InputStream is = new GZIPInputStream(new UnsyncByteArrayInputStream(bytes));
    UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(bytes.length);

    byte[] buffer = new byte[1500];

    int c = 0;/*from   ww  w .j  a va2s .co m*/

    while (true) {
        if (c == -1) {
            break;
        }

        c = is.read(buffer, 0, 1500);

        if (c != -1) {
            ubaos.write(buffer, 0, c);
        }
    }

    is.close();

    ubaos.flush();
    ubaos.close();

    return ubaos.toByteArray();
}

From source file:edu.stanford.muse.index.NEROld.java

public static void readLocationsFreebase() throws IOException {
    InputStream is = new GZIPInputStream(NER.class.getClassLoader().getResourceAsStream("locations.gz"));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        StringTokenizer st = new StringTokenizer(line, "\t");
        if (st.countTokens() == 3) {
            String locationName = st.nextToken();
            String canonicalName = locationName.toLowerCase();
            String lat = st.nextToken();
            String longi = st.nextToken();
            locations.put(canonicalName, new LocationInfo(locationName, lat, longi));
        }/*  w w w .j  ava2s . co m*/
    }
}

From source file:uk.ac.ebi.eva.pipeline.io.readers.GeneReaderTest.java

@Test
public void shouldReadAllLinesInGtf() throws Exception {
    ExecutionContext executionContext = MetaDataInstanceFactory.createStepExecution().getExecutionContext();

    //simulate VEP output file
    File file = JobTestUtils.makeGzipFile(GtfStaticTestData.GTF_CONTENT);

    GeneReader geneReader = new GeneReader(file);
    geneReader.setSaveState(false);/*  w ww . j a v  a  2s  .c o m*/
    geneReader.open(executionContext);

    FeatureCoordinates gene;
    int chromosomeCount = 0;
    int count = 0;
    while ((gene = geneReader.read()) != null) {
        count++;
        if (gene.getChromosome() != null && !gene.getChromosome().isEmpty()) {
            chromosomeCount++;
        }
    }
    // all should have at least the chromosome field
    assertEquals(count, chromosomeCount);

    // GeneReader should get all the lines from the file
    long actualCount = JobTestUtils.getLines(new GZIPInputStream(new FileInputStream(file)));
    assertEquals(actualCount, count);
}

From source file:com.google.devtools.build.lib.bazel.repository.TarGzFunction.java

@Nullable
@Override//www. j  ava 2s . c o m
public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctionException {
    DecompressorDescriptor descriptor = (DecompressorDescriptor) skyKey.argument();
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;

    try (GZIPInputStream gzipStream = new GZIPInputStream(
            new FileInputStream(descriptor.archivePath().getPathFile()))) {
        TarArchiveInputStream tarStream = new TarArchiveInputStream(gzipStream);
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }

            Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
            FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
            if (entry.isDirectory()) {
                FileSystemUtils.createDirectoryAndParents(filename);
            } else {
                if (entry.isSymbolicLink()) {
                    PathFragment linkName = new PathFragment(entry.getLinkName());
                    if (linkName.isAbsolute()) {
                        linkName = linkName.relativeTo(PathFragment.ROOT_DIR);
                        linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
                    }
                    FileSystemUtils.ensureSymbolicLink(filename, linkName);
                } else {
                    Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
                    filename.chmod(entry.getMode());
                }
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }

    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(
                new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"),
                Transience.PERSISTENT);
    }

    return new DecompressorValue(descriptor.repositoryPath());
}