Example usage for java.io File getParent

List of usage examples for java.io File getParent

Introduction

In this page you can find the example usage for java.io File getParent.

Prototype

public String getParent() 

Source Link

Document

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Decompress.//from   w  ww . j ava  2s.  co m
 *
 * @param prefix
 *            the prefix
 * @param inputFile
 *            the input file
 * @param tempFile
 *            the temp file
 * @return the file
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static File decompress(final String prefix, final File inputFile, final File tempFile)
        throws IOException {
    final File tmpDestDir = createTodayPrefixedDirectory(prefix, new File(tempFile.getParent()));

    ZipFile zipFile = new ZipFile(inputFile);

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);

        if (entry.isDirectory()) {
            // Assume directories are stored parents first then
            // children.
            (new File(tmpDestDir, entry.getName())).mkdir();

            continue;
        }

        File newFile = new File(tmpDestDir, entry.getName());

        FileOutputStream fos = new FileOutputStream(newFile);
        try {
            byte[] buf = new byte[1024];
            int len;

            while ((len = stream.read(buf)) >= 0) {
                saveCompressedStream(buf, fos, len);
            }

        } catch (IOException e) {
            zipFile.close();

            IOException ioe = new IOException("Not valid ZIP archive file type.");
            ioe.initCause(e);
            throw ioe;
        } finally {
            fos.flush();
            fos.close();

            stream.close();
        }
    }
    zipFile.close();

    if ((tmpDestDir.listFiles().length == 1) && (tmpDestDir.listFiles()[0].isDirectory())) {
        return getShpFile(tmpDestDir.listFiles()[0]);
    }

    // File[] files = tmpDestDir.listFiles(new FilenameFilter() {
    //
    // public boolean accept(File dir, String name) {
    // return FilenameUtils.getExtension(name).equalsIgnoreCase("shp");
    // }
    // });
    //
    // return files.length > 0 ? files[0] : null;

    return getShpFile(tmpDestDir);
}

From source file:eu.optimis.ecoefficiencytool.core.tools.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info(//w ww. ja  va  2  s.c  om
            "ECO: File " + fileObject.getAbsolutePath() + " didn't exist. Creating one with default values...");

    //Create parent directories.
    log.info("ECO: Creating parent directories.");
    new File(fileObject.getParent()).mkdirs();

    //Create an empty file to copy the contents of the default file.
    log.info("ECO: Creating empty file.");
    new File(fileObject.getAbsolutePath()).createNewFile();

    //Copy file.
    log.info("ECO: Copying file " + fileObject.getName());
    InputStream streamIn = ConfigManager.class.getResourceAsStream("/" + fileObject.getName());
    FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath());
    byte[] buf = new byte[8192];
    while (true) {
        int length = streamIn.read(buf);
        if (length < 0) {
            break;
        }
        streamOut.write(buf, 0, length);
    }

    //Close streams after copying.
    try {
        streamIn.close();
    } catch (IOException ignore) {
        log.error("ECO: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("ECO: Couldn't close file output stream");
    }
}

From source file:eu.optimis.infrastructureproviderriskassessmenttool.core.configration.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info("IPRA: File " + fileObject.getAbsolutePath()
            + " didn't exist. Creating one with default values...");

    //Create parent directories.
    log.info("IPRA: Creating parent directories.");
    new File(fileObject.getParent()).mkdirs();

    //Create an empty file to copy the contents of the default file.
    log.info("IPRA: Creating empty file.");
    new File(fileObject.getAbsolutePath()).createNewFile();

    //Copy file./*w w  w.j a v  a2 s  .c  om*/
    log.info("IPRA: Copying file " + fileObject.getName());
    InputStream streamIn = ConfigManager.class.getResourceAsStream("/" + fileObject.getName());
    FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath());
    byte[] buf = new byte[8192];
    while (true) {
        int length = streamIn.read(buf);
        if (length < 0) {
            break;
        }
        streamOut.write(buf, 0, length);
    }

    //Close streams after copying.
    try {
        streamIn.close();
    } catch (IOException ignore) {
        log.error("IPRA: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("IPRA: Couldn't close file output stream");
    }
}

