Package de.innovationgate.eclipse.utils

Source Code of de.innovationgate.eclipse.utils.FileUtils

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;

import de.innovationgate.utils.WGUtils;

public class FileUtils {


 
  public static void copy(String fromFileName, String toFileName) throws IOException {
    File fromFile = new File(fromFileName);
    File toFile = new File(toFileName);
    copy(fromFile, toFile);
  }
 
 
  public static void copy(File fromFile, File toFile) throws IOException {
    FileInputStream from = null;
    FileOutputStream to = null;
    try {
      from = new FileInputStream(fromFile);
      to = new FileOutputStream(toFile);
      byte[] buffer = new byte[4096];
      int bytesRead;

      while ((bytesRead = from.read(buffer)) != -1)
        to.write(buffer, 0, bytesRead); // write
    } finally {
      if (from != null)
        try {
          from.close();
        } catch (IOException e) {
          ;
        }
      if (to != null)
        try {
          to.close();
        } catch (IOException e) {
        }
    }
  }
 
 
  public static IFolder createFolder(IContainer container, String name) throws CoreException {
    return createFolder(container, name, null);
  }
 
  public static IFolder createFolder(IContainer container, String name, IProgressMonitor monitor) throws CoreException {
    IFolder folder = container.getFolder(new Path(name));
    if (!folder.exists()) {
      folder.create(false, true, monitor);       
    }
    return folder;
  }
 
  public static void copyWithVariableSubstitution(File source, File target, Map<String,String> variables, String varPrefix, String varSuffix) throws IOException {
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
      reader = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target)));
      String line = reader.readLine();
      while (line != null) {
        line = substitute(line, variables, varPrefix, varSuffix);
        writer.write(line + "\n");
        line = reader.readLine();
      }
    } catch (IOException e) {
      throw e;
    } finally {
      if (writer != null) {
        try {
          writer.close();   
        } catch (IOException e) {         
        }
      }
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {         
        }
      }
    }
  }

  private static String substitute(String line, Map<String, String> variables, String varPrefix, String varSuffix) {
    if (variables != null) {
      Iterator<String> vars = variables.keySet().iterator();
      while (vars.hasNext()) {
        String variableName = vars.next();
        String toReplace = varPrefix + variableName + varSuffix;
        String replacement = variables.get(variableName);
        line = WGUtils.strReplace(line, toReplace, replacement, true);
      }
    }
    return line;
  }

 
  @SuppressWarnings("unchecked")
  public static final void unzip(File file, File targetDir) throws ZipException, IOException {
      Enumeration entries = null;
      ZipFile zipFile = null;
      try {
        zipFile = new ZipFile(file);
       
        entries = zipFile.entries();

        while(entries.hasMoreElements()) {
          ZipEntry entry = (ZipEntry)entries.nextElement();
          if(entry.isDirectory()) {
            (new File(targetDir, entry.getName())).mkdir();
            continue;
          }
          InputStream in = zipFile.getInputStream(entry);
          OutputStream out = new FileOutputStream(new File(targetDir, entry.getName()));   
          try {
            WGUtils.inToOut(in, out, 2048);
          } finally {
            out.close();
            in.close();
          }
        }
        zipFile.close();
    } finally {
        if (zipFile != null) {
          try {
            zipFile.close();
          } catch (IOException e) {           
          }
        }
      }
    }
 
  public static final IPath makeRelative(IPath source, IPath context){
    IPath result = new Path("");
    int mfs = source.matchingFirstSegments(context);
    source = source.removeFirstSegments(mfs);
    context = context.removeFirstSegments(mfs);
   
    for(int i=0;i < source.segmentCount();i++){
      result=result.append("../");
    }
    result=result.append(context);   
   
    return result;
  }
 
 
 
 
  public static final void unzip(InputStream zipIn, File targetDir) throws ZipException, IOException {
    File temp = File.createTempFile("UNZIP", null);
    temp.deleteOnExit();
   
    try {
      OutputStream out = new FileOutputStream(temp);
      try {
        WGUtils.inToOut(zipIn, out, 2048);
      } finally {
        out.close();
      }
      unzip(temp, targetDir);
    } finally {
      temp.delete();
    }
  }
 
//  public static final IPath makeRelative(IPath source, IPath base) {
//    int segments = source.matchingFirstSegments(base);
//    IPath relativePath = source.removeFirstSegments(segments);
//    return relativePath;
//  }

}
TOP

Related Classes of de.innovationgate.eclipse.utils.FileUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.