Home | History | Annotate | Download | only in src
      1 /*
      2  * LZMAEncDemo
      3  *
      4  * Author: Lasse Collin <lasse.collin (at) tukaani.org>
      5  *
      6  * This file has been put into the public domain.
      7  * You can do whatever you want with this file.
      8  */
      9 
     10 import java.io.*;
     11 import org.tukaani.xz.*;
     12 
     13 /**
     14  * Compresses a single file from standard input to standard ouput into
     15  * the .lzma file format.
     16  * <p>
     17  * NOTE: For most purposes, .lzma is a legacy format and usually you should
     18  * use .xz instead.
     19  * <p>
     20  * Two optional arguments are supported:
     21  * <ol>
     22  *   <li>LZMA preset level which is an integer in the range [0, 9].
     23  *       The default is 6.</li>
     24  *   <li>Uncompressed size of the input as bytes.<li>
     25  * </ol>
     26  */
     27 class LZMAEncDemo {
     28     public static void main(String[] args) throws Exception {
     29         LZMA2Options options = new LZMA2Options();
     30         long inputSize = -1;
     31 
     32         if (args.length >= 1)
     33             options.setPreset(Integer.parseInt(args[0]));
     34 
     35         if (args.length >= 2)
     36             inputSize = Long.parseLong(args[1]);
     37 
     38         System.err.println("Encoder memory usage: "
     39                            + options.getEncoderMemoryUsage() + " KiB");
     40         System.err.println("Decoder memory usage: "
     41                            + options.getDecoderMemoryUsage() + " KiB");
     42 
     43         // LZMAOutputStream writes one byte at a time. It helps a little,
     44         // especially in the fastest presets, to use BufferedOutputStream.
     45         OutputStream out = new BufferedOutputStream(System.out);
     46         LZMAOutputStream encoder = new LZMAOutputStream(out, options,
     47                                                         inputSize);
     48 
     49         byte[] buf = new byte[8192];
     50         int size;
     51         while ((size = System.in.read(buf)) != -1)
     52             encoder.write(buf, 0, size);
     53 
     54         encoder.finish();
     55         out.flush();
     56     }
     57 }
     58