List of usage examples for java.util.zip Deflater finish
boolean finish
To view the source code for java.util.zip Deflater finish.
Click Source Link
From source file:Main.java
public static void main(String[] args) throws Exception { // main method // Encode a String into bytes String inputString = "this is a test"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output1 = new byte[input.length]; Deflater compresser = new Deflater(); compresser.setInput(input);/*from w ww . j a v a2 s .c o m*/ compresser.finish(); int compressedDataLength = compresser.deflate(output1); compresser.end(); String str = new String(Base64.getEncoder().encode(output1)); System.out.println("Deflated String:" + str); byte[] output2 = Base64.getDecoder().decode(str); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output2); byte[] result = str.getBytes(); int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); System.out.println("Deflated String:" + outputString); }
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { byte[] input = "asdf".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//from www . j ava 2 s. co m compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "www.java2s.com".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//from w w w .j a v a 2 s. c om compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); System.out.println(Arrays.toString(compressedData)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "this is a test".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//from w w w. ja v a 2 s . c o m compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); Inflater decompressor = new Inflater(); decompressor.setInput(compressedData); bos = new ByteArrayOutputStream(compressedData.length); buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] decompressedData = bos.toByteArray(); System.out.println(new String(decompressedData)); }
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 w w . j a v a 2 s . co m 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. //www . j ava 2 s . c o m * * @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
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 av a 2s. c om*/ 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:Main.java
public static byte[] compress(byte[] data, int level) throws IOException { if (data == null || data.length == 0) { return data; }//from ww w .j av a 2 s .co m ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setLevel(level); deflater.setInput(data); deflater.finish(); byte[] buf = new byte[BUFFER_SIZE]; while (!deflater.finished()) { int count = deflater.deflate(buf); bout.write(buf, 0, count); } deflater.end(); bout.close(); return bout.toByteArray(); }
From source file:Main.java
public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException { Deflater compresser = new Deflater(); compresser.setInput(originalData, offset, length); compresser.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(length); int count;/* w ww . j a v a 2s. co m*/ byte[] buf = new byte[1024]; while (!compresser.finished()) { count = compresser.deflate(buf); bos.write(buf, 0, count); } compresser.end(); byte[] compressData = bos.toByteArray(); bos.close(); return compressData; }
From source file:Main.java
public static byte[] zipCompress(byte[] input, int level) { Deflater compressor = new Deflater(); compressor.setLevel(level);// w w w . j ava 2 s .c o m compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } return bos.toByteArray(); }