Example usage for java.util.zip GZIPOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:com.comcast.cqs.util.Util.java

public static String compress(String decompressed) throws IOException {
    if (decompressed == null || decompressed.equals("")) {
        return decompressed;
    }/*from  w  w  w.j  a  v a  2 s.  com*/
    ByteArrayOutputStream out = new ByteArrayOutputStream(decompressed.length());
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(decompressed.getBytes());
    gzip.close();
    out.close();
    String compressed = Base64.encodeBase64String(out.toByteArray());
    logger.debug("event=compressed from=" + decompressed.length() + " to=" + compressed.length());
    return compressed;
}

From source file:com.zotoh.core.io.StreamUte.java

/**
 * @param bits//from  w  w  w.j a v a  2 s. c o  m
 * @return
 * @throws IOException
 */
public static byte[] gzip(byte[] bits) throws IOException {

    ByteOStream baos = new ByteOStream();
    GZIPOutputStream g = new GZIPOutputStream(baos);

    if (bits != null && bits.length > 0) {
        g.write(bits, 0, bits.length);
        g.close();
    }

    return baos.asBytes();
}

From source file:packjacket.StaticUtils.java

/**
 * GZips the main log file/*from w w w . jav  a 2 s  .  c  o  m*/
 * @return the gzipped file
 * @throws IOException if any I/O error occurs
 */
public static File gzipLog() throws IOException {
    //Write out buffer of log file
    RunnerClass.nfh.flush();
    //Initialize log and gzip-log files
    File log = new File(RunnerClass.homedir + "pj.log");
    GZIPOutputStream out = new GZIPOutputStream(
            new FileOutputStream(new File(log.getCanonicalPath() + ".pjl")));
    FileInputStream in = new FileInputStream(log);

    //How many bytes to copy with each incrmental copy of file.
    int bufferSize = 4 * 1024;

    //Buffer into which data is read from source file
    byte[] buffer = new byte[bufferSize];
    //How many bytes read so far
    int bytesRead;
    //Runs until no bytes left to read from source
    while ((bytesRead = in.read(buffer)) >= 0)
        out.write(buffer, 0, bytesRead);
    //Close streams
    out.close();
    in.close();

    return new File(log.getCanonicalPath() + ".pjl");
}

From source file:com.urs.triptracks.TripUploader.java

public static byte[] compress(String string) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());/*from  ww  w  .j  a va 2s .  c  o  m*/
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();
    return compressed;
}

From source file:Main.java

