001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.internal.wizards.datatransfer;
011:
012: import java.io.BufferedOutputStream;
013: import java.io.FileNotFoundException;
014: import java.io.FileOutputStream;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.net.URI;
018: import java.util.zip.GZIPOutputStream;
019:
020: import org.eclipse.core.filesystem.EFS;
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.resources.ResourceAttributes;
024: import org.eclipse.core.runtime.CoreException;
025:
026: /**
027: * Exports resources to a .tar.gz file.
028: *
029: * @since 3.1
030: */
031: public class TarFileExporter implements IFileExporter {
032: private TarOutputStream outputStream;
033: private GZIPOutputStream gzipOutputStream;
034:
035: /**
036: * Create an instance of this class.
037: *
038: * @param filename java.lang.String
039: * @param compress boolean
040: * @exception java.io.IOException
041: */
042: public TarFileExporter(String filename, boolean compress)
043: throws IOException {
044: if (compress) {
045: gzipOutputStream = new GZIPOutputStream(
046: new FileOutputStream(filename));
047: outputStream = new TarOutputStream(
048: new BufferedOutputStream(gzipOutputStream));
049: } else {
050: outputStream = new TarOutputStream(
051: new BufferedOutputStream(new FileOutputStream(
052: filename)));
053: }
054: }
055:
056: /**
057: * Do all required cleanup now that we're finished with the
058: * currently-open .tar.gz
059: *
060: * @exception java.io.IOException
061: */
062: public void finished() throws IOException {
063: outputStream.close();
064: if (gzipOutputStream != null) {
065: gzipOutputStream.close();
066: }
067: }
068:
069: /**
070: * Write the contents of the file to the tar archive.
071: *
072: * @param entry
073: * @param contents
074: * @exception java.io.IOException
075: * @exception org.eclipse.core.runtime.CoreException
076: */
077: private void write(TarEntry entry, IFile contents)
078: throws IOException, CoreException {
079: final URI location = contents.getLocationURI();
080: if (location == null) {
081: throw new FileNotFoundException(contents.getFullPath()
082: .toOSString());
083: }
084:
085: InputStream contentStream = contents.getContents(false);
086: entry.setSize(EFS.getStore(location).fetchInfo().getLength());
087: outputStream.putNextEntry(entry);
088: try {
089: int n;
090: byte[] readBuffer = new byte[4096];
091: while ((n = contentStream.read(readBuffer)) > 0) {
092: outputStream.write(readBuffer, 0, n);
093: }
094: } finally {
095: if (contentStream != null) {
096: contentStream.close();
097: }
098: }
099:
100: outputStream.closeEntry();
101: }
102:
103: /**
104: * Write the passed resource to the current archive.
105: *
106: * @param resource org.eclipse.core.resources.IFile
107: * @param destinationPath java.lang.String
108: * @exception java.io.IOException
109: * @exception org.eclipse.core.runtime.CoreException
110: */
111: public void write(IFile resource, String destinationPath)
112: throws IOException, CoreException {
113:
114: TarEntry newEntry = new TarEntry(destinationPath);
115: if (resource.getLocalTimeStamp() != IResource.NULL_STAMP) {
116: newEntry.setTime(resource.getLocalTimeStamp() / 1000);
117: }
118: ResourceAttributes attributes = resource
119: .getResourceAttributes();
120: if (attributes != null && attributes.isExecutable()) {
121: newEntry.setMode(newEntry.getMode() | 0111);
122: }
123: if (attributes != null && attributes.isReadOnly()) {
124: newEntry.setMode(newEntry.getMode() & ~0222);
125: }
126: write(newEntry, resource);
127: }
128: }
|