Example usage for org.apache.commons.io FileUtils copyFileToDirectory

List of usage examples for org.apache.commons.io FileUtils copyFileToDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyFileToDirectory.

Prototype

public static void copyFileToDirectory(File srcFile, File destDir) throws IOException 

Source Link

Document

Copies a file to a directory preserving the file date.

Usage

From source file:com.pieframework.runtime.utils.azure.Cspack.java

private void stageRoleBinaries(Role role, File roleDir) {
    File roleBin = null;//w  ww  .  jav a  2 s  .c  om
    if (role.getProps().get("type") != null) {
        roleBin = new File(roleDir.getPath() + File.separatorChar);
        roleBin.mkdir();

        if (roleBin != null) {
            for (String key : role.getChildren().keySet()) {

                if (role.getChildren().get(key) instanceof Service) {
                    Service s = (Service) role.getChildren().get(key);
                    if (s.getProps().get("type") != null
                            && s.getProps().get("type").equalsIgnoreCase("application")) {
                        String query = s.getProps().get("package");
                        if (query != null) {
                            String fQuery = s.getProps().get("package");
                            String nQuery = ResourceLoader.getResourceName(fQuery);
                            String pQuery = ResourceLoader.getResourcePath(fQuery);
                            Files bootstrap = (Files) s.getResources().get(nQuery);
                            if (bootstrap != null) {
                                List<File> flist = ResourceLoader.findPath(bootstrap.getLocalPath(), pQuery);
                                if (flist.size() == 1 && flist.get(0).exists()
                                        && FilenameUtils.isExtension(flist.get(0).getPath(), "zip")) {

                                    try {
                                        Zipper.unzip(flist.get(0).getPath(), roleBin.getPath());
                                        if (role.getProps().get("type").equals("WebRole")) {
                                            //Cleanup non-bin files
                                            for (File f : roleBin.listFiles()) {
                                                if (!f.getName().equalsIgnoreCase("bin")) {
                                                    FileUtils.forceDelete(f);
                                                }
                                            }

                                            //Move contents of bin dir into the current directory
                                            File binDir = new File(
                                                    roleBin.getPath() + File.separatorChar + "bin");
                                            if (binDir.exists()) {
                                                for (File f : binDir.listFiles()) {
                                                    if (f.isDirectory()) {
                                                        FileUtils.copyDirectory(f, roleBin);
                                                    } else {
                                                        FileUtils.copyFileToDirectory(f, roleBin);
                                                    }
                                                }
                                                FileUtils.forceDelete(binDir);
                                            }
                                        }
                                    } catch (ZipException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:fm.last.commons.io.LastFileUtilsTest.java

@Test(expected = NullPointerException.class)
public void testMoveFileToDirectorySafely_NullDir() throws IOException {
    // first copy file from data folder to temp folder so it can be moved safely
    File originalFile = dataFolder.getFile("3805bytes.log");
    FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot());
    File inputFile = new File(tempFolder.getRoot(), originalFile.getName());
    assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists());
    LastFileUtils.moveFileToDirectorySafely(inputFile, null, false); // null, create dir false
}

From source file:com.legstar.jaxb.AbstractJaxbGenTest.java

/**
 * Check a result against a reference./*from  ww w  . j ava  2 s.c o m*/
 * 
 * @param schemaName the schema name
 * @param fileName the file name to check
 * @param refFolder the reference folder
 * @param resultFolder the result folder
 * @throws Exception if something fails
 */
public void check(final String schemaName, final String fileName, final File refFolder, final File resultFolder)
        throws Exception {
    File resultFile = new File(resultFolder, fileName);

    if (isCreateReferences()) {
        FileUtils.copyFileToDirectory(resultFile, refFolder);
    } else {
        File referenceFile = new File(refFolder, fileName);
        assertEquals(referenceFile, resultFile);
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.project.page.ProjectExportPanel.java

/**
 * Copy, if exists, curation documents to a folder that will be exported as Zip file
 *
 * @param aProject//w w w  .j  av a 2s.  c  o m
 *            The {@link Project}
 * @param aCurationDocumentExist
 *            Check if Curation document exists
 * @param aCopyDir
 *            The folder where curated documents are copied to be exported as Zip File
 */
private void exportCuratedDocuments(ProjectExportModel aModel, File aCopyDir)
        throws FileNotFoundException, UIMAException, IOException, ClassNotFoundException {
    // Get all the source documents from the project
    List<de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument> documents = repository
            .listSourceDocuments(aModel.project);

    // Determine which format to use for export.
    Class<?> writer;
    if (FORMAT_AUTO.equals(aModel.format)) {
        writer = WebannoCustomTsvWriter.class;
    } else {
        writer = repository.getWritableFormats().get(repository.getWritableFormatId(aModel.format));
        if (writer == null) {
            writer = WebannoCustomTsvWriter.class;
        }
    }

    int initProgress = progress - 1;
    int i = 1;
    for (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument : documents) {
        File curationCasDir = new File(aCopyDir + CURATION_AS_SERIALISED_CAS + sourceDocument.getName());
        FileUtils.forceMkdir(curationCasDir);

        File curationDir = new File(aCopyDir + CURATION_FOLDER + sourceDocument.getName());
        FileUtils.forceMkdir(curationDir);

        // If the curation document is exist (either finished or in progress
        if (sourceDocument.getState().equals(SourceDocumentState.CURATION_FINISHED)
                || sourceDocument.getState().equals(SourceDocumentState.CURATION_IN_PROGRESS)) {
            File curationCasFile = repository.getCasFile(sourceDocument, CURATION_USER);
            if (curationCasFile.exists()) {
                // Copy CAS - this is used when importing the project again
                FileUtils.copyFileToDirectory(curationCasFile, curationCasDir);

                // Copy secondary export format for convenience - not used during import
                File curationFile = repository.exportAnnotationDocument(sourceDocument, CURATION_USER, writer,
                        CURATION_USER, Mode.CURATION);
                FileUtils.copyFileToDirectory(curationFile, curationDir);
                FileUtils.forceDelete(curationFile);
            }
        }

        progress = initProgress + (int) Math.ceil(((double) i) / documents.size() * 10.0);
        i++;
    }
}

From source file:com.blackducksoftware.tools.vuln_collector.VCProcessor.java

/**
 * Creates a directory using the project name Parses the name to escape
 * offensive characters.// w  w  w  .j  ava2  s .com
 * 
 * @param reportLocation
 * @param project
 * @return
 * @throws Exception
 */
private File prepareSubDirectory(File reportLocation, String project) throws Exception {
    project = formatProjectPath(project);
    File reportLocationSubDir = new File(reportLocation.toString() + File.separator + project);
    if (!reportLocationSubDir.exists()) {
        boolean dirsMade = reportLocationSubDir.mkdirs();
        if (!dirsMade) {
            throw new Exception("Unable to create report sub-directory for project: " + project);
        }
    }

    // Copy the web resources into this new location
    ClassLoader classLoader = getClass().getClassLoader();
    File webresources = new File(classLoader.getResource(WEB_RESOURCE).getFile());

    if (!webresources.exists()) {
        throw new Exception("Fatal exception, internal web resources are missing!");
    }

    File[] webSubDirs = webresources.listFiles();
    if (webSubDirs.length == 0) {
        throw new Exception(
                "Fatal exception, internal web resources sub directories are missing!  Corrupt archive.");
    }

    boolean readable = webresources.setReadable(true);
    if (!readable) {
        throw new Exception("Fatal. Cannot read internal web resource directory!");
    }

    try {
        for (File webSubDir : webSubDirs) {
            if (webSubDir.isDirectory()) {
                FileUtils.copyDirectoryToDirectory(webSubDir, reportLocationSubDir);
            } else {
                FileUtils.copyFileToDirectory(webSubDir, reportLocationSubDir);
            }
        }
    } catch (IOException ioe) {
        throw new Exception("Error during creation of report directory", ioe);
    }

    return reportLocationSubDir;
}

From source file:net.orzo.lib.Files.java

public void copyFile(String srcPath, String dstPath) throws IOException {
    File src = new File(srcPath);
    File dst = new File(dstPath);

    if (src.isFile() && dst.getParentFile().isDirectory() && !dst.exists()) {
        FileUtils.copyFile(src, dst);//from   ww w.j  a  va 2 s.com

    } else if (src.isFile() && dst.isDirectory()) {
        FileUtils.copyFileToDirectory(src, dst);

    } else {
        throw new IllegalArgumentException(
                "srcPath must be a file, dstPath must be either a (non-existing) file or a directory");
    }
}

From source file:es.bsc.servicess.ide.actions.DeployAction.java

private void deployOrchestrations(IJavaProject project, String serverLocation) throws IOException {
    IFolder outFolder = project.getProject().getFolder("output");
    IFile war = outFolder.getFile(project.getProject().getName() + ".war");
    File srcFile = war.getLocation().toFile();
    File ceDir = new File(serverLocation + "/webapps");
    if (ceDir.isDirectory()) {
        File f = new File(ceDir.getAbsolutePath() + "/" + project.getProject().getName() + ".war");
        File dir = new File(ceDir.getAbsolutePath() + "/" + project.getProject().getName());
        if (f.exists()) {
            f.delete();/*from  w  ww  . j a v a 2s  .com*/
        }
        if (dir.exists()) {
            dir.delete();
        }
        FileUtils.copyFileToDirectory(srcFile, ceDir);
    }

}

From source file:com.eviware.soapui.plugins.PluginManagerTest.java

private void copyResourceToDirectory(String resource, File fakeHomeDirectory) throws IOException {
    FileUtils.copyFileToDirectory(new File(PluginManagerTest.class.getResource(resource).getFile()),
            fakeHomeDirectory);//from   w w  w. j av a  2s . c  o m
}

From source file:fm.last.commons.io.LastFileUtilsTest.java

@Test(expected = NullPointerException.class)
public void testMoveFileToDirectorySafely_NullDir_CreateDir() throws IOException {
    // first copy file from data folder to temp folder so it can be moved safely
    File originalFile = dataFolder.getFile("3805bytes.log");
    FileUtils.copyFileToDirectory(originalFile, tempFolder.getRoot());
    File inputFile = new File(tempFolder.getRoot(), originalFile.getName());
    assertTrue(inputFile.getAbsolutePath() + " not found", inputFile.exists());
    LastFileUtils.moveFileToDirectorySafely(inputFile, null, true); // null, create dir true
}

From source file:com.universal.storage.UniversalFileStorage.java

/**
 * This method retrieves a file from the storage.
 * The method will retrieve the file according to the passed path.  
 * A file will be stored within the settings' tmp folder.
 * //  www  .  ja  va 2 s  .  co  m
 * @param path in context.
 * @returns a file pointing to the retrieved file.
 */
public File retrieveFile(String path) throws UniversalIOException {
    PathValidator.validatePath(path);

    validateRoot(this.settings);

    if ("".equals(path.trim())) {
        return null;
    }

    File file = new File(this.settings.getRoot() + path);

    if (!file.exists()) {
        UniversalIOException error = new UniversalIOException(file.getName() + " doesn't exist.");
        this.triggerOnErrorListeners(error);
        throw error;
    }

    if (file.isDirectory()) {
        UniversalIOException error = new UniversalIOException(file.getName() + " is a folder.");
        this.triggerOnErrorListeners(error);
        throw error;
    }

    File tmp = new File(this.settings.getTmp());
    try {
        FileUtils.copyFileToDirectory(file, tmp);
    } catch (Exception e) {
        UniversalIOException error = new UniversalIOException(e.getMessage());
        this.triggerOnErrorListeners(error);
        throw error;
    }

    return new UniversalFile(this.settings.getTmp() + file.getName());
}