1 | /*
|
---|
2 | * Copyright 2000-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 | import java.io.FileInputStream;
|
---|
21 | import java.io.FileOutputStream;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.util.zip.GZIPInputStream;
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Expands a file that has been compressed with the GZIP
|
---|
28 | * algorithm. Normally used to compress non-compressed archives such
|
---|
29 | * as TAR files.
|
---|
30 | *
|
---|
31 | * @since Ant 1.1
|
---|
32 | *
|
---|
33 | * @ant.task category="packaging"
|
---|
34 | */
|
---|
35 |
|
---|
36 | public class GUnzip extends Unpack {
|
---|
37 |
|
---|
38 | private static final String DEFAULT_EXTENSION = ".gz";
|
---|
39 |
|
---|
40 | protected String getDefaultExtension() {
|
---|
41 | return DEFAULT_EXTENSION;
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected void extract() {
|
---|
45 | if (source.lastModified() > dest.lastModified()) {
|
---|
46 | log("Expanding " + source.getAbsolutePath() + " to "
|
---|
47 | + dest.getAbsolutePath());
|
---|
48 |
|
---|
49 | FileOutputStream out = null;
|
---|
50 | GZIPInputStream zIn = null;
|
---|
51 | FileInputStream fis = null;
|
---|
52 | try {
|
---|
53 | out = new FileOutputStream(dest);
|
---|
54 | fis = new FileInputStream(source);
|
---|
55 | zIn = new GZIPInputStream(fis);
|
---|
56 | byte[] buffer = new byte[8 * 1024];
|
---|
57 | int count = 0;
|
---|
58 | do {
|
---|
59 | out.write(buffer, 0, count);
|
---|
60 | count = zIn.read(buffer, 0, buffer.length);
|
---|
61 | } while (count != -1);
|
---|
62 | } catch (IOException ioe) {
|
---|
63 | String msg = "Problem expanding gzip " + ioe.getMessage();
|
---|
64 | throw new BuildException(msg, ioe, getLocation());
|
---|
65 | } finally {
|
---|
66 | if (fis != null) {
|
---|
67 | try {
|
---|
68 | fis.close();
|
---|
69 | } catch (IOException ioex) {
|
---|
70 | //ignore
|
---|
71 | }
|
---|
72 | }
|
---|
73 | if (out != null) {
|
---|
74 | try {
|
---|
75 | out.close();
|
---|
76 | } catch (IOException ioex) {
|
---|
77 | //ignore
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if (zIn != null) {
|
---|
81 | try {
|
---|
82 | zIn.close();
|
---|
83 | } catch (IOException ioex) {
|
---|
84 | //ignore
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|