001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.builder.support;
019:
020: import java.io.BufferedInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.zip.ZipEntry;
029: import java.util.zip.ZipInputStream;
030:
031: import spoon.support.builder.CtFile;
032: import spoon.support.builder.CtFolder;
033: import spoon.support.builder.FileFactory;
034:
035: public class CtFolderZip implements CtFolder {
036:
037: File f;
038:
039: List<CtFile> files;
040:
041: public CtFolderZip(File f) throws IOException {
042: super ();
043: if (!f.isFile()) {
044: throw new IOException(f.getName()
045: + " is not a valid zip file");
046: }
047: this .f = f;
048: }
049:
050: public List<CtFile> getAllFiles() {
051: return getFiles();
052: }
053:
054: public List<CtFile> getAllJavaFiles() {
055: List<CtFile> files = new ArrayList<CtFile>();
056:
057: for (CtFile f : getFiles())
058: if (f.isJava())
059: files.add(f);
060:
061: // no subfolder, skipping
062: // for (CtFolder fol : getSubFolder())
063: // files.addAll(fol.getAllJavaFile());
064: return files;
065: }
066:
067: public List<CtFile> getFiles() {
068: // Indexing content
069: if (files == null) {
070: files = new ArrayList<CtFile>();
071: try {
072: ZipInputStream zipInput = new ZipInputStream(
073: new BufferedInputStream(new FileInputStream(f)));
074:
075: ZipEntry entry;
076: while ((entry = zipInput.getNextEntry()) != null) {
077: // deflate in buffer
078: final int BUFFER = 2048;
079: ByteArrayOutputStream output = new ByteArrayOutputStream(
080: BUFFER);
081: int count;
082: byte data[] = new byte[BUFFER];
083: while ((count = zipInput.read(data, 0, BUFFER)) != -1) {
084: output.write(data, 0, count);
085: }
086: output.flush();
087: output.close();
088:
089: files.add(new CtFileZip(this , entry.getName(),
090: output.toByteArray()));
091: }
092:
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097: return files;
098: }
099:
100: public String getName() {
101: return f.getName();
102: }
103:
104: public CtFolder getParent() {
105: try {
106: return FileFactory.createFolder(f.getParentFile());
107: } catch (FileNotFoundException e) {
108: e.printStackTrace();
109: }
110: return null;
111: }
112:
113: public List<CtFolder> getSubFolder() {
114: return new ArrayList<CtFolder>(0);
115: }
116:
117: public boolean isFile() {
118: return false;
119: }
120:
121: @Override
122: public String toString() {
123: return getName();
124: }
125:
126: public String getPath() {
127: return toString();
128: }
129:
130: }
|