From source file:eu.optimis.serviceproviderriskassessmenttool.core.configration.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info("SPRA: File " + fileObject.getAbsolutePath()
            + " didn't exist. Creating one with default values...");

    //Create parent directories.
    log.info("SPRA: Creating parent directories.");
    new File(fileObject.getParent()).mkdirs();

    //Create an empty file to copy the contents of the default file.
    log.info("SPRA: Creating empty file.");
    new File(fileObject.getAbsolutePath()).createNewFile();

    //Copy file.// w w  w  .  j  a  v  a  2 s . c o m
    log.info("SPRA: Copying file " + fileObject.getName());
    InputStream streamIn = ConfigManager.class.getResourceAsStream("/" + fileObject.getName());
    FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath());
    byte[] buf = new byte[8192];
    while (true) {
        int length = streamIn.read(buf);
        if (length < 0) {
            break;
        }
        streamOut.write(buf, 0, length);
    }

    //Close streams after copying.
    try {
        streamIn.close();
    } catch (IOException ignore) {
        log.error("SPRA: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("SPRA: Couldn't close file output stream");
    }
}

From source file:com.athena.peacock.common.core.util.ZipUtil.java

/**
 * <pre>/*from  ww  w  .j  ava  2 s .  c o  m*/
 * ? ?? ? ?  .
 * </pre>
 * @param sourceFile
 * @param destDir
 * @return 
 * @throws IOException
 */
public static String decompress(String sourceFile, String destDir) throws IOException {
    Assert.notNull(sourceFile, "sourceFile cannot be null.");

    File src = new File(sourceFile);
    File dest = null;

    Assert.isTrue(src.exists(), src + " does not exist.");
    Assert.isTrue(src.isFile(), sourceFile + " is not a file.");

    if (StringUtils.isEmpty(destDir)) {
        dest = new File(src.getParent(), src.getName().substring(0, src.getName().lastIndexOf(".")));
    } else {
        dest = new File(destDir);
    }

    Unzip unzip = new Unzip();
    unzip.setSrc(src);
    unzip.setDest(dest);
    unzip.execute();

    return dest.getAbsolutePath();
}

From source file:org.ala.spatial.web.services.DownloadController.java

private static void fixGdmFiles(String pid, File dir) {
    try {//from  w  w  w  .  j  av a 2  s .  c  om
        File tmpdir = new File(dir.getParent() + "/temp/" + pid);
        //FileCopyUtils.copy(new FileInputStream(dir), new FileOutputStream(tmpdir));
        FileUtils.copyDirectory(dir, tmpdir);

        //File rsp = new File(tmpdir.getAbsolutePath() + "/removedSpecies.txt");
        //File rspnew = new File(tmpdir.getAbsolutePath() + "/prediction_removedSpecies.txt");

        //File msp = new File(tmpdir.getAbsolutePath() + "/maskedOutSensitiveSpecies.txt");
        //File mspnew = new File(tmpdir.getAbsolutePath() + "/prediction_maskedOutSensitiveSpecies.txt");

        //rsp.renameTo(rspnew);
        //msp.renameTo(mspnew);

        //(new File(tmpdir.getAbsolutePath() + "/species.zip")).delete();

        FilenameFilter ff = new SuffixFileFilter(".grd");
        File[] files = tmpdir.listFiles(ff);
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (!f.getName().startsWith("domain")) {
                f.delete();
            }
        }

    } catch (Exception e) {
        System.out.println("Unable to fix Prediction files");
        e.printStackTrace(System.out);
    }
}

From source file:io.Tools.java

/**
 * Create test PDB and Chemcomp folder. Also all PDB files in resources are copied there so all test can use this
 * folder/*from   w  w w.jav  a  2  s.  com*/
 *
 * @return
 */
