KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > ZipScanner


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.types;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.zip.ZipException JavaDoc;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.types.resources.FileResource;
29 import org.apache.tools.ant.types.resources.ZipResource;
30 import org.apache.tools.zip.ZipEntry;
31 import org.apache.tools.zip.ZipFile;
32
33 /**
34  * Scans zip archives for resources.
35  */

36 public class ZipScanner extends ArchiveScanner {
37
38     /**
39      * Fills the file and directory maps with resources read from the
40      * archive.
41      *
42      * @param src the archive to scan.
43      * @param encoding encoding used to encode file names inside the archive.
44      * @param fileEntries Map (name to resource) of non-directory
45      * resources found inside the archive.
46      * @param matchFileEntries Map (name to resource) of non-directory
47      * resources found inside the archive that matched all include
48      * patterns and didn't match any exclude patterns.
49      * @param dirEntries Map (name to resource) of directory
50      * resources found inside the archive.
51      * @param matchDirEntries Map (name to resource) of directory
52      * resources found inside the archive that matched all include
53      * patterns and didn't match any exclude patterns.
54      */

55     protected void fillMapsFromArchive(Resource src, String JavaDoc encoding,
56                                        Map JavaDoc fileEntries, Map JavaDoc matchFileEntries,
57                                        Map JavaDoc dirEntries, Map JavaDoc matchDirEntries) {
58         ZipEntry entry = null;
59         ZipFile zf = null;
60
61         File JavaDoc srcFile = null;
62         if (src instanceof FileResource) {
63             srcFile = ((FileResource) src).getFile();
64         } else {
65             throw new BuildException("only file resources are supported");
66         }
67
68         try {
69             try {
70                 zf = new ZipFile(srcFile, encoding);
71             } catch (ZipException JavaDoc ex) {
72                 throw new BuildException("problem reading " + srcFile, ex);
73             } catch (IOException JavaDoc ex) {
74                 throw new BuildException("problem opening " + srcFile, ex);
75             }
76             Enumeration JavaDoc e = zf.getEntries();
77             while (e.hasMoreElements()) {
78                 entry = (ZipEntry) e.nextElement();
79                 Resource r = new ZipResource(srcFile, encoding, entry);
80                 String JavaDoc name = entry.getName();
81                 if (entry.isDirectory()) {
82                     name = trimSeparator(name);
83                     dirEntries.put(name, r);
84                     if (match(name)) {
85                         matchDirEntries.put(name, r);
86                     }
87                 } else {
88                     fileEntries.put(name, r);
89                     if (match(name)) {
90                         matchFileEntries.put(name, r);
91                     }
92                 }
93             }
94         } finally {
95             if (zf != null) {
96                 try {
97                     zf.close();
98                 } catch (IOException JavaDoc ex) {
99                     // swallow
100
}
101             }
102         }
103     }
104 }
105
Popular Tags