Java by API/java.util.zip/Deflater

Материал из Java эксперт
Перейти к: навигация, поиск

Deflater.BEST_COMPRESSION

   <source lang="java">


import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] input = "asdf".getBytes();
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_COMPRESSION);
   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);
   }
   bos.close();
   byte[] compressedData = bos.toByteArray();
 }

}

 </source>
   
  
 
  



Deflater: deflate(byte[] b)

   <source lang="java">


import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] input = "asdf".getBytes();
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_COMPRESSION);
   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);
   }
   bos.close();
   byte[] compressedData = bos.toByteArray();
 }

}

 </source>
   
  
 
  



Deflater: finish()

   <source lang="java">


import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] input = "asdf".getBytes();
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_COMPRESSION);
   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);
   }
   bos.close();
   byte[] compressedData = bos.toByteArray();
 }

}

 </source>
   
  
 
  



Deflater: setInput(byte[] b)

   <source lang="java">


import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] input = "asdf".getBytes();
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_COMPRESSION);
   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);
   }
   bos.close();
   byte[] compressedData = bos.toByteArray();
 }

}

 </source>
   
  
 
  



Deflater: setLevel(int level)

   <source lang="java">


import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] input = "asdf".getBytes();
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_COMPRESSION);
   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);
   }
   bos.close();
   byte[] compressedData = bos.toByteArray();
 }

}

 </source>