1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.net.URI ; 19 import java.util.zip.GZIPOutputStream ; 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 32 public class TarFileExporter implements IFileExporter { 33 private TarOutputStream outputStream; 34 private GZIPOutputStream gzipOutputStream; 35 36 37 44 public TarFileExporter(String filename, boolean compress) throws IOException { 45 if(compress) { 46 gzipOutputStream = new GZIPOutputStream (new FileOutputStream (filename)); 47 outputStream = new TarOutputStream(new BufferedOutputStream (gzipOutputStream)); 48 } else { 49 outputStream = new TarOutputStream(new BufferedOutputStream (new FileOutputStream (filename))); 50 } 51 } 52 53 59 public void finished() throws IOException { 60 outputStream.close(); 61 if(gzipOutputStream != null) { 62 gzipOutputStream.close(); 63 } 64 } 65 66 74 private void write(TarEntry entry, IFile contents) throws IOException , CoreException { 75 final URI location = contents.getLocationURI(); 76 if (location == null) { 77 throw new FileNotFoundException (contents.getFullPath().toOSString()); 78 } 79 80 InputStream 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 106 public void write(IFile resource, String destinationPath) 107 throws IOException , 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 |