1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 24 27 class FileSystemResourceExporter { 28 32 public void createFolder(IPath destinationPath) { 33 new File (destinationPath.toOSString()).mkdir(); 34 } 35 36 39 public void write(IResource resource, IPath destinationPath) 40 throws CoreException, IOException { 41 if (resource.getType() == IResource.FILE) 42 writeFile((IFile) resource, destinationPath); 43 else 44 writeChildren((IContainer) resource, destinationPath); 45 } 46 47 50 protected void writeChildren(IContainer folder, IPath destinationPath) 51 throws CoreException, IOException { 52 if (folder.isAccessible()) { 53 IResource[] children = folder.members(); 54 for (int i = 0; i < children.length; i++) { 55 IResource child = children[i]; 56 writeResource(child, destinationPath.append(child.getName())); 57 } 58 } 59 } 60 61 65 protected void writeFile(IFile file, IPath destinationPath) 66 throws IOException , CoreException { 67 FileOutputStream output = null; 68 InputStream contentStream = null; 69 70 try { 71 output = new FileOutputStream (destinationPath.toOSString()); 72 contentStream = file.getContents(false); 73 int chunkSize = contentStream.available(); 74 byte[] readBuffer = new byte[chunkSize]; 75 int n = contentStream.read(readBuffer); 76 77 while (n > 0) { 78 output.write(readBuffer); 79 n = contentStream.read(readBuffer); 80 } 81 } finally { 82 if (output != null) 83 output.close(); 84 if (contentStream != null) 85 contentStream.close(); 86 } 87 } 88 89 92 protected void writeResource(IResource resource, IPath destinationPath) 93 throws CoreException, IOException { 94 if (resource.getType() == IResource.FILE) 95 writeFile((IFile) resource, destinationPath); 96 else { 97 createFolder(destinationPath); 98 writeChildren((IContainer) resource, destinationPath); 99 } 100 } 101 } 102 | Popular Tags |