public static String createPermanentTestFolder() {

    String d = System.getProperty("user.home");
    String builtTestFolder = d + File.separator + "Documents" + File.separator + testFolderName
            + File.separator;
    final File baseDir = new File(builtTestFolder);

    String builttestPDBFolder = builtTestFolder + File.separator + "pdb";
    baseDir.mkdirs();
    final File pdbDir = new File(builttestPDBFolder);
    if (Files.exists(Paths.get(builttestPDBFolder))) {
        try {
            FileUtils.deleteDirectory(pdbDir);
        } catch (IOException e) {
        }
    }
    pdbDir.mkdir();

    String builttestChemcompFolder = builtTestFolder + File.separator + "chemcomp";
    final File chemcompDir = new File(builttestChemcompFolder);
    if (Files.exists(Paths.get(builttestChemcompFolder))) {
        try {
            FileUtils.deleteDirectory(chemcompDir);
        } catch (IOException e) {
        }
    }

    chemcompDir.mkdirs();

    pdbDir.mkdir();
    testChemcompFolder = builtTestFolder;
    testPDBFolder = builttestPDBFolder;

    String resourcesPDBFolder = null;
    try {
        URL url = BiojavaReaderFromPDBFolderTest.class.getClassLoader().getResource("pdb/1di9.cif.gz");
        File pdb1di9file = new File(url.toURI());
        resourcesPDBFolder = pdb1di9file.getParent();
        Map<String, List<MMcifFileInfos>> indexPDBFileInFolder = IOTools
                .indexPDBFileInFolder(new File(resourcesPDBFolder).toString());
        for (Map.Entry<String, List<MMcifFileInfos>> entry : indexPDBFileInFolder.entrySet()) {
            try {
                FileUtils.copyFileToDirectory(new File(entry.getValue().get(0).getPathToFile().toString()),
                        pdbDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    String resourcesChemcompFolder = null;
    try {
        URL url = BiojavaReaderFromPDBFolderTest.class.getClassLoader().getResource("chemcomp/0DY.cif.gz");
        File chemcomp0DY = new File(url.toURI());
        resourcesChemcompFolder = chemcomp0DY.getParent();
        Map<String, List<Path>> indexPDBFileInFolder = IOTools
                .indexChemcompFileInFolder(new File(resourcesChemcompFolder).toString());
        for (Map.Entry<String, List<Path>> entry : indexPDBFileInFolder.entrySet()) {
            try {
                FileUtils.copyFileToDirectory(new File(entry.getValue().get(0).toString()),
                        new File(builttestChemcompFolder));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return testChemcompFolder;
}

From source file:com.athena.chameleon.common.utils.ZipUtil.java

/**
 * <pre>/*www  . j  a v a2s .c  o  m*/
 * ? ?? ? ?  .
 * </pre>
 * @param sourceFile
 * @param destDir
 * @return
 * @throws IOException
 */
public static boolean decompress(String sourceFile, String destDir) throws IOException {
    Assert.notNull(sourceFile, "sourceFile cannot be null.");

    File src = new File(sourceFile);
    File dest = null;

    Assert.isTrue(src.exists(), src + " does not exist.");
    Assert.isTrue(src.isFile(), sourceFile + " is not a file.");

    if (StringUtils.isEmpty(destDir)) {
        dest = new File(src.getParent(), src.getName().substring(0, src.getName().lastIndexOf(".")));
    } else {
        dest = new File(destDir);
    }

    Unzip unzip = new Unzip();
    unzip.setSrc(src);
    unzip.setDest(dest);
    unzip.execute();

    return true;
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Unpacks an existing JAR/Zip archive./* w  ww .j ava  2  s.co m*/
 * 
 * @param zipFile The Archive file to unzip.
 * @param outputFolder The destination folder where the unzipped content shall be saved.
 * @see <a href="http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/">
 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/</a>
 */
private static void unZipIt(File zipFile, File outputFolder) {

    byte[] buffer = new byte[1024];

    try {

        // create output directory is not exists
        File folder = outputFolder;
        if (folder.exists()) {
            FileUtils.deleteDirectory(folder);
        }
        folder.mkdir();

        // get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder, fileName);

            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            if (!ze.isDirectory()) {
                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
            }
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.ala.spatial.web.services.DownloadController.java

private static void fixMaxentFiles(String pid, File dir) {
    try {// w ww. jav  a 2s. c  o  m
        File tmpdir = new File(dir.getParent() + "/temp/" + pid);
        //FileCopyUtils.copy(new FileInputStream(dir), new FileOutputStream(tmpdir));
        FileUtils.copyDirectory(dir, tmpdir);

        //File grd = new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd");
        //File grdnew = new File(tmpdir.getAbsolutePath() + "/prediction.grd");

        //File gri = new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri");
        //File grinew = new File(tmpdir.getAbsolutePath() + "/prediction.gri");

        File rsp = new File(tmpdir.getAbsolutePath() + "/removedSpecies.txt");
        File rspnew = new File(tmpdir.getAbsolutePath() + "/prediction_removedSpecies.txt");

        File msp = new File(tmpdir.getAbsolutePath() + "/maskedOutSensitiveSpecies.txt");
        File mspnew = new File(tmpdir.getAbsolutePath() + "/prediction_maskedOutSensitiveSpecies.txt");

        //grd.renameTo(grdnew);
        //gri.renameTo(grinew);
        rsp.renameTo(rspnew);
        msp.renameTo(mspnew);

        (new File(tmpdir.getAbsolutePath() + "/species.zip")).delete();
        (new File(tmpdir.getAbsolutePath() + "/species.bat")).delete();
        (new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd")).delete();
        (new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri")).delete();

    } catch (Exception e) {
        System.out.println("Unable to fix Prediction files");
        e.printStackTrace(System.out);
    }
}