1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 19 22 class FileSystemExporter { 23 27 public void createFolder(IPath destinationPath) { 28 new File(destinationPath.toOSString()).mkdir(); 29 } 30 33 public void write(IResource resource,IPath destinationPath) throws CoreException, IOException { 34 if (resource.getType() == IResource.FILE) 35 writeFile((IFile)resource,destinationPath); 36 else 37 writeChildren((IContainer)resource,destinationPath); 38 } 39 42 protected void writeChildren(IContainer folder, IPath destinationPath) throws CoreException, IOException { 43 if (folder.isAccessible()) { 44 IResource[] children = folder.members(); 45 for (int i = 0; i<children.length; i++) { 46 IResource child = children[i]; 47 writeResource( 48 child, 49 destinationPath.append(child.getName())); 50 } 51 } 52 } 53 57 protected void writeFile(IFile file, IPath destinationPath) throws IOException, CoreException { 58 FileOutputStream output = null; 59 InputStream contentStream = null; 60 61 try { 62 contentStream = file.getContents(false); 63 output = new FileOutputStream(destinationPath.toOSString()); 64 int chunkSize = contentStream.available(); 65 byte[] readBuffer = new byte[chunkSize]; 66 int n = contentStream.read(readBuffer); 67 68 while (n > 0) { 69 output.write(readBuffer); 70 n = contentStream.read(readBuffer); 71 } 72 } finally { 73 if (output != null) 74 output.close(); 75 if (contentStream != null) 76 contentStream.close(); 77 } 78 } 79 82 protected void writeResource(IResource resource,IPath destinationPath) throws CoreException, IOException { 83 if (resource.getType() == IResource.FILE) 84 writeFile((IFile)resource,destinationPath); 85 else { 86 createFolder(destinationPath); 87 writeChildren((IContainer)resource,destinationPath); 88 } 89 } 90 } 91 | Popular Tags |