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.asakusafw.testdriver.compiler.util.DeploymentUtil.java

/**
 * Creates a fat jar from the classpath entries.
 * @param entries the classpath entries//w ww  .j a v  a 2  s.co m
 * @param target the target fat jar path
 * @throws IOException if I/O error was occurred while building the target jar
 */
public static void buildFatJar(List<File> entries, File target) throws IOException {
    if (target.getParentFile().isDirectory() == false && target.getParentFile().mkdirs() == false) {
        throw new IOException(
                MessageFormat.format("Failed to copy into {0} (cannot create target directory)", target));
    }
    Set<String> saw = new HashSet<>();
    try (ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)))) {
        for (File path : entries) {
            if (path.isDirectory()) {
                putEntry(zip, path, null, saw);
            } else {
                mergeEntries(zip, path, saw);
            }
        }
    }
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Returns a plain file listing as a document.
 *
 * @param directory the directory to list.
 * @return a plain file listing as a document.
 */// w  ww.j a va2 s. c  o m
public static Document buildDirectoryListing(File directory) {
    Document dirList = DOMUtil.newDocument(false, false);
    Element root = dirList.createElement("dir");
    root.setAttribute("path", directory.toURI().toString());
    root.setAttribute("parentDir", directory.getParentFile().toURI().toString());

    File[] fileList = directory.listFiles();
    File file;
    Element element;
    for (int i = 0; i < fileList.length; i++) {
        file = fileList[i];

        if (file.isDirectory()) {
            element = dirList.createElement("dir");
        } else {
            element = dirList.createElement("file");
        }

        element.setAttribute("name", file.getName());
        element.setAttribute("path", file.toURI().toString());
        root.appendChild(element);
    }

    dirList.appendChild(root);
    return dirList;
}

From source file:com.diffplug.gradle.ZipMisc.java

/**
 * Unzips a directory to a folder.//www . ja  v a  2s. c  o  m
 *
 * @param input            a zip file
 * @param destinationDir   where the zip will be extracted to
 */
public static void unzip(File input, File destinationDir) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream(new FileInputStream(input)))) {
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            File dest = new File(destinationDir, entry.getName());
            if (entry.isDirectory()) {
                FileMisc.mkdirs(dest);
            } else {
                FileMisc.mkdirs(dest.getParentFile());
                try (OutputStream output = new BufferedOutputStream(new FileOutputStream(dest))) {
                    copy(zipInput, output);
                }
            }
        }
    }
}

From source file:com.seleniumtests.util.FileUtility.java

public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException {
    File firefoxProfile = new File(storeLocation);
    String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile();

    try (JarFile jar = new JarFile(location);) {
        logger.info("Extracting jar file::: " + location);
        firefoxProfile.mkdir();//from w  w w .  j a va2  s  . c  o  m

        Enumeration<?> jarFiles = jar.entries();
        while (jarFiles.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) jarFiles.nextElement();
            String currentEntry = entry.getName();
            File destinationFile = new File(storeLocation, currentEntry);
            File destinationParent = destinationFile.getParentFile();

            // create the parent directory structure if required
            destinationParent.mkdirs();
            if (!entry.isDirectory()) {
                BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry));
                int currentByte;

                // buffer for writing file
                byte[] data = new byte[BUFFER];

                // write the current file to disk
                try (FileOutputStream fos = new FileOutputStream(destinationFile);) {
                    BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);

                    // read and write till last byte
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        destination.write(data, 0, currentByte);
                    }

                    destination.flush();
                    destination.close();
                    is.close();
                }
            }
        }
    }

    FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF"));
    if (OSUtility.isWindows()) {
        new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete();
    } else {
        new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete();
    }
}

From source file:fr.paris.lutece.plugins.swaggerui.service.SwaggerFileService.java

/**
 * Returns the list of swagger files/*from  ww w .  ja  v a2s.  com*/
 * 
 * @param request The HTTP request
 * @return The list of swagger files
 */
