001: /*******************************************************************************
002: * Copyright (c) 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.navigator.util;
011:
012: import java.io.File;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.net.URL;
018: import java.util.Enumeration;
019: import java.util.zip.ZipEntry;
020: import java.util.zip.ZipFile;
021:
022: import org.eclipse.core.resources.IProject;
023: import org.eclipse.core.resources.IProjectDescription;
024: import org.eclipse.core.resources.IResource;
025: import org.eclipse.core.resources.IWorkspace;
026: import org.eclipse.core.resources.ResourcesPlugin;
027: import org.eclipse.core.runtime.CoreException;
028: import org.eclipse.core.runtime.IPath;
029: import org.eclipse.core.runtime.IProgressMonitor;
030: import org.eclipse.core.runtime.NullProgressMonitor;
031: import org.eclipse.core.runtime.Path;
032: import org.eclipse.core.runtime.Platform;
033: import org.eclipse.ui.tests.navigator.NavigatorTestsPlugin;
034:
035: public class ProjectUnzipUtil {
036:
037: private IPath zipLocation;
038: private String[] projectNames;
039: private IPath rootLocation = ResourcesPlugin.getWorkspace()
040: .getRoot().getLocation();
041: private static final String META_PROJECT_NAME = ".project"; //$NON-NLS-1$
042:
043: public ProjectUnzipUtil(IPath aLocalZipFilePath,
044: String[] aProjectNames) {
045: zipLocation = getLocalPath(aLocalZipFilePath);
046: projectNames = aProjectNames;
047:
048: }
049:
050: public IPath getLocalPath(IPath zipFilePath) {
051: URL url = NavigatorTestsPlugin.getDefault().find(zipFilePath);
052: try {
053: url = Platform.asLocalURL(url);
054: } catch (IOException e) {
055: e.printStackTrace();
056: }
057: return new Path(url.getPath());
058: }
059:
060: public boolean createProjects() {
061: try {
062: expandZip();
063: ResourcesPlugin.getWorkspace().getRoot().refreshLocal(
064: IResource.DEPTH_INFINITE, null);
065: buildProjects();
066: } catch (CoreException e) {
067: e.printStackTrace();
068: return false;
069: } catch (IOException e) {
070: e.printStackTrace();
071: return false;
072: }
073:
074: return true;
075: }
076:
077: public boolean reset() {
078: try {
079: expandZip();
080: ResourcesPlugin.getWorkspace().getRoot().refreshLocal(
081: IResource.DEPTH_INFINITE, null);
082: } catch (CoreException e) {
083: e.printStackTrace();
084: return false;
085: } catch (IOException e) {
086: e.printStackTrace();
087: return false;
088: }
089:
090: return true;
091: }
092:
093: private IProgressMonitor getProgessMonitor() {
094: return new NullProgressMonitor();
095: }
096:
097: private void expandZip() throws CoreException, IOException {
098: IProgressMonitor monitor = getProgessMonitor();
099: ZipFile zipFile = null;
100: try {
101: zipFile = new ZipFile(zipLocation.toFile());
102: } catch (IOException e1) {
103: throw e1;
104: }
105: Enumeration entries = zipFile.entries();
106: while (entries.hasMoreElements()) {
107: ZipEntry entry = (ZipEntry) entries.nextElement();
108: monitor.subTask(entry.getName());
109: File aFile = computeLocation(entry.getName()).toFile();
110: File parentFile = null;
111: try {
112: if (entry.isDirectory()) {
113: aFile.mkdirs();
114: } else {
115: parentFile = aFile.getParentFile();
116: if (!parentFile.exists())
117: parentFile.mkdirs();
118: if (!aFile.exists())
119: aFile.createNewFile();
120: copy(zipFile.getInputStream(entry),
121: new FileOutputStream(aFile));
122: if (entry.getTime() > 0)
123: aFile.setLastModified(entry.getTime());
124: }
125: } catch (IOException e) {
126: throw e;
127: }
128: monitor.worked(1);
129: }
130: }
131:
132: private IPath computeLocation(String name) {
133: return rootLocation.append(name);
134: }
135:
136: public static void copy(InputStream in, OutputStream out)
137: throws IOException {
138: byte[] buffer = new byte[1024];
139: try {
140: int n = in.read(buffer);
141: while (n > 0) {
142: out.write(buffer, 0, n);
143: n = in.read(buffer);
144: }
145: } finally {
146: in.close();
147: out.close();
148: }
149: }
150:
151: public void setRootLocation(IPath rootLocation) {
152: this .rootLocation = rootLocation;
153: }
154:
155: private void buildProjects() throws IOException, CoreException {
156: for (int i = 0; i < projectNames.length; i++) {
157:
158: IWorkspace workspace = ResourcesPlugin.getWorkspace();
159:
160: IPath projectPath = new Path(
161: "/" + projectNames[i] + "/" + META_PROJECT_NAME); //$NON-NLS-1$//$NON-NLS-2$
162: IPath path = rootLocation.append(projectPath);
163: IProject project = ResourcesPlugin.getWorkspace().getRoot()
164: .getProject(projectNames[i]);
165: IProjectDescription description = workspace
166: .loadProjectDescription(path);
167: project.create(description, (getProgessMonitor()));
168: project.open(getProgessMonitor());
169:
170: }
171: }
172:
173: }
|