KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > TarFileExporter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.wizards.datatransfer;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.net.URI JavaDoc;
19 import java.util.zip.GZIPOutputStream JavaDoc;
20
21 import org.eclipse.core.filesystem.EFS;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.ResourceAttributes;
25 import org.eclipse.core.runtime.CoreException;
26
27 /**
28  * Exports resources to a .tar.gz file.
29  *
30  * @since 3.1
31  */

32 public class TarFileExporter implements IFileExporter {
33     private TarOutputStream outputStream;
34     private GZIPOutputStream JavaDoc gzipOutputStream;
35     
36
37     /**
38      * Create an instance of this class.
39      *
40      * @param filename java.lang.String
41      * @param compress boolean
42      * @exception java.io.IOException
43      */

44     public TarFileExporter(String JavaDoc filename, boolean compress) throws IOException JavaDoc {
45         if(compress) {
46             gzipOutputStream = new GZIPOutputStream JavaDoc(new FileOutputStream JavaDoc(filename));
47             outputStream = new TarOutputStream(new BufferedOutputStream JavaDoc(gzipOutputStream));
48         } else {
49             outputStream = new TarOutputStream(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(filename)));
50         }
51     }
52
53     /**
54      * Do all required cleanup now that we're finished with the
55      * currently-open .tar.gz
56      *
57      * @exception java.io.IOException
58      */

59     public void finished() throws IOException JavaDoc {
60         outputStream.close();
61         if(gzipOutputStream != null) {
62             gzipOutputStream.close();
63         }
64     }
65
66     /**
67      * Write the contents of the file to the tar archive.
68      *
69      * @param entry
70      * @param contents
71      * @exception java.io.IOException
72      * @exception org.eclipse.core.runtime.CoreException
73      */

74     private void write(TarEntry entry, IFile contents) throws IOException JavaDoc, CoreException {
75         final URI JavaDoc location = contents.getLocationURI();
76         if (location == null) {
77             throw new FileNotFoundException JavaDoc(contents.getFullPath().toOSString());
78         }
79         
80         InputStream JavaDoc contentStream = contents.getContents(false);
81         entry.setSize(EFS.getStore(location).fetchInfo().getLength());
82         outputStream.putNextEntry(entry);
83         try {
84             int n;
85             byte[] readBuffer = new byte[4096];
86             while ((n = contentStream.read(readBuffer)) > 0) {
87                 outputStream.write(readBuffer, 0, n);
88             }
89         } finally {
90             if (contentStream != null) {
91                 contentStream.close();
92             }
93         }
94
95         outputStream.closeEntry();
96     }
97
98     /**
99      * Write the passed resource to the current archive.
100      *
101      * @param resource org.eclipse.core.resources.IFile
102      * @param destinationPath java.lang.String
103      * @exception java.io.IOException
104      * @exception org.eclipse.core.runtime.CoreException
105      */

106     public void write(IFile resource, String JavaDoc destinationPath)
107             throws IOException JavaDoc, CoreException {
108
109         TarEntry newEntry = new TarEntry(destinationPath);
110         if(resource.getLocalTimeStamp() != IResource.NULL_STAMP) {
111             newEntry.setTime(resource.getLocalTimeStamp() / 1000);
112         }
113         ResourceAttributes attributes = resource.getResourceAttributes();
114         if (attributes != null && attributes.isExecutable()) {
115             newEntry.setMode(newEntry.getMode() | 0111);
116         }
117         if (attributes != null && attributes.isReadOnly()) {
118             newEntry.setMode(newEntry.getMode() & ~0222);
119         }
120         write(newEntry, resource);
121     }
122 }
123
Popular Tags