Home | History | Annotate | Download | only in src
      1 /*
      2  * XZDecDemo
      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  * Decompresses .xz files to standard output. If no arguments are given,
     15  * reads from standard input.
     16  */
     17 class XZDecDemo {
     18     public static void main(String[] args) {
     19         byte[] buf = new byte[8192];
     20         String name = null;
     21 
     22         try {
     23             if (args.length == 0) {
     24                 name = "standard input";
     25                 InputStream in = new XZInputStream(System.in);
     26 
     27                 int size;
     28                 while ((size = in.read(buf)) != -1)
     29                     System.out.write(buf, 0, size);
     30 
     31             } else {
     32                 // Read from files given on the command line.
     33                 for (int i = 0; i < args.length; ++i) {
     34                     name = args[i];
     35                     InputStream in = new FileInputStream(name);
     36 
     37                     try {
     38                         // Since XZInputStream does some buffering internally
     39                         // anyway, BufferedInputStream doesn't seem to be
     40                         // needed here to improve performance.
     41                         // in = new BufferedInputStream(in);
     42                         in = new XZInputStream(in);
     43 
     44                         int size;
     45                         while ((size = in.read(buf)) != -1)
     46                             System.out.write(buf, 0, size);
     47 
     48                     } finally {
     49                         // Close FileInputStream (directly or indirectly
     50                         // via XZInputStream, it doesn't matter).
     51                         in.close();
     52                     }
     53                 }
     54             }
     55         } catch (FileNotFoundException e) {
     56             System.err.println("XZDecDemo: Cannot open " + name + ": "
     57                                + e.getMessage());
     58             System.exit(1);
     59 
     60         } catch (EOFException e) {
     61             System.err.println("XZDecDemo: Unexpected end of input on "
     62                                + name);
     63             System.exit(1);
     64 
     65         } catch (IOException e) {
     66             System.err.println("XZDecDemo: Error decompressing from "
     67                                + name + ": " + e.getMessage());
     68             System.exit(1);
     69         }
     70     }
     71 }
     72