Example usage for java.io File getParentFile

List of usage examples for java.io File getParentFile

Introduction

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

Prototype

public File getParentFile() 

Source Link

Document

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

Usage

From source file:com.android.build.gradle.internal.transforms.ExtractJarsTransform.java

private static void extractJar(@NonNull File outJarFolder, @NonNull File jarFile, boolean extractCode)
        throws IOException {
    mkdirs(outJarFolder);//from w  w w.  java2  s.c om
    HashSet<String> lowerCaseNames = new HashSet<>();
    boolean foundCaseInsensitiveIssue = false;

    try (Closer closer = Closer.create()) {
        FileInputStream fis = closer.register(new FileInputStream(jarFile));
        ZipInputStream zis = closer.register(new ZipInputStream(fis));
        // loop on the entries of the intermediary package and put them in the final package.
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            try {
                String name = entry.getName();

                // do not take directories
                if (entry.isDirectory()) {
                    continue;
                }

                foundCaseInsensitiveIssue = foundCaseInsensitiveIssue
                        || !lowerCaseNames.add(name.toLowerCase(Locale.US));

                Action action = getAction(name, extractCode);
                if (action == Action.COPY) {
                    File outputFile = new File(outJarFolder, name.replace('/', File.separatorChar));
                    mkdirs(outputFile.getParentFile());

                    try (Closer closer2 = Closer.create()) {
                        java.io.OutputStream outputStream = closer2
                                .register(new BufferedOutputStream(new FileOutputStream(outputFile)));
                        ByteStreams.copy(zis, outputStream);
                        outputStream.flush();
                    }
                }
            } finally {
                zis.closeEntry();
            }
        }

    }

    if (foundCaseInsensitiveIssue) {
        LOGGER.error(
                "Jar '{}' contains multiple entries which will map to "
                        + "the same file on case insensitive file systems.\n"
                        + "This can be caused by obfuscation with useMixedCaseClassNames.\n"
                        + "This build will be incorrect on case insensitive " + "file systems.",
                jarFile.getAbsolutePath());
    }
}

From source file:com.bellman.bible.service.common.FileManager.java

public static boolean copyFile(File fromFile, File toFile) {
    boolean ok = false;
    try {//from   ww  w . ja  v  a  2s.c o  m
        // don't worry if tofile exists, allow overwrite
        if (fromFile.exists()) {
            //ensure the target dir exists or FileNotFoundException is thrown creating dst FileChannel
            File toDir = toFile.getParentFile();
            toDir.mkdir();

            long fromFileSize = fromFile.length();
            log.debug("Source file length:" + fromFileSize);
            if (fromFileSize > CommonUtils.getFreeSpace(toDir.getPath())) {
                // not enough room on SDcard
                ok = false;
            } else {
                // move the file
                FileInputStream srcStream = new FileInputStream(fromFile);
                FileChannel src = srcStream.getChannel();
                FileOutputStream dstStream = new FileOutputStream(toFile);
                FileChannel dst = dstStream.getChannel();
                try {
                    dst.transferFrom(src, 0, src.size());
                    ok = true;
                } finally {
                    src.close();
                    dst.close();
                    srcStream.close();
                    dstStream.close();
                }
            }
        } else {
            // fromfile does not exist
            ok = false;
        }
    } catch (Exception e) {
        log.error("Error moving file to sd card", e);
    }
    return ok;
}

From source file:eionet.gdem.utils.ZipUtil.java

/**
 * Unzips files to output directory.//from w  w  w .  j  av  a  2  s. c  o  m
 * @param inZip Zip file
 * @param outDir Output directory
 * @throws IOException If an error occurs.
 */
public static void unzip(String inZip, String outDir) throws IOException {

    File sourceZipFile = new File(inZip);
    File unzipDestinationDirectory = new File(outDir);

    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();
        // System.out.println("Extracting: " + entry);

        File destFile = new File(unzipDestinationDirectory, currentEntry);

        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = null;
            BufferedOutputStream dest = null;
            try {
                is = new BufferedInputStream(zipFile.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte[] data = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                dest = new BufferedOutputStream(fos, BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
            } finally {
                IOUtils.closeQuietly(dest);
                IOUtils.closeQuietly(is);
            }
        }
    }
    zipFile.close();
}

