Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4a0e7f15d6a5f23f56e13d7602b65c028ebfb0d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (c) 2001, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jee.archive.internal;



import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jst.jee.archive.AbstractArchiveSaveAdapter;
import org.eclipse.jst.jee.archive.ArchiveSaveFailureException;
import org.eclipse.jst.jee.archive.IArchive;
import org.eclipse.jst.jee.archive.IArchiveResource;
import org.eclipse.jst.jee.archive.IArchiveSaveAdapter;



/**
 * This is a concrete implentation of IArchiveSaveAdapter. All the contents of the archive, including xmi
 * resources, will be output to a ZipOutputStream. The output stream should be passed in by the
 * client.
 */
public class ZipStreamArchiveSaveAdapterImpl extends AbstractArchiveSaveAdapter {
	protected OutputStream destinationStream;
	/** Used internally */
	protected ZipOutputStream zipOutputStream;

	/**
	 * Wraps a new zip output stream around the parameter
	 */
	public ZipStreamArchiveSaveAdapterImpl(OutputStream out) {
		setDestinationStream(out);
		setZipOutputStream(new ZipOutputStream(out));
	}

	@Override
	public void close() throws IOException {
		getDestinationStream().close();
	}

	protected IArchiveSaveAdapter createNestedSaveAdapter(IArchive anArchive) {
		return new ZipStreamArchiveSaveAdapterImpl(getZipOutputStream());
	}

	@Override
	public void finish() throws IOException {
		getZipOutputStream().finish();
		//If this is not nested, close the stream to free up the resource
		//otherwise, don't close it because the parent may not be done
		if (!(getDestinationStream() instanceof ZipOutputStream))
			getDestinationStream().close();
	}

	/**
	 * @return java.io.OutputStream
	 */
	public java.io.OutputStream getDestinationStream() {
		return destinationStream;
	}

	protected java.io.OutputStream getOutputStreamForResource(Resource aResource) throws java.io.IOException {
		return getZipOutputStream();
	}

	/**
	 * @return java.util.zip.ZipOutputStream
	 */
	protected java.util.zip.ZipOutputStream getZipOutputStream() {
		return zipOutputStream;
	}

	@Override
	protected void save(IArchiveResource resource) throws ArchiveSaveFailureException {
		try {
			IPath path = resource.getPath();
			if(resource.getType() == IArchiveResource.DIRECTORY_TYPE && !path.hasTrailingSeparator()){
				path = path.addTrailingSeparator();
			}
			
			ZipEntry entry = new ZipEntry(path.toString());
			if (resource.getLastModified() > 0)
				entry.setTime(resource.getLastModified());
			getZipOutputStream().putNextEntry(entry);
			if (resource.getType() != IArchiveResource.DIRECTORY_TYPE) {
				ArchiveUtil.copy(resource.getInputStream(), getZipOutputStream());
			}
			getZipOutputStream().closeEntry();
		} catch (IOException e) {
			throw new ArchiveSaveFailureException(e);
		}
	}

	/**
	 * @param newDestinationStream
	 *            java.io.OutputStream
	 */
	public void setDestinationStream(java.io.OutputStream newDestinationStream) {
		destinationStream = newDestinationStream;
	}

	/**
	 * @param newZipOutputStream
	 *            java.util.zip.ZipOutputStream
	 */
	protected void setZipOutputStream(java.util.zip.ZipOutputStream newZipOutputStream) {
		zipOutputStream = newZipOutputStream;
	}

}

Back to the top