Java tutorial
/******************************************************************************* * Copyright (c) 2018 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0 * which is available at https://www.apache.org/licenses/LICENSE-2.0. * * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 *******************************************************************************/ package org.eclipse.winery.accountability.blockchain.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.IOUtils; /** * Provide methods that allow compression and decompression of byte arrays */ public class CompressionUtils { public static byte[] compress(byte[] content) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(content); gzipOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } System.out.printf("Compressiono %f\n", (1.0f * content.length / byteArrayOutputStream.size())); return byteArrayOutputStream.toByteArray(); } public static byte[] decompress(byte[] contentBytes) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(contentBytes)), out); } catch (IOException e) { throw new RuntimeException(e); } return out.toByteArray(); } }