List of usage examples for java.util.zip Deflater reset
public void reset()
From source file:Main.java
public static void main(String[] args) throws IOException { Deflater def = new Deflater(); byte[] input = new byte[1024]; byte[] output = new byte[1024]; FileInputStream fin = new FileInputStream("a.dat"); FileOutputStream fout = new FileOutputStream("b.dat"); int numRead = fin.read(input); def.setInput(input, 0, numRead);/*w ww . ja v a2s . c om*/ while (!def.needsInput()) { int numCompressedBytes = def.deflate(output, 0, output.length); if (numCompressedBytes > 0) { fout.write(output, 0, numCompressedBytes); } } def.finish(); fin.close(); fout.flush(); fout.close(); def.reset(); }
From source file:cz.muni.fi.xklinec.zipstream.App.java
/** * Entry point. /* w w w . j a v a 2s. 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:NCDSearch.DistributedNCDSearch.java
public static float NCD(byte[] file, byte[] target, int compression) { Deflater compressor = new Deflater(compression); //This is where we dump our compressed bytes. All we need is the size of this thing. //TODO: In theory the compressed bytes could exceed the length of the target files... byte[] outputtrash = new byte[file.length + target.length]; int bothcompressedsize; int filecompressedsize; int targetcompressedsize; //puts the target file and the searched file together. byte[] both = new byte[file.length + target.length]; for (int i = 0; i < file.length; i++) { both[i] = file[i];/* www .j a v a2s . com*/ } 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 (float) (bothcompressedsize - filecompressedsize) / (float) targetcompressedsize; }
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];/*from w w w.j av a 2 s .c om*/ } 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:org.apache.tez.common.TezCommonUtils.java
@Private public static ByteString compressByteArrayToByteString(byte[] inBytes, Deflater deflater) throws IOException { deflater.reset(); ByteString.Output os = ByteString.newOutput(); DeflaterOutputStream compressOs = null; try {//from w w w . j a va2s . co m compressOs = new DeflaterOutputStream(os, deflater); compressOs.write(inBytes); compressOs.finish(); ByteString byteString = os.toByteString(); return byteString; } finally { if (compressOs != null) { compressOs.close(); } } }
From source file:org.dragonet.net.ClientChunkManager.java
/** * Send a single chunk to the client/* w w w . jav a 2s. c o m*/ * * @param chunkX The chunk X coordinate * @param chunkZ The chunk Z coordinate */ private synchronized void sendChunk(int chunkX, int chunkZ) { try { GlowChunkSnapshot chunk = this.getSession().getPlayer().getWorld().getChunkAt(chunkX, chunkZ) .getChunkSnapshot(); ByteArrayOutputStream totalData = new ByteArrayOutputStream(); PEBinaryWriter writer = new PEBinaryWriter(totalData); if (writer.getEndianness() == PEBinaryUtils.BIG_ENDIAN) { writer.switchEndianness(); } writer.writeInt(chunkX); writer.writeInt(chunkZ); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { for (int y = 0; y < 128; y++) { writer.writeByte((byte) (this.getSession().getTranslator() .translateBlockToPE(chunk.getBlockTypeId(x, y, z)) & 0xFF)); } } } writer.write(new byte[16384]); for (int i = 0; i < 16384; i++) { writer.writeByte((byte) 0xF0); } for (int i = 0; i < 16384; i++) { writer.writeByte((byte) 0x11); } for (int i = 0; i < 256; i++) { writer.writeByte((byte) 0x00); } for (int i = 0; i < 256; i++) { writer.writeByte((byte) 0x00); writer.writeByte((byte) 0x85); writer.writeByte((byte) 0xB2); writer.writeByte((byte) 0x4A); } Deflater deflater = new Deflater(2); deflater.reset(); deflater.setInput(totalData.toByteArray()); deflater.finish(); byte[] bufferDeflate = new byte[65536]; int deflatedSize = deflater.deflate(bufferDeflate); FullChunkPacket packet = new FullChunkPacket(); packet.compressedData = ArrayUtils.subarray(bufferDeflate, 0, deflatedSize); this.getSession().send(packet); } catch (IOException e) { } }