1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 |
|
---|
21 | import java.io.BufferedInputStream;
|
---|
22 | import java.io.FileInputStream;
|
---|
23 | import java.io.FileOutputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.bzip2.CBZip2InputStream;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Expands a file that has been compressed with the BZIP2
|
---|
30 | * algorithm. Normally used to compress non-compressed archives such
|
---|
31 | * as TAR files.
|
---|
32 | *
|
---|
33 | *
|
---|
34 | * @since Ant 1.5
|
---|
35 | *
|
---|
36 | * @ant.task category="packaging"
|
---|
37 | */
|
---|
38 |
|
---|
39 | public class BUnzip2 extends Unpack {
|
---|
40 |
|
---|
41 | private static final String DEFAULT_EXTENSION = ".bz2";
|
---|
42 |
|
---|
43 | protected String getDefaultExtension() {
|
---|
44 | return DEFAULT_EXTENSION;
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected void extract() {
|
---|
48 | if (source.lastModified() > dest.lastModified()) {
|
---|
49 | log("Expanding " + source.getAbsolutePath() + " to "
|
---|
50 | + dest.getAbsolutePath());
|
---|
51 |
|
---|
52 | FileOutputStream out = null;
|
---|
53 | CBZip2InputStream zIn = null;
|
---|
54 | FileInputStream fis = null;
|
---|
55 | BufferedInputStream bis = null;
|
---|
56 | try {
|
---|
57 | out = new FileOutputStream(dest);
|
---|
58 | fis = new FileInputStream(source);
|
---|
59 | bis = new BufferedInputStream(fis);
|
---|
60 | int b = bis.read();
|
---|
61 | if (b != 'B') {
|
---|
62 | throw new BuildException("Invalid bz2 file.", getLocation());
|
---|
63 | }
|
---|
64 | b = bis.read();
|
---|
65 | if (b != 'Z') {
|
---|
66 | throw new BuildException("Invalid bz2 file.", getLocation());
|
---|
67 | }
|
---|
68 | zIn = new CBZip2InputStream(bis);
|
---|
69 | byte[] buffer = new byte[8 * 1024];
|
---|
70 | int count = 0;
|
---|
71 | do {
|
---|
72 | out.write(buffer, 0, count);
|
---|
73 | count = zIn.read(buffer, 0, buffer.length);
|
---|
74 | } while (count != -1);
|
---|
75 | } catch (IOException ioe) {
|
---|
76 | String msg = "Problem expanding bzip2 " + ioe.getMessage();
|
---|
77 | throw new BuildException(msg, ioe, getLocation());
|
---|
78 | } finally {
|
---|
79 | if (bis != null) {
|
---|
80 | try {
|
---|
81 | bis.close();
|
---|
82 | } catch (IOException ioex) {
|
---|
83 | // ignore
|
---|
84 | }
|
---|
85 | }
|
---|
86 | if (fis != null) {
|
---|
87 | try {
|
---|
88 | fis.close();
|
---|
89 | } catch (IOException ioex) {
|
---|
90 | // ignore
|
---|
91 | }
|
---|
92 | }
|
---|
93 | if (out != null) {
|
---|
94 | try {
|
---|
95 | out.close();
|
---|
96 | } catch (IOException ioex) {
|
---|
97 | // ignore
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (zIn != null) {
|
---|
101 | try {
|
---|
102 | zIn.close();
|
---|
103 | } catch (IOException ioex) {
|
---|
104 | // ignore
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|