public static void toTargz(String srcFile, String targetFile) throws IOException {
    File sourceFile = new File(srcFile);
    File target = new File(targetFile);
    FileInputStream in = null;/*  ww  w.ja v  a 2s.c  o  m*/
    GZIPOutputStream gout = null;
    try {
        in = new FileInputStream(sourceFile);
        gout = new GZIPOutputStream(new FileOutputStream(target));
        byte[] array = new byte[BUFFER_LEN];
        int number = -1;
        while ((number = in.read(array, 0, array.length)) != -1) {
            gout.write(array, 0, number);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (gout != null) {
            try {
                gout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:util.io.Mirror.java

/**
 * Write the mirror array to the given stream.
 *
 * @param stream The stream to write the mirror array to.
 * @param mirrorArr The mirror array to write.
 * @throws IOException Thrown if something went wrong.
 *///w w w.j a v a 2  s  . c om
private static void writeMirrorListToStream(OutputStream stream, Mirror[] mirrorArr) throws IOException {
    GZIPOutputStream gOut = new GZIPOutputStream(stream);

    PrintWriter writer = new PrintWriter(gOut);
    for (Mirror mirror : mirrorArr) {
        writer.print(mirror.getUrl());
        writer.print(";");
        writer.println(String.valueOf(mirror.getWeight()));
    }
    writer.close();

    gOut.close();
}

From source file:com.hbs.common.josn.JSONUtil.java

public static void writeJSONToResponse(HttpServletResponse response, String encoding, boolean wrapWithComments,
        String serializedJSON, boolean smd, boolean gzip, String contentType) throws IOException {
    String json = serializedJSON == null ? "" : serializedJSON;
    if (wrapWithComments) {
        StringBuilder sb = new StringBuilder("/* ");
        sb.append(json);//from w  ww .j a  v  a  2 s .com
        sb.append(" */");
        json = sb.toString();
    }
    if (log.isDebugEnabled()) {
        log.debug("[JSON]" + json);
    }

    if (contentType == null) {
        contentType = "application/json";
    }
    response.setContentType((smd ? "application/json-rpc;charset=" : contentType + ";charset=") + encoding);
    if (gzip) {
        response.addHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = null;
        InputStream in = null;
        try {
            out = new GZIPOutputStream(response.getOutputStream());
            in = new ByteArrayInputStream(json.getBytes());
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null) {
                out.finish();
                out.close();
            }
        }

    } else {
        response.setContentLength(json.getBytes(encoding).length);
        PrintWriter out = response.getWriter();
        out.print(json);
    }
}

From source file:backtype.storm.utils.Utils.java

public static byte[] gzip(byte[] data) {
    try {//from www  . j av  a2  s. c om
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(bos);
        out.write(data);
        out.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.drill.exec.vector.complex.writer.TestJsonReader.java

License:asdf

public static void gzipIt(File sourceFile) throws IOException {

    // modified from: http://www.mkyong.com/java/how-to-compress-a-file-in-gzip-format/
    byte[] buffer = new byte[1024];
    GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(sourceFile.getPath() + ".gz"));

    FileInputStream in = new FileInputStream(sourceFile);

    int len;/*from w  w  w  .  ja va2s .com*/
    while ((len = in.read(buffer)) > 0) {
        gzos.write(buffer, 0, len);
    }
    in.close();
    gzos.finish();
    gzos.close();
}

From source file:com.wattzap.model.social.SelfLoopsAPI.java

public static int uploadActivity(String email, String passWord, String fileName, String note)
        throws IOException {
    JSONObject jsonObj = null;/*from www .j av  a2  s.c  o  m*/

    FileInputStream in = null;
    GZIPOutputStream out = null;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("enctype", "multipart/mixed");

        in = new FileInputStream(fileName);
        // Create stream to compress data and write it to the to file.
        ByteArrayOutputStream obj = new ByteArrayOutputStream();
        out = new GZIPOutputStream(obj);

        // Copy bytes from one stream to the other
        byte[] buffer = new byte[4096];
        int bytes_read;
        while ((bytes_read = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytes_read);
        }
        out.close();
        in.close();

        ByteArrayBody bin = new ByteArrayBody(obj.toByteArray(), ContentType.create("application/x-gzip"),
                fileName);
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("email", new StringBody(email, ContentType.TEXT_PLAIN))
                .addPart("pw", new StringBody(passWord, ContentType.TEXT_PLAIN)).addPart("tcxfile", bin)
                .addPart("note", new StringBody(note, ContentType.TEXT_PLAIN)).build();

        httpPost.setEntity(reqEntity);

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            int code = response.getStatusLine().getStatusCode();
            switch (code) {
            case 200:

                HttpEntity respEntity = response.getEntity();

                if (respEntity != null) {
                    // EntityUtils to get the response content
                    String content = EntityUtils.toString(respEntity);
                    //System.out.println(content);
                    JSONParser jsonParser = new JSONParser();
                    jsonObj = (JSONObject) jsonParser.parse(content);
                }

                break;
            case 403:
                throw new RuntimeException(
                        "Authentification failure " + email + " " + response.getStatusLine());
            default:
                throw new RuntimeException("Error " + code + " " + response.getStatusLine());
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }

        int activityId = ((Long) jsonObj.get("activity_id")).intValue();

        // parse error code
        int error = ((Long) jsonObj.get("error_code")).intValue();
        if (activityId == -1) {
            String message = (String) jsonObj.get("message");
            switch (error) {
            case 102:
                throw new RuntimeException("Empty TCX file " + fileName);
            case 103:
                throw new RuntimeException("Invalide TCX Format " + fileName);
            case 104:
                throw new RuntimeException("TCX Already Present " + fileName);
            case 105:
                throw new RuntimeException("Invalid XML " + fileName);
            case 106:
                throw new RuntimeException("invalid compression algorithm");
            case 107:
                throw new RuntimeException("Invalid file mime types");
            default:
                throw new RuntimeException(message + " " + error);
            }
        }

        return activityId;
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
        httpClient.close();
    }
}