Class GZIPUtil
- java.lang.Object
-
- hu.icellmobilsoft.coffee.tool.utils.compress.GZIPUtil
-
public class GZIPUtil extends Object
Helper class to GZIP compress/decompress string contents
Example use of compress/decompresspublic static void main(String[] args) { String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmlRoot><xmlChild xmlField=\"xmlValue\">anotherXmlValue</xmlChild></xmlRoot>"; try { System.out.println("source size: " + source.getBytes(StandardCharsets.UTF_8).length); long start = System.currentTimeMillis(); byte[] compressed = compress(source.getBytes(StandardCharsets.UTF_8)); long end = System.currentTimeMillis(); System.out.println("compressed size: " + compressed.length + ", time: " + (end - start) + "ms"); int decompressedSize = decompressedSize(compressed); System.out.println("original size: " + source.length() + ", decompressed size: " + decompressedSize); start = System.currentTimeMillis(); byte[] decompressed = decompress(compressed); end = System.currentTimeMillis(); System.out.println("decompressed: " + new String(decompressed, StandardCharsets.UTF_8)); System.out.println("decompress time: " + (end - start) + "ms"); } catch (Exception e) { e.printStackTrace(); } }- Since:
- 1.0.0
- Author:
- robert.kaplar
-
-
Constructor Summary
Constructors Constructor Description GZIPUtil()Default constructor, constructs a new object.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]compress(byte[] data)Compress the source byte array contentstatic byte[]decompress(byte[] data)Decompress the compressed byte array contentstatic <T> Tdecompress(byte[] data, Class<T> clazz)Unzip and convert result from byte[].static intdecompressedSize(byte[] data)Visszaadja a GZIP eredeti meretet.static booleanisCompressed(byte[] bytes)Determines if a byte array is compressed.
-
-
-
Method Detail
-
compress
public static byte[] compress(byte[] data) throws hu.icellmobilsoft.coffee.dto.exception.BaseExceptionCompress the source byte array content- Parameters:
data- input byte array- Returns:
- compressed byte array
- Throws:
hu.icellmobilsoft.coffee.dto.exception.BaseException- exception
-
decompress
public static byte[] decompress(byte[] data) throws hu.icellmobilsoft.coffee.dto.exception.BaseExceptionDecompress the compressed byte array content- Parameters:
data- input byte array- Returns:
- decompressed byte array
- Throws:
hu.icellmobilsoft.coffee.dto.exception.BaseException- exception
-
decompress
public static <T> T decompress(byte[] data, Class<T> clazz) throws hu.icellmobilsoft.coffee.dto.exception.BaseExceptionUnzip and convert result from byte[].- Type Parameters:
T- destination type- Parameters:
data- input byte arrayclazz- destination class- Returns:
- unzipped and converted object
- Throws:
hu.icellmobilsoft.coffee.dto.exception.BaseException- exception
-
decompressedSize
public static int decompressedSize(byte[] data)
Visszaadja a GZIP eredeti meretet. Valojaban nem mindig lehet szamolni vele, de megis van valami. Minta:- Parameters:
data- input adat- Returns:
- eredeti meret
- See Also:
- https://stackoverflow.com/questions/7317243/gets-the-uncompressed-size-of-this-gzipinputstream
-
isCompressed
public static boolean isCompressed(byte[] bytes)
Determines if a byte array is compressed. Thejava.util.zipGZip implementaiton does not expose the GZip header so it is difficult to determine if a string is compressed.- Parameters:
bytes- an array of bytes- Returns:
- true if the array is compressed or false otherwise
- See Also:
- https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
-
-