001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.Enumeration;
024: import java.util.Map;
025: import java.util.zip.ZipException;
026:
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.types.resources.FileResource;
029: import org.apache.tools.ant.types.resources.ZipResource;
030: import org.apache.tools.zip.ZipEntry;
031: import org.apache.tools.zip.ZipFile;
032:
033: /**
034: * Scans zip archives for resources.
035: */
036: public class ZipScanner extends ArchiveScanner {
037:
038: /**
039: * Fills the file and directory maps with resources read from the
040: * archive.
041: *
042: * @param src the archive to scan.
043: * @param encoding encoding used to encode file names inside the archive.
044: * @param fileEntries Map (name to resource) of non-directory
045: * resources found inside the archive.
046: * @param matchFileEntries Map (name to resource) of non-directory
047: * resources found inside the archive that matched all include
048: * patterns and didn't match any exclude patterns.
049: * @param dirEntries Map (name to resource) of directory
050: * resources found inside the archive.
051: * @param matchDirEntries Map (name to resource) of directory
052: * resources found inside the archive that matched all include
053: * patterns and didn't match any exclude patterns.
054: */
055: protected void fillMapsFromArchive(Resource src, String encoding,
056: Map fileEntries, Map matchFileEntries, Map dirEntries,
057: Map matchDirEntries) {
058: ZipEntry entry = null;
059: ZipFile zf = null;
060:
061: File srcFile = null;
062: if (src instanceof FileResource) {
063: srcFile = ((FileResource) src).getFile();
064: } else {
065: throw new BuildException(
066: "only file resources are supported");
067: }
068:
069: try {
070: try {
071: zf = new ZipFile(srcFile, encoding);
072: } catch (ZipException ex) {
073: throw new BuildException("problem reading " + srcFile,
074: ex);
075: } catch (IOException ex) {
076: throw new BuildException("problem opening " + srcFile,
077: ex);
078: }
079: Enumeration e = zf.getEntries();
080: while (e.hasMoreElements()) {
081: entry = (ZipEntry) e.nextElement();
082: Resource r = new ZipResource(srcFile, encoding, entry);
083: String name = entry.getName();
084: if (entry.isDirectory()) {
085: name = trimSeparator(name);
086: dirEntries.put(name, r);
087: if (match(name)) {
088: matchDirEntries.put(name, r);
089: }
090: } else {
091: fileEntries.put(name, r);
092: if (match(name)) {
093: matchFileEntries.put(name, r);
094: }
095: }
096: }
097: } finally {
098: if (zf != null) {
099: try {
100: zf.close();
101: } catch (IOException ex) {
102: // swallow
103: }
104: }
105: }
106: }
107: }
|