public static List<SwaggerFile> getSwaggerFiles(HttpServletRequest request) {
    List<SwaggerFile> listSwaggerFiles = new ArrayList<>();
    List<File> listSwaggerDirectories = new ArrayList<>();
    String[] filesExtension = { EXT_JSON, EXT_YAML };
    File folderWebApp = new File(AppPathService.getWebAppPath() + SWAGGER_DIRECTORY_PATH);
    findDirectory(listSwaggerDirectories, folderWebApp);

    for (File swaggerDirectory : listSwaggerDirectories) {
        Collection<File> filesSwagger = FileUtils.listFiles(swaggerDirectory, filesExtension, true);
        for (File fileSwagger : filesSwagger) {
            String strPluginName = swaggerDirectory.getParentFile().getParentFile().getName();
            if (PluginService.isPluginEnable(strPluginName)) {
                SwaggerFile swaggerFile = new SwaggerFile();
                swaggerFile.setPluginName(strPluginName);
                swaggerFile.setVersion(fileSwagger.getParentFile().getName());

                String relativePath = new File(AppPathService.getWebAppPath()).toURI()
                        .relativize(fileSwagger.toURI()).getPath();
                swaggerFile.setPath(AppPathService.getBaseUrl(request) + SERVLET_PATH + relativePath);

                listSwaggerFiles.add(swaggerFile);
            }
        }
    }
    return listSwaggerFiles;
}

From source file:net.ontopia.utils.TestFileUtils.java

public static void transferTestInputDirectory(ResourcesDirectoryReader directoryReader, String path)
        throws IOException {
    for (URL resource : directoryReader.getResources()) {
        String relative = resource.getFile();
        relative = relative.substring(relative.lastIndexOf(path));
        File file = new File(new File(getTestdataOutputDirectory()), relative);
        file.getParentFile().mkdirs();
        try (InputStream in = resource.openStream(); OutputStream out = new FileOutputStream(file)) {
            IOUtils.copy(in, out);/*from w  w  w.j a v a 2s.c o m*/
        }
    }
}

From source file:com.thoughtworks.go.util.FileUtil.java

public static String subtractPath(File rootPath, File file) {
    String fullPath = FilenameUtils.separatorsToUnix(file.getParentFile().getPath());
    String basePath = FilenameUtils.separatorsToUnix(rootPath.getPath());
    return StringUtils.removeStart(StringUtils.removeStart(fullPath, basePath), "/");
}

From source file:com.validation.manager.core.tool.Tool.java

public static File createZipFile(List<File> files, String zipName) throws FileNotFoundException, IOException {
    if (!zipName.endsWith(".zip")) {
        zipName += ".zip";
    }/* w  w w  . j  av a 2s  .com*/
    File f = new File(zipName);
    if (f.getParentFile() != null) {
        f.getParentFile().mkdirs();
    }
    try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))) {
        files.forEach(file -> {
            try {
                ZipEntry ze = new ZipEntry(file.getName());
                out.putNextEntry(ze);
                byte[] data = FileUtils.readFileToByteArray(file);
                out.write(data, 0, data.length);
                out.closeEntry();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        });
    }
    return f;
}

From source file:com.ruesga.rview.misc.CacheHelper.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void writeFileCache(Context context, Account account, String name, byte[] data)
        throws IOException {
    File file = new File(getAccountCacheDir(context, account), name);
    file.getParentFile().mkdirs();
    FileUtils.writeByteArrayToFile(file, data);
}

From source file:arena.utils.FileUtils.java

public static void writeArrayToFile(byte data[], File outFile) throws IOException {
    OutputStream out = null;/*from w w  w.  ja  v  a 2s .  com*/
    try {
        outFile.getParentFile().mkdirs();
        out = new FileOutputStream(outFile);
        out.write(data);
        out.close();
    } finally {
        if (out != null) {
            out.close();
        }
    }
}