Last active
April 26, 2019 08:23
-
-
Save bryanmaina/bf8cc8245be4a5af203f8e87d571bc5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.zip.DeflaterOutputStream; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
public final class ZipHelper { | |
private static final Logger LOG = LoggerFactory.getLogger(ZipHelper.class); | |
/** | |
* The level should be one of | |
* | |
* @param src source | |
* @param level Level from {@link java.util.zip.Deflater} | |
* @param nowrap if true use GZIP compatible compression | |
* @return | |
* @throws IOException | |
*/ | |
public static byte[] compress(final byte[] src, final int level, boolean nowrap) throws IOException { | |
byte[] result; | |
java.util.zip.Deflater deflater = new java.util.zip.Deflater(level, nowrap); | |
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length); | |
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater)) { | |
deflaterOutputStream.write(src); | |
deflaterOutputStream.finish(); | |
deflaterOutputStream.close(); | |
result = byteArrayOutputStream.toByteArray(); | |
} catch (IOException e) { | |
deflater.end(); | |
LOG.error("Failed to compress the given source."); | |
throw e; | |
} | |
return result; | |
} | |
/** | |
* @param filename | |
* @param src | |
* @return | |
* @throws IOException | |
*/ | |
public static byte[] zipBytes(String filename, byte[] src) throws IOException { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ZipOutputStream zos = new ZipOutputStream(baos); | |
ZipEntry entry = new ZipEntry(filename); | |
entry.setSize(src.length); | |
zos.putNextEntry(entry); | |
zos.write(src); | |
zos.closeEntry(); | |
zos.close(); | |
return baos.toByteArray(); | |
} | |
/** | |
* Calculate the size of a byte array | |
* | |
* @param input source to calculate size | |
* @return an {@link HashMap} of the size in byte, kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, yottabytes | |
*/ | |
static HashMap<String, Double> fileSize(byte[] input) { | |
double bytes = input.length; | |
double kilobytes = (bytes / 1024); | |
double megabytes = (kilobytes / 1024); | |
double gigabytes = (megabytes / 1024); | |
double terabytes = (gigabytes / 1024); | |
double petabytes = (terabytes / 1024); | |
double exabytes = (petabytes / 1024); | |
double zettabytes = (exabytes / 1024); | |
double yottabytes = (zettabytes / 1024); | |
return new HashMap<String, Double>() { | |
{ | |
put("bytes", bytes); | |
put("kilobytes", kilobytes); | |
put("megabytes", megabytes); | |
put("gigabytes", gigabytes); | |
put("terabytes", terabytes); | |
put("petabytes", petabytes); | |
put("exabytes", exabytes); | |
put("zettabytes", zettabytes); | |
put("yottabytes", yottabytes); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment