01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.taskdefs;
20:
21: import java.io.FileOutputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.util.zip.GZIPInputStream;
25: import org.apache.tools.ant.BuildException;
26: import org.apache.tools.ant.util.FileUtils;
27:
28: /**
29: * Expands a file that has been compressed with the GZIP
30: * algorithm. Normally used to compress non-compressed archives such
31: * as TAR files.
32: *
33: * @since Ant 1.1
34: *
35: * @ant.task category="packaging"
36: */
37:
38: public class GUnzip extends Unpack {
39:
40: private static final String DEFAULT_EXTENSION = ".gz";
41:
42: /**
43: * Get the default extension.
44: * @return the value ".gz"
45: */
46: protected String getDefaultExtension() {
47: return DEFAULT_EXTENSION;
48: }
49:
50: /**
51: * Implement the gunzipping.
52: */
53: protected void extract() {
54: if (source.lastModified() > dest.lastModified()) {
55: log("Expanding " + source.getAbsolutePath() + " to "
56: + dest.getAbsolutePath());
57:
58: FileOutputStream out = null;
59: GZIPInputStream zIn = null;
60: InputStream fis = null;
61: try {
62: out = new FileOutputStream(dest);
63: fis = srcResource.getInputStream();
64: zIn = new GZIPInputStream(fis);
65: byte[] buffer = new byte[8 * 1024];
66: int count = 0;
67: do {
68: out.write(buffer, 0, count);
69: count = zIn.read(buffer, 0, buffer.length);
70: } while (count != -1);
71: } catch (IOException ioe) {
72: String msg = "Problem expanding gzip "
73: + ioe.getMessage();
74: throw new BuildException(msg, ioe, getLocation());
75: } finally {
76: FileUtils.close(fis);
77: FileUtils.close(out);
78: FileUtils.close(zIn);
79: }
80: }
81: }
82:
83: /**
84: * Whether this task can deal with non-file resources.
85: *
86: * <p>This implementation returns true only if this task is
87: * <gunzip>. Any subclass of this class that also wants to
88: * support non-file resources needs to override this method. We
89: * need to do so for backwards compatibility reasons since we
90: * can't expect subclasses to support resources.</p>
91: * @return true if this task supports non file resources.
92: * @since Ant 1.7
93: */
94: protected boolean supportsNonFileResources() {
95: return getClass().equals(GUnzip.class);
96: }
97: }
|