• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * LZMADecDemo
3  *
4  * Author: Lasse Collin <lasse.collin@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 .lzma files to standard output. If no arguments are given,
15  * reads from standard input.
16  *
17  * NOTE: For most purposes, .lzma is a legacy format and usually you should
18  * use .xz instead.
19  */
20 class LZMADecDemo {
main(String[] args)21     public static void main(String[] args) {
22         byte[] buf = new byte[8192];
23         String name = null;
24 
25         try {
26             if (args.length == 0) {
27                 name = "standard input";
28 
29                 // No need to use BufferedInputStream with System.in which
30                 // seems to be fast with one-byte reads.
31                 InputStream in = new LZMAInputStream(System.in);
32 
33                 int size;
34                 while ((size = in.read(buf)) != -1)
35                     System.out.write(buf, 0, size);
36 
37             } else {
38                 // Read from files given on the command line.
39                 for (int i = 0; i < args.length; ++i) {
40                     name = args[i];
41                     InputStream in = new FileInputStream(name);
42 
43                     try {
44                         // In contrast to other classes in org.tukaani.xz,
45                         // LZMAInputStream doesn't do buffering internally
46                         // and reads one byte at a time. BufferedInputStream
47                         // gives a huge performance improvement here but even
48                         // then it's slower than the other input streams from
49                         // org.tukaani.xz.
50                         in = new BufferedInputStream(in);
51                         in = new LZMAInputStream(in);
52 
53                         int size;
54                         while ((size = in.read(buf)) != -1)
55                             System.out.write(buf, 0, size);
56 
57                     } finally {
58                         // Close FileInputStream (directly or indirectly
59                         // via LZMAInputStream, it doesn't matter).
60                         in.close();
61                     }
62                 }
63             }
64         } catch (FileNotFoundException e) {
65             System.err.println("LZMADecDemo: Cannot open " + name + ": "
66                                + e.getMessage());
67             System.exit(1);
68 
69         } catch (EOFException e) {
70             System.err.println("LZMADecDemo: Unexpected end of input on "
71                                + name);
72             System.exit(1);
73 
74         } catch (IOException e) {
75             System.err.println("LZMADecDemo: Error decompressing from "
76                                + name + ": " + e.getMessage());
77             System.exit(1);
78         }
79     }
80 }
81