Example usage for java.util.zip DeflaterOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:FileDeflater.java

public static void main(String[] args) throws Exception {
    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");
    DeflaterOutputStream dos = new DeflaterOutputStream(fout);
    for (int c = fin.read(); c != -1; c = fin.read()) {
        dos.write(c);
    }/*from  w  w w .ja  v a2 s  . c o  m*/
    dos.close();
    fin.close();
}

From source file:Main.java

public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
        try {//w  w  w.  j a v a2 s . c  om
            FileInputStream fin = new FileInputStream(args[i]);
            FileOutputStream fout = new FileOutputStream(args[i] + "dfl");
            DeflaterOutputStream dos = new DeflaterOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                dos.write(c);
            }
            dos.close();
            fin.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {/*from  ww  w  .j  ava2 s  .c om*/
            FileInputStream fin = new FileInputStream(args[i]);
            FileOutputStream fout = new FileOutputStream(args[i] + DEFLATE_SUFFIX);
            DeflaterOutputStream dos = new DeflaterOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                dos.write(c);
            }
            dos.close();
            fin.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:Main.java

public static String compressString(String str, String encoding) {
    String compressedString = "";

    try {/*from  w w w .  ja v a 2  s .  c  om*/
        byte[] input = str.getBytes(encoding);
        Deflater d = new Deflater();
        d.setLevel(Deflater.BEST_COMPRESSION);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
        dout.write(input);
        dout.close();
        compressedString = new String(Hex.encodeHex(out.toByteArray()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return compressedString;
}

From source file:zipB64.java

protected static String encodeMessage(String messageStr) {
    try {/*from   w  w  w . j av a2  s . co m*/
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        Base64 b = new Base64(-1);
        return new String(b.encode(bytesOut.toByteArray()));
    } catch (Exception e) {
        return "crotte";
    }
}

From source file:com.barrybecker4.common.util.Base64Codec.java

/**
 * take a String and compress it.// w w w.  j a  v  a 2 s.  com
 * See @decompress for reversing the compression.
 * @param data a string to compress.
 * @return compressed string representation.
 */
public static synchronized String compress(final String data) {

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
    Deflater deflater = new Deflater();
    DeflaterOutputStream oStream = new DeflaterOutputStream(byteOut, deflater);

    try {
        oStream.write(data.getBytes(CONVERTER_UTF8));
        oStream.flush();
        oStream.close();
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e);
    } catch (IOException e) {
        throw new IllegalStateException("io error :" + e.getMessage(), e);
    }

    return new String(Base64.encodeBase64(byteOut.toByteArray()));
}

From source file:com.eviware.soapui.impl.wsdl.support.CompressionSupport.java

private static byte[] DeflaterCompress(byte[] requestContent) throws IOException {
    ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
    DeflaterOutputStream defstream = new DeflaterOutputStream(compressedContent);
    defstream.write(requestContent);
    defstream.finish();//from  w w w .j av  a 2s. c om

    // get the compressed content
    return compressedContent.toByteArray();
}

From source file:com.faceye.feature.util.http.DeflateUtils.java

/**
 * Returns a deflated copy of the input array.
 *//*from ww w  .  j  a  v a 2 s . c  o  m*/
public static final byte[] deflate(byte[] in) {
    // compress using DeflaterOutputStream 
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

    DeflaterOutputStream outStream = new DeflaterOutputStream(byteOut);

    try {
        outStream.write(in);
    } catch (Exception e) {
        LOG.error("Error compressing: ", e);
    }

    try {
        outStream.close();
    } catch (IOException e) {
        LOG.error("Error closing: ", e);
    }

    return byteOut.toByteArray();
}

From source file:com.iflytek.spider.util.DeflateUtils.java

/**
 * Returns a deflated copy of the input array.
 *//*  ww w .jav a  2 s  .  c o  m*/
public static final byte[] deflate(byte[] in) {
    // compress using DeflaterOutputStream 
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

    DeflaterOutputStream outStream = new DeflaterOutputStream(byteOut);

    try {
        outStream.write(in);
    } catch (Exception e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
    }

    try {
        outStream.close();
    } catch (IOException e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
    }

    return byteOut.toByteArray();
}

From source file:de.topobyte.livecg.core.painting.backend.ipe.IpeImageEncoder.java

public static IpeImage encode(Image image) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            int rgb = image.getRGB(i, j);
            dos.write(rgb >> 16);
            dos.write(rgb >> 8);//from w ww.ja  v  a 2s.com
            dos.write(rgb);
        }
    }
    dos.close();
    byte[] buffer = baos.toByteArray();
    int length = buffer.length;

    byte[] bbase64 = Base64.encodeBase64(buffer);
    String base64 = new String(bbase64);

    return new IpeImage(base64, length);
}