View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  
23  package net.sf.jdiskcatalog.scanner;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Date;
28  import java.util.zip.ZipEntry;
29  import java.util.zip.ZipInputStream;
30  
31  import net.sf.jdiskcatalog.analysis.BasicFileAnalyser;
32  import net.sf.jdiskcatalog.analysis.CrcAnalyser;
33  import net.sf.jdiskcatalog.api.MutableNodeInfo;
34  import net.sf.jdiskcatalog.api.Node;
35  import net.sf.jdiskcatalog.api.NodeInfo;
36  import net.sf.jdiskcatalog.api.StreamScanner;
37  import net.sf.jdiskcatalog.model.NodeImpl;
38  
39  
40  public class ZipScanner implements StreamScanner
41  {
42  	public static final String KEY_COMPRESSEDSIZE = "compressedSize";
43  
44  	private StreamScanner scanner;
45  
46  	public void setScanner(StreamScanner scanner)
47  	{
48  		this.scanner = scanner;
49  	}
50  
51  	public boolean scan(InputStream stream, Node root) throws IOException
52  	{
53  		String filename = root.getNodeInfo().getName().toLowerCase();
54  		if (!filename.endsWith(".zip") && !filename.endsWith(".jar") && !filename.endsWith(".war"))
55  			return false;
56  		ZipInputStream zis = new ZipInputStream(stream);
57  		ZipEntry ze = zis.getNextEntry();
58  		while (ze != null)
59  		{
60  			add(ze, root, zis);
61  			ze = zis.getNextEntry();
62  		}
63  		return true;
64  	}
65  
66  	private void add(ZipEntry ze, Node root, InputStream stream) throws IOException
67  	{
68  		Node node = null;
69  		if (ze.isDirectory())
70  			node = addDir(ze.getName(), root);
71  		else
72  		{
73  			String name = ze.getName();
74  			int pos = name.lastIndexOf('/');
75  			Node dirNode = root;
76  			if (pos != -1)
77  			{
78  				dirNode = addDir(name.substring(0, pos), root);
79  				name = name.substring(pos + 1);
80  			}
81  			node = new NodeImpl(name, NodeInfo.Type.FILE);
82  			dirNode.add(node);
83  			scanner.scan(stream, node);
84  		}
85  
86  		MutableNodeInfo nodeInfo = (MutableNodeInfo)node.getNodeInfo();
87  		nodeInfo.setProperty(BasicFileAnalyser.KEY_LASTMODIFIED, new Date(ze.getTime()));
88  		if (!ze.isDirectory())
89  		{
90  			nodeInfo.setProperty(BasicFileAnalyser.KEY_FILESIZE, ze.getSize());
91  			nodeInfo.setProperty(CrcAnalyser.KEY_CRC, ze.getCrc());
92  			nodeInfo.setProperty(KEY_COMPRESSEDSIZE, ze.getCompressedSize());
93  		}
94  	}
95  
96  	private Node addDir(String dir, Node root)
97  	{
98  		String[] path = dir.split("/");
99  		Node dirNode = root;
100 		for (String pathElement : path)
101 		{
102 			Node newNode = dirNode.getChild(pathElement);
103 			if (newNode == null)
104 			{
105 				newNode = new NodeImpl(pathElement, NodeInfo.Type.DIRECTORY);
106 				dirNode.add(newNode);
107 			}
108 			dirNode = newNode;
109 		}
110 		return dirNode;
111 	}
112 }