KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > GUnzip


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.zip.GZIPInputStream JavaDoc;
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 JavaDoc DEFAULT_EXTENSION = ".gz";
41
42     /**
43      * Get the default extension.
44      * @return the value ".gz"
45      */

46     protected String JavaDoc 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 JavaDoc out = null;
59             GZIPInputStream JavaDoc zIn = null;
60             InputStream JavaDoc fis = null;
61             try {
62                 out = new FileOutputStream JavaDoc(dest);
63                 fis = srcResource.getInputStream();
64                 zIn = new GZIPInputStream JavaDoc(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 JavaDoc ioe) {
72                 String JavaDoc msg = "Problem expanding gzip " + ioe.getMessage();
73                 throw new BuildException(msg, ioe, getLocation());
74             } finally {
75                 FileUtils.close(fis);
76                 FileUtils.close(out);
77                 FileUtils.close(zIn);
78             }
79         }
80     }
81
82     /**
83      * Whether this task can deal with non-file resources.
84      *
85      * <p>This implementation returns true only if this task is
86      * &lt;gunzip&gt;. Any subclass of this class that also wants to
87      * support non-file resources needs to override this method. We
88      * need to do so for backwards compatibility reasons since we
89      * can't expect subclasses to support resources.</p>
90      * @return true if this task supports non file resources.
91      * @since Ant 1.7
92      */

93     protected boolean supportsNonFileResources() {
94         return getClass().equals(GUnzip.class);
95     }
96 }
97
Popular Tags