List of usage examples for java.util.zip Deflater Deflater
public Deflater(int level, boolean nowrap)
From source file:cz.muni.fi.xklinec.zipstream.App.java
/** * Entry point. /* ww w. j a v a 2 s . c om*/ * * @param args * @throws FileNotFoundException * @throws IOException * @throws NoSuchFieldException * @throws ClassNotFoundException * @throws NoSuchMethodException */ public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, InterruptedException { OutputStream fos = null; InputStream fis = null; if ((args.length != 0 && args.length != 2)) { System.err.println(String.format("Usage: app.jar source.apk dest.apk")); return; } else if (args.length == 2) { System.err.println( String.format("Will use file [%s] as input file and [%s] as output file", args[0], args[1])); fis = new FileInputStream(args[0]); fos = new FileOutputStream(args[1]); } else if (args.length == 0) { System.err.println(String.format("Will use file [STDIN] as input file and [STDOUT] as output file")); fis = System.in; fos = System.out; } final Deflater def = new Deflater(9, true); ZipArchiveInputStream zip = new ZipArchiveInputStream(fis); // List of postponed entries for further "processing". List<PostponedEntry> peList = new ArrayList<PostponedEntry>(6); // Output stream ZipArchiveOutputStream zop = new ZipArchiveOutputStream(fos); zop.setLevel(9); // Read the archive ZipArchiveEntry ze = zip.getNextZipEntry(); while (ze != null) { ZipExtraField[] extra = ze.getExtraFields(true); byte[] lextra = ze.getLocalFileDataExtra(); UnparseableExtraFieldData uextra = ze.getUnparseableExtraFieldData(); byte[] uextrab = uextra != null ? uextra.getLocalFileDataData() : null; // ZipArchiveOutputStream.DEFLATED // // Data for entry byte[] byteData = Utils.readAll(zip); byte[] deflData = new byte[0]; int infl = byteData.length; int defl = 0; // If method is deflated, get the raw data (compress again). if (ze.getMethod() == ZipArchiveOutputStream.DEFLATED) { def.reset(); def.setInput(byteData); def.finish(); byte[] deflDataTmp = new byte[byteData.length * 2]; defl = def.deflate(deflDataTmp); deflData = new byte[defl]; System.arraycopy(deflDataTmp, 0, deflData, 0, defl); } System.err.println(String.format( "ZipEntry: meth=%d " + "size=%010d isDir=%5s " + "compressed=%07d extra=%d lextra=%d uextra=%d " + "comment=[%s] " + "dataDesc=%s " + "UTF8=%s " + "infl=%07d defl=%07d " + "name [%s]", ze.getMethod(), ze.getSize(), ze.isDirectory(), ze.getCompressedSize(), extra != null ? extra.length : -1, lextra != null ? lextra.length : -1, uextrab != null ? uextrab.length : -1, ze.getComment(), ze.getGeneralPurposeBit().usesDataDescriptor(), ze.getGeneralPurposeBit().usesUTF8ForNames(), infl, defl, ze.getName())); final String curName = ze.getName(); // META-INF files should be always on the end of the archive, // thus add postponed files right before them if (curName.startsWith("META-INF") && peList.size() > 0) { System.err.println( "Now is the time to put things back, but at first, I'll perform some \"facelifting\"..."); // Simulate som evil being done Thread.sleep(5000); System.err.println("OK its done, let's do this."); for (PostponedEntry pe : peList) { System.err.println( "Adding postponed entry at the end of the archive! deflSize=" + pe.deflData.length + "; inflSize=" + pe.byteData.length + "; meth: " + pe.ze.getMethod()); pe.dump(zop, false); } peList.clear(); } // Capturing interesting files for us and store for later. // If the file is not interesting, send directly to the stream. if ("classes.dex".equalsIgnoreCase(curName) || "AndroidManifest.xml".equalsIgnoreCase(curName)) { System.err.println("### Interesting file, postpone sending!!!"); PostponedEntry pe = new PostponedEntry(ze, byteData, deflData); peList.add(pe); } else { // Write ZIP entry to the archive zop.putArchiveEntry(ze); // Add file data to the stream zop.write(byteData, 0, infl); zop.closeArchiveEntry(); } ze = zip.getNextZipEntry(); } // Cleaning up stuff zip.close(); fis.close(); zop.finish(); zop.close(); fos.close(); System.err.println("THE END!"); }
From source file:Main.java
/** * DEFLATEs the specified input data.//from w w w . j a v a2 s . c om * * @param data the input data * @param dictionary the dictionary, or null if none * @return the compressed data */ public static byte[] deflate(byte[] data, byte[] dictionary) { Deflater deflater = new Deflater(8, true); if (dictionary != null) { deflater.setDictionary(dictionary); } deflater.setInput(data); deflater.finish(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (!deflater.finished()) { int n = deflater.deflate(buffer); byteArrayOutputStream.write(buffer, 0, n); } byte[] result = byteArrayOutputStream.toByteArray(); return result; }
From source file:org.xdi.zip.CompressionHelper.java
public static byte[] deflate(byte[] data, boolean nowrap) throws IOException { Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap); deflater.setInput(data);/*from w w w. ja va2s .c om*/ deflater.finish(); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); os.write(buffer, 0, count); } } finally { IOUtils.closeQuietly(os); } return os.toByteArray(); }
From source file:Main.java
public static byte[] compress(byte[] input, int compressionLevel, boolean GZIPFormat) throws IOException { Deflater compressor = new Deflater(compressionLevel, GZIPFormat); compressor.setInput(input);/*from ww w . j a va2 s . c o m*/ compressor.finish(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); byte[] readBuffer = new byte[1024]; int readCount = 0; while (!compressor.finished()) { readCount = compressor.deflate(readBuffer); if (readCount > 0) { bao.write(readBuffer, 0, readCount); } } compressor.end(); return bao.toByteArray(); }
From source file:com.hundsun.jresplus.web.nosession.cookie.HessianZipSerializer.java
public static byte[] encode(Object object) throws SerializationException { if (object == null) { return null; }//from w w w . j av a 2s .com ByteArrayOutputStream baos = new ByteArrayOutputStream(); Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false); DeflaterOutputStream dos = new DeflaterOutputStream(baos, def); Hessian2Output ho = null; try { ho = new Hessian2Output(dos); ho.writeObject(object); } catch (Exception e) { throw new SerializationException("Failed to encode date", e); } finally { if (ho != null) { try { ho.close(); } catch (IOException e) { } } try { dos.close(); } catch (IOException e) { } def.end(); } return baos.toByteArray(); }
From source file:v7db.files.Compression.java
/** * @return 0, if the "deflated" data fills the whole output array */// w w w . j av a 2 s . c om static int deflate(byte[] data, int off, int len, byte[] out) { Deflater deflater = new Deflater(BEST_COMPRESSION, true); deflater.setInput(data, off, len); deflater.finish(); int size = deflater.deflate(out); if (size == 0 || size == out.length) return 0; return size; }
From source file:ZipUtil.java
/** * Deflates the file and returns the deflated file. *///w ww.j a v a 2 s.c o m public static byte[] zipByteArray(byte[] file) throws IOException { byte[] byReturn = null; Deflater oDeflate = new Deflater(Deflater.DEFLATED, false); oDeflate.setInput(file); oDeflate.finish(); ByteArrayOutputStream oZipStream = new ByteArrayOutputStream(); try { while (!oDeflate.finished()) { byte[] byRead = new byte[ZIP_BUFFER_SIZE]; int iBytesRead = oDeflate.deflate(byRead); if (iBytesRead == byRead.length) { oZipStream.write(byRead); } else { oZipStream.write(byRead, 0, iBytesRead); } } oDeflate.end(); byReturn = oZipStream.toByteArray(); } finally { oZipStream.close(); } return byReturn; }
From source file:org.openmrs.module.shr.contenthandler.api.ContentTest.java
private static byte[] compressDeflate(String content) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DeflaterOutputStream deflateOut = new DeflaterOutputStream(out, new Deflater(0, true)); deflateOut.write(content.getBytes()); IOUtils.closeQuietly(deflateOut);/* w w w . j a v a 2 s. c om*/ return out.toByteArray(); }
From source file:org.samlsnort.util.EncodingTool.java
public static String deflateToBase64(String inflated) { Deflater deflater = new Deflater(Deflater.DEFLATED, true); deflater.setInput(inflated.getBytes(CHARSET)); deflater.finish();/*from w w w . java 2s . c o m*/ byte[] deflatedBytes = new byte[2048]; int len = deflater.deflate(deflatedBytes); return new String(Base64.encodeBase64(Arrays.copyOf(deflatedBytes, len)), CHARSET); }
From source file:fr.eoit.util.dumper.DumperTest.java
@Test public void testCompression() { byte[] strBytes = COPYRIGHTS.getBytes(); byte[] output = new byte[8096]; Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true); compresser.setInput(strBytes);/*from w w w. j av a2s. c o m*/ compresser.finish(); int compressedDataLength = compresser.deflate(output); compresser.end(); String inputString = new String(Hex.encodeHex(strBytes)); String hexString = new String(Arrays.copyOf(output, compressedDataLength)); int i = 0; i++; }