Java XML Data Type Converter compress(String string)

Here you can find the source of compress(String string)

Description

compress

License

Apache License

Declaration

public static byte[] compress(String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.zip.GZIPOutputStream;
import javax.xml.bind.DatatypeConverter;

public class Main {
    public static byte[] compress(String string) {
        byte[] compressed = null;
        try {//from   ww w. ja  va2  s  .  c  o  m
            ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
            GZIPOutputStream gos = new GZIPOutputStream(os);
            gos.write(string.getBytes());
            gos.close();
            compressed = os.toByteArray();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return compressed;
    }

    public static void compress(String source_filepath, String destinaton_zip_filepath) throws Exception {
        byte[] buffer = new byte[1024];
        FileOutputStream fileOutputStream = new FileOutputStream(destinaton_zip_filepath);
        GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);
        FileInputStream fileInput = new FileInputStream(source_filepath);
        int bytes_read;
        while ((bytes_read = fileInput.read(buffer)) > 0) {
            gzipOuputStream.write(buffer, 0, bytes_read);
        }
        fileInput.close();
        gzipOuputStream.finish();
        gzipOuputStream.close();
        fileOutputStream.close();
    }

    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }
}

Related

  1. appendByteAsJavaByteInitializer(StringBuilder sb, byte b)
  2. byteArrayAsUrlString(byte[] bytes)
  3. calculateCR16(String value)
  4. calculateHMAC(final String secretKey, final String feedId)
  5. compress(String string)
  6. convertBpmnVariableValueToXslParam(final Object bpmnVariableValue)
  7. convertToFourBytes(int numberToConvert)
  8. convertToPEM(PublicKey key)
  9. convertToTwoBytes(int numberToConvert)