Example usage for java.util.zip GZIPOutputStream write

List of usage examples for java.util.zip GZIPOutputStream write

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream write.

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:csiro.pidsvc.servlet.controller.java

protected void returnAttachment(HttpServletResponse response, String filename, String outputFormat,
        String content) throws IOException {
    //      response.setContentType("application/xml"); response.getWriter().write(content); return;
    if (outputFormat.equalsIgnoreCase("xml")) {
        response.setContentType("application/xml");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);
        response.getOutputStream().write(content.getBytes());
    } else {/*from w w w  . j a va  2s  . c om*/
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);
        GZIPOutputStream gos = new GZIPOutputStream(response.getOutputStream());
        gos.write(content.getBytes());
        gos.close();
    }
}

From source file:com.orange.mmp.api.ws.jsonrpc.MMPJsonRpcServlet.java

/**
 * Gzip something.//  ww w. j a va 2 s. com
 * 
 * @param in
 *            original content
 * @return size gzipped content
 */
private byte[] gzip(byte[] in) throws IOException {
    if (in != null && in.length > 0) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(in);
        gout.flush();
        gout.close();
        return bout.toByteArray();
    }
    return new byte[0];
}

From source file:net.sf.ehcache.constructs.web.PageInfo.java

/**
 * @param ungzipped the bytes to be gzipped
 * @return gzipped bytes//from w  w  w. j  av a 2 s .  c  o  m
 */
private byte[] gzip(byte[] ungzipped) throws IOException, AlreadyGzippedException {
    if (isGzipped(ungzipped)) {
        throw new AlreadyGzippedException("The byte[] is already gzipped. It should not be gzipped again.");
    }
    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);
    gzipOutputStream.write(ungzipped);
    gzipOutputStream.close();
    return bytes.toByteArray();
}

From source file:halive.shootinoutside.common.core.game.map.GameMap.java

public byte[] serializeMapToCompressedByteArray() throws IOException {
    //Compress data
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(out);
    gzipOut.write(serializeMapToJSONString().getBytes());
    gzipOut.flush();/*from   w ww .j  av  a  2s .c  o  m*/
    gzipOut.finish();
    return out.toByteArray();
}

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

private void zipToFile(File sizemapZipFile, byte[] bytes) throws FileNotFoundException, IOException {
    FileOutputStream gzos = new FileOutputStream(sizemapZipFile);
    GZIPOutputStream gzipstream = new GZIPOutputStream(gzos);
    gzipstream.write(bytes);
    gzipstream.finish();//from w w w  .  j a va 2  s . co  m
    IOUtils.closeQuietly(gzos);
}

From source file:org.runnerup.export.GoogleFitSynchronizer.java

private Status sendData(StringWriter w, String suffix, RequestMethod method) throws IOException {
    Status status = Status.ERROR;/*from w ww. ja va 2 s  .co  m*/
    for (int attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
        HttpURLConnection connect = getHttpURLConnection(suffix, method);
        GZIPOutputStream gos = new GZIPOutputStream(connect.getOutputStream());
        gos.write(w.toString().getBytes());
        gos.flush();
        gos.close();

        int code = connect.getResponseCode();
        try {
            if (code == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                continue;
            } else if (code != HttpStatus.SC_OK) {
                Log.i(getName(), SyncHelper.parse(new GZIPInputStream(connect.getErrorStream())).toString());
                status = Status.ERROR;
                break;
            } else {
                Log.i(getName(), SyncHelper.parse(new GZIPInputStream(connect.getInputStream())).toString());
                status = Status.OK;
                break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            connect.disconnect();
        }
    }
    return status;
}

From source file:org.apache.mahout.text.SequenceFilesFromMailArchivesTest.java

/**
 * Create the input and output directories needed for testing
 * the SequenceFilesFromMailArchives application.
 *//*from w ww.ja  va 2 s .  com*/
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    inputDir = getTestTempDir("mail-archives-in");

    // write test mail messages to a gzipped file in a nested directory
    File subDir = new File(inputDir, "subdir");
    subDir.mkdir();
    File gzFile = new File(subDir, "mail-messages.gz");
    GZIPOutputStream gzOut = null;
    try {
        gzOut = new GZIPOutputStream(new FileOutputStream(gzFile));
        gzOut.write(testMailMessages.getBytes("UTF-8"));
        gzOut.finish();
    } finally {
        Closeables.close(gzOut, false);
    }

    File subDir2 = new File(subDir, "subsubdir");
    subDir2.mkdir();
    File gzFile2 = new File(subDir2, "mail-messages-2.gz");
    try {
        gzOut = new GZIPOutputStream(new FileOutputStream(gzFile2));
        gzOut.write(testMailMessages.getBytes("UTF-8"));
        gzOut.finish();
    } finally {
        Closeables.close(gzOut, false);
    }
}

From source file:org.apache.felix.karaf.webconsole.gogo.GogoPlugin.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String encoding = request.getHeader("Accept-Encoding");
    boolean supportsGzip = (encoding != null && encoding.toLowerCase().indexOf("gzip") > -1);
    SessionTerminal st = (SessionTerminal) request.getSession(true).getAttribute("terminal");
    if (st == null || st.isClosed()) {
        st = new SessionTerminal();
        request.getSession().setAttribute("terminal", st);
    }//ww w  . j a v  a  2 s .  co m
    String str = request.getParameter("k");
    String f = request.getParameter("f");
    String dump = st.handle(str, f != null && f.length() > 0);
    if (dump != null) {
        if (supportsGzip) {
            response.setHeader("Content-Encoding", "gzip");
            response.setHeader("Content-Type", "text/html");
            try {
                GZIPOutputStream gzos = new GZIPOutputStream(response.getOutputStream());
                gzos.write(dump.getBytes());
                gzos.close();
            } catch (IOException ie) {
                // handle the error here
                ie.printStackTrace();
            }
        } else {
            response.getOutputStream().write(dump.getBytes());
        }
    }
}

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

/**
 * compress byte array data with GZIP.//ww w .  ja  va  2 s.c  o  m
 * 
 * @param input
 *            the input data
 * @return the compressed data
 * @throws java.io.IOException
 */
public byte[] compress(byte[] input) throws java.io.IOException {
    byte[] result = null;
    java.io.ByteArrayOutputStream baout = null;
    GZIPOutputStream gzipout = null;
    try {
        baout = new java.io.ByteArrayOutputStream();
        gzipout = new GZIPOutputStream(baout);
        gzipout.write(input);
        gzipout.finish();
        result = baout.toByteArray();
        return result;
    } finally {
        IOUtils.closeQuietly(baout);
        IOUtils.closeQuietly(gzipout);
    }
}