From source file:gate.termraider.util.Utilities.java

public static File addExtensionIfNotExtended(File file, String extension) {
    String name = file.getName();
    if (name.contains(".")) {
        return file;
    }//from   w  ww . j  a va 2  s. c om

    // implied else: add extension
    File parentDir = file.getParentFile();
    if (extension.startsWith(".")) {
        name = name + extension;
    } else {
        name = name + "." + extension;
    }

    return new File(parentDir, name);
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

public static void extract(String zipPathFile, String destinationPath) throws Exception {
    FileInputStream fins = new FileInputStream(zipPathFile);
    ZipInputStream zipInputStream = new ZipInputStream(fins);
    try {/*from  www  .  jav  a  2  s.  c o m*/
        ZipEntry ze;
        byte ch[] = new byte[256];
        while ((ze = zipInputStream.getNextEntry()) != null) {
            File zipFile = new File(destinationPath + ze.getName());
            File zipFilePath = new File(zipFile.getParentFile().getPath());
            if (ze.isDirectory()) {
                if (!zipFile.exists()) {
                    if (!zipFile.mkdirs()) {
                        LOGGER.error("Create folder failed for '" + zipFile.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                }
                zipInputStream.closeEntry();
            } else {
                if (!zipFilePath.exists()) {
                    if (!zipFilePath.mkdirs()) {
                        LOGGER.error("Create folder failed for '" + zipFilePath.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                }
                FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
                try {
                    int i;
                    while ((i = zipInputStream.read(ch)) != -1) {
                        fileOutputStream.write(ch, 0, i);
                    }
                    zipInputStream.closeEntry();
                } finally {
                    fileOutputStream.close();
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(fins);
        IOUtils.closeQuietly(zipInputStream);
    }

}

From source file:UtilFunctions.java

public static int batchRename(File[] files, String prefix, String postfix, String replaceThis,
        String withThis) {//from  ww  w.  j a v  a2s.  c  o m
    try {
        int filesChanged = 0;

        for (File f : files) {

            //This bit should have better duplicate name support
            if (f.getName().contains(replaceThis)) {
                String name = f.getName();
                name = name.replace(replaceThis, withThis);

                f.renameTo(new File(f.getParentFile().getCanonicalPath() + "/" + name));

            }

            String name = prefix + f.getName() + postfix;

            f.renameTo(new File(f.getParentFile().getCanonicalPath() + "/" + name));

            filesChanged++;
        }

        return filesChanged;

    } catch (Exception e) {
        return -1;
    }

}

From source file:com.ejisto.util.IOUtils.java

public static void writeFile(InputStream inputStream, File baseDir, String filename) throws IOException {
    File out = new File(baseDir, filename);
    if (!out.getParentFile().exists() && !out.getParentFile().mkdirs()) {
        throw new IOException("Unable to write file " + out.getAbsolutePath());
    }//  w  w w . j ava  2  s.c o m
    FileOutputStream outputStream = new FileOutputStream(out);
    org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
    org.apache.commons.io.IOUtils.closeQuietly(outputStream);
}

From source file:cop.maven.plugins.RamlMojoIT.java

private static InvocationResult runMaven(File pom, String goal) throws MavenInvocationException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setPomFile(pom);//from   w  ww .ja  v a 2s.  co  m
    request.setGoals(Arrays.asList("clean", goal));

    Invoker invoker = new DefaultInvoker();
    invoker.setMavenHome(new File(getMavenHome()));
    invoker.setWorkingDirectory(pom.getParentFile());
    return invoker.execute(request);
}

From source file:eu.udig.omsbox.utils.OmsBoxUtils.java

/**
 * Checks if the given path is a GRASS raster file.
 * // w  ww  .  j  a  v a  2  s .com
 * <p>Note that there is no check on the existence of the file.
 * 
 * @param path the path to check.
 * @return true if the file is a grass raster.
 */
public static boolean isGrass(String path) {
    if (path == null)
        return false;
    File file = new File(path);
    File cellFolderFile = file.getParentFile();
    File mapsetFile = cellFolderFile.getParentFile();
    File windFile = new File(mapsetFile, "WIND");
    return cellFolderFile.getName().toLowerCase().equals("cell") && windFile.exists();
}