List of usage examples for java.util.zip Deflater deflate
public int deflate(ByteBuffer output)
From source file:org.getspout.spout.packet.PacketAddonData.java
public void compress() { if (!compressed) { if (data != null) { Deflater deflater = new Deflater(); deflater.setInput(data);/*from w w w . j a va2 s. co m*/ deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:org.phpmaven.phar.PharJavaPackager.java
private void packFile(final ByteArrayOutputStream fileEntriesBaos, final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath) throws IOException { if (DEBUG) {//from w w w. j a v a 2 s .co m System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes."); } final byte[] fileBytes = filePath.getBytes("UTF-8"); writeIntLE(fileEntriesBaos, fileBytes.length); fileEntriesBaos.write(fileBytes); // TODO Complain with files larger than 4 bytes file length writeIntLE(fileEntriesBaos, (int) fileToPack.length()); writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000)); final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack); if (DEBUG) { System.out.println("read " + uncompressed.length + " bytes from file."); } final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream(); // final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream); // gzipStream.write(uncompressed); // gzipStream.flush(); final CRC32 checksum = new CRC32(); checksum.update(uncompressed); final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); deflater.setInput(uncompressed); deflater.finish(); final byte[] buf = new byte[Short.MAX_VALUE]; while (!deflater.needsInput()) { final int bytesRead = deflater.deflate(buf); compressedStream.write(buf, 0, bytesRead); } final byte[] compressed = compressedStream.toByteArray(); if (DEBUG) { System.out.println("compressed to " + compressed.length + " bytes."); } // final Inflater decompresser = new Inflater(); // decompresser.setInput(compressed); // byte[] result = new byte[5000]; // try { // int resultLength = decompresser.inflate(result); // final String str = new String(result, 0, resultLength); // int i = 42; // } catch (DataFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // decompresser.end(); compressedFilesBaos.write(compressed); writeIntLE(fileEntriesBaos, compressed.length); writeIntLE(fileEntriesBaos, checksum.getValue()); // bits: 0x00001000, gzip fileEntriesBaos.write(0); fileEntriesBaos.write(0x10); fileEntriesBaos.write(0); fileEntriesBaos.write(0); // 0 bytes manifest writeIntLE(fileEntriesBaos, 0); }
From source file:nl.nn.adapterframework.util.Misc.java
public static byte[] compress(byte[] input) throws IOException { // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input);//from w ww . j av a2 s .c o m compressor.finish(); // Create an expandable byte array to hold the compressed data. // You cannot use an array that's the same size as the orginal because // there is no guarantee that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); // Get the compressed data return bos.toByteArray(); }
From source file:NCDSearch.NCDSearch.java
public double NCD(byte[] file, byte[] target, int compression) { Deflater compressor = new Deflater(compression); byte[] outputtrash = new byte[file.length + target.length]; int bothcompressedsize; int filecompressedsize; int targetcompressedsize; byte[] both = new byte[file.length + target.length]; for (int i = 0; i < file.length; i++) { both[i] = file[i];//w w w . java 2 s. c o m } for (int i = 0; i < target.length; i++) { both[i + file.length] = target[i]; } compressor.setInput(file); compressor.finish(); filecompressedsize = compressor.deflate(outputtrash); compressor.reset(); compressor.setInput(target); compressor.finish(); targetcompressedsize = compressor.deflate(outputtrash); compressor.reset(); compressor.setInput(both); compressor.finish(); bothcompressedsize = compressor.deflate(outputtrash); compressor.reset(); return (double) (bothcompressedsize - filecompressedsize) / (double) targetcompressedsize; }
From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java
@Override public String encodeDiff(final RevisionCodecData codecData, final Diff diff) throws UnsupportedEncodingException, EncodingException { String sEncoding;/*from w w w . ja v a 2s.c o m*/ byte[] bData = encode(codecData, diff); if (MODE_ZIP_COMPRESSION) { Deflater compresser = new Deflater(); compresser.setInput(bData); compresser.finish(); byte[] output = new byte[1000]; ByteArrayOutputStream stream = new ByteArrayOutputStream(); int cLength; do { cLength = compresser.deflate(output); stream.write(output, 0, cLength); } while (cLength == 1000); output = stream.toByteArray(); if (bData.length + 1 < output.length) { sEncoding = Base64.encodeBase64String(bData); } else { sEncoding = "_" + Base64.encodeBase64String(output); } } else { sEncoding = Base64.encodeBase64String(bData); } return sEncoding; }
From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java
@Override public byte[] binaryDiff(final RevisionCodecData codecData, final Diff diff) throws UnsupportedEncodingException, EncodingException { byte[] bData = encode(codecData, diff); if (MODE_ZIP_COMPRESSION) { Deflater compresser = new Deflater(); compresser.setInput(bData);//ww w .j a va 2s . c o m compresser.finish(); byte[] output = new byte[1000]; ByteArrayOutputStream stream = new ByteArrayOutputStream(); int cLength; do { cLength = compresser.deflate(output); stream.write(output, 0, cLength); } while (cLength == 1000); output = stream.toByteArray(); if (bData.length + 1 < output.length) { return bData; } else { stream = new ByteArrayOutputStream(); stream.write(new byte[] { -128 }, 0, 1); stream.write(output, 0, output.length); return stream.toByteArray(); } } return bData; }
From source file:org.ajax4jsf.resource.ResourceBuilderImpl.java
protected byte[] encrypt(byte[] src) { try {/*from w ww . ja va 2s . c om*/ Deflater compressor = new Deflater(Deflater.BEST_SPEED); byte[] compressed = new byte[src.length + 100]; compressor.setInput(src); compressor.finish(); int totalOut = compressor.deflate(compressed); byte[] zipsrc = new byte[totalOut]; System.arraycopy(compressed, 0, zipsrc, 0, totalOut); compressor.end(); return codec.encode(zipsrc); } catch (Exception e) { throw new FacesException("Error encode resource data", e); } }
From source file:se.kth.infosys.lumberjack.protocol.LumberjackClient.java
public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException { output.writeByte(PROTOCOL_VERSION);/*from w w w . j a v a 2s . c om*/ output.writeByte(FRAME_COMPRESSED); ByteArrayOutputStream uncompressedBytes = new ByteArrayOutputStream(); DataOutputStream uncompressedOutput = new DataOutputStream(uncompressedBytes); for (Map<String, byte[]> keyValues : keyValuesList) { logger.trace("Adding data frame"); sendDataFrame(uncompressedOutput, keyValues); } uncompressedOutput.close(); Deflater compressor = new Deflater(); byte[] uncompressedData = uncompressedBytes.toByteArray(); logger.trace("Deflating data: {} bytes", uncompressedData.length); if (logger.isTraceEnabled()) { HexDump.dump(uncompressedData, 0, System.out, 0); } compressor.setInput(uncompressedData); compressor.finish(); ByteArrayOutputStream compressedBytes = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buffer); compressedBytes.write(buffer, 0, count); } compressedBytes.close(); byte[] compressedData = compressedBytes.toByteArray(); logger.trace("Deflated data: {} bytes", compressor.getTotalOut()); if (logger.isTraceEnabled()) { HexDump.dump(compressedData, 0, System.out, 0); } output.writeInt(compressor.getTotalOut()); output.write(compressedData); output.flush(); logger.trace("Sending compressed frame: {} frames", keyValuesList.size()); return 6 + compressor.getTotalOut(); }
From source file:com.hichinaschool.flashcards.libanki.Utils.java
/** * Compress data./* w ww . j av a 2 s. c om*/ * @param bytesToCompress is the byte array to compress. * @return a compressed byte array. * @throws java.io.IOException */ public static byte[] compress(byte[] bytesToCompress, int comp) throws IOException { // Compressor with highest level of compression. Deflater compressor = new Deflater(comp, true); // Give the compressor the data to compress. compressor.setInput(bytesToCompress); compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(bytesToCompress.length); // Compress the data byte[] buf = new byte[65536]; while (!compressor.finished()) { bos.write(buf, 0, compressor.deflate(buf)); } bos.close(); // Get the compressed data return bos.toByteArray(); }