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:Main.java

/**
 * Extract a zip resource into real files and directories
 * /*from   ww  w .ja v a2 s  .c o  m*/
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param directory target directory
 * @param overwrite indicates whether to overwrite existing files
 * @return list of files that were unpacked (if overwrite is false, this list won't include files
 *         that existed before)
 * @throws IOException
 */
public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite)
        throws IOException {
    final int BUFSIZE = 2048;
    byte buffer[] = new byte[BUFSIZE];
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE));
    List<File> files = new ArrayList<File>();
    ZipEntry entry;
    directory.mkdirs();
    while ((entry = zin.getNextEntry()) != null) {
        File file = new File(directory, entry.getName());
        files.add(file);
        if (overwrite || !file.exists()) {
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries.
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE);
                int nRead;
                while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) {
                    bos.write(buffer, 0, nRead);
                }
                bos.flush();
                bos.close();
            }
        }
    }
    zin.close();
    return files;
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*from   w ww  .  j a  va 2 s. co  m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:utils.TestUtils.java

public static File getJsDir() {
    File testClasses = codeSourceDir(TestUtils.class);
    File project = testClasses.getParentFile().getParentFile();
    return new File(project, "..");
}

From source file:com.enonic.esl.io.FileUtil.java

private static void inflateFile(File dir, InputStream is, ZipEntry entry) throws IOException {
    String entryName = entry.getName();
    File f = new File(dir, entryName);

    if (entryName.endsWith("/")) {
        f.mkdirs();/*from   w  w w .j a v a  2s  . c o m*/
    } else {
        File parentDir = f.getParentFile();
        if (parentDir != null && !parentDir.exists()) {
            parentDir.mkdirs();
        }
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(f);
            byte[] buffer = new byte[BUF_SIZE];
            for (int n = 0; (n = is.read(buffer)) > 0;) {
                os.write(buffer, 0, n);
            }
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

public static List<File> unTar(final File inputFile)
        throws FileNotFoundException, IOException, ArchiveException {
    return unTar(inputFile, inputFile.getParentFile());
}

From source file:com.intuit.karate.cucumber.FeatureWrapper.java

public static FeatureWrapper fromFile(File file, ClassLoader classLoader) {
    try {/*ww w. j a  va2  s  .com*/
        String text = FileUtils.readFileToString(file, "utf-8");
        return new FeatureWrapper(text, ScriptEnv.init(file.getParentFile(), classLoader));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.khepry.utilities.GenericUtilities.java

public static void generateOutFile(String outFilePath, String outputText, Handler handler) {
    Logger.getLogger("").addHandler(handler);
    File outFile = new File(outFilePath);
    if (outFile.getParentFile().exists()) {
        try {//from  ww w  .  j a v  a 2s. com
            BufferedWriter bw = new BufferedWriter(new FileWriter(outFilePath));
            bw.write(outputText);
            bw.close();
        } catch (IOException ex) {
            Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE,
                "Output path: " + outFile.getParent() + " does not exist!", new FileNotFoundException());
    }
}

From source file:eu.leads.processor.planner.ClassUtil.java

private static String createClassName(File root, File file) {
    StringBuffer sb = new StringBuffer();
    String fileName = file.getName();
    sb.append(fileName.substring(0, fileName.lastIndexOf(".class")));
    file = file.getParentFile();
    while (file != null && !file.equals(root)) {
        sb.insert(0, '.').insert(0, file.getName());
        file = file.getParentFile();/*from   ww w. j a v  a 2 s .c o m*/
    }
    return sb.toString();
}

From source file:Main.java

private static String getAaptResult(String sdkPath, String apkPath, final Pattern pattern) {
    try {/*from   www.  j  a v a 2s  . com*/
        final File apkFile = new File(apkPath);
        final ByteArrayOutputStream aaptOutput = new ByteArrayOutputStream();
        final String command = getAaptDumpBadgingCommand(sdkPath, apkFile.getName());

        Process process = Runtime.getRuntime().exec(command, null, apkFile.getParentFile());

        InputStream inputStream = process.getInputStream();
        for (int last = inputStream.read(); last != -1; last = inputStream.read()) {
            aaptOutput.write(last);
        }

        String packageId = "";
        final String aaptResult = aaptOutput.toString();
        if (aaptResult.length() > 0) {
            final Matcher matcher = pattern.matcher(aaptResult);
            if (matcher.find()) {
                packageId = matcher.group(1);
            }
        }
        return packageId;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.insightml.utils.io.IoUtils.java

public static void append(final File file, final String text) {
    try {//from   w  w w  .  ja  va  2 s.  c om
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        try (FileWriter fw = new FileWriter(file, true); BufferedWriter out = new BufferedWriter(fw)) {
            out.write(text);
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}