KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > scanning > ZipArchiveInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.javacore.scanning;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29 /**
30  *
31  * @author thurka
32  */

33 class ZipArchiveInfo {
34     ZipFile JavaDoc zipFile;
35     String JavaDoc offset;
36     Map JavaDoc directoryMap=new HashMap JavaDoc(1000);
37     ZipDirInfo rootInfo;
38
39     /** Creates a new instance of ZipArchiveInfo */
40     ZipArchiveInfo(File JavaDoc f, String JavaDoc offset) throws IOException JavaDoc {
41         zipFile=new ZipFile JavaDoc(f);
42         this.offset = offset;
43         rootInfo=new ZipDirInfo("",""); // NOI18N
44
}
45     
46     void close() throws IOException JavaDoc {
47         if (zipFile != null) zipFile.close();
48     }
49     
50     FileInfo getRootFileInfo() {
51         Enumeration JavaDoc en=zipFile.entries();
52         
53         while(en.hasMoreElements()) {
54             ZipEntry JavaDoc entry=(ZipEntry JavaDoc)en.nextElement();
55             if (!entry.isDirectory()) {
56                 String JavaDoc name=entry.getName();
57                 if (offset != null) {
58                     if (name.startsWith(offset)) {
59                         name = name.substring(offset.length());
60                     }
61                     else {
62                         continue;
63                     }
64                 }
65                 int lastSlashIndex = name.lastIndexOf('/');
66                 String JavaDoc directoryName="";
67                 String JavaDoc shortName=name;
68                 
69                 if (lastSlashIndex > -1) {
70                     directoryName=name.substring(0, lastSlashIndex);
71                     shortName=name.substring(lastSlashIndex+1);
72                 }
73                 getDirectory(directoryName).addEntry(zipFile,entry,shortName,name);
74             }
75         }
76         return rootInfo;
77     }
78     
79     private ZipDirInfo getDirectory(String JavaDoc dirName) {
80         ZipDirInfo zipDir=(ZipDirInfo)directoryMap.get(dirName);
81         
82         if (zipDir==null){
83             ZipDirInfo parentInfo;
84             int lastSlashIndex = dirName.lastIndexOf('/');
85             String JavaDoc shortName=dirName;
86             if (lastSlashIndex > -1) {
87                 String JavaDoc parentDirName = dirName.substring(0, lastSlashIndex);
88                 shortName=dirName.substring(lastSlashIndex+1);
89                 parentInfo=getDirectory(parentDirName);
90             } else {
91                 parentInfo=rootInfo;
92             }
93             zipDir=new ZipDirInfo(dirName,shortName);
94             parentInfo.addDir(zipDir);
95             directoryMap.put(dirName,zipDir);
96         }
97         return zipDir;
98     }
99 }
100
Popular Tags