List of usage examples for java.io File getParentFile
public File getParentFile()
null
if this pathname does not name a parent directory. From source file:ezbake.frack.submitter.util.JarUtil.java
public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException { JarOutputStream target = null; JarInputStream source;// w ww. ja v a 2s . c om File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath())); log.debug("Output file created at {}", outputJar.getAbsolutePath()); outputJar.createNewFile(); try { source = new JarInputStream(new FileInputStream(sourceJar)); target = new JarOutputStream(new FileOutputStream(outputJar)); ZipEntry entry = source.getNextEntry(); while (entry != null) { String name = entry.getName(); // Add ZIP entry to output stream. target.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; byte[] buffer = new byte[BUFFER_SIZE]; while ((len = source.read(buffer)) > 0) { target.write(buffer, 0, len); } entry = source.getNextEntry(); } source.close(); for (File fileToAdd : newFiles) { add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target); } } finally { if (target != null) { log.debug("Closing output stream"); target.close(); } } return outputJar; }
From source file:com.taobao.android.builder.tools.xml.XmlHelper.java
public static void saveDocument(Document document, File file) throws IOException { file.getParentFile().mkdirs(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); saveFile(document, format, file);/*from w ww. j a v a 2 s. com*/ }
From source file:functionalTests.dataspaces.GCMFunctionalDataSpacesBase.java
private static void createInputFileContent(final File file) throws IOException { final File parentFile = file.getParentFile(); if (!parentFile.exists()) { assertTrue(parentFile.mkdirs()); }//from w w w .jav a 2 s .co m final BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(INPUT_FILE_CONTENT); writer.close(); }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Moves a file "safely" - first copies it to the destination with ".part" appended to the filename, and then renames * it. This is useful for copying files to locations where files with certain extensions are processed. The rename * step should be a lot quicker than the copying step, preventing the file from being processed before it is fully * copied.// www . j ava2s . c o m * * @param srcFile Source file. * @param destFile Destination file. * @throws IOException If an error occurrs moving the file. */ public static void moveFileSafely(File srcFile, File destFile) throws IOException { File partFile = new File(destFile.getParentFile(), destFile.getName() + ".part"); FileUtils.moveFile(srcFile, partFile); if (!partFile.renameTo(destFile)) { throw new IOException( "Error renaming " + partFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); } }
From source file:Main.java
public static void unzip(String strZipFile) { try {/*from w w w . j a v a 2s. c o m*/ /* * STEP 1 : Create directory with the name of the zip file * * For e.g. if we are going to extract c:/demo.zip create c:/demo directory where we can extract all the zip entries */ File fSourceZip = new File(strZipFile); String zipPath = strZipFile.substring(0, strZipFile.length() - 4); File temp = new File(zipPath); temp.mkdir(); System.out.println(zipPath + " created"); /* * STEP 2 : Extract entries while creating required sub-directories */ ZipFile zipFile = new ZipFile(fSourceZip); Enumeration<? extends ZipEntry> e = zipFile.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); File destinationFilePath = new File(zipPath, entry.getName()); // create directories if required. destinationFilePath.getParentFile().mkdirs(); // if the entry is directory, leave it. Otherwise extract it. if (entry.isDirectory()) { continue; } else { // System.out.println("Extracting " + destinationFilePath); /* * Get the InputStream for current entry of the zip file using * * InputStream getInputStream(Entry entry) method. */ BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); int b; byte buffer[] = new byte[1024]; /* * read the current entry from the zip file, extract it and write the extracted file. */ FileOutputStream fos = new FileOutputStream(destinationFilePath); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); while ((b = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, b); } // flush the output stream and close it. bos.flush(); bos.close(); // close the input stream. bis.close(); } } zipFile.close(); } catch (IOException ioe) { System.out.println("IOError :" + ioe); } }
From source file:com.cip.crane.agent.utils.FileExtractUtils.java
public static void unZip(final File inputFile) throws FileNotFoundException, IOException { unZip(inputFile, inputFile.getParentFile()); }
From source file:org.codehaus.mojo.screenshot.ScreenshotMojo.java
/** * Computes the path for a file relative to a given base, or fails if the * only shared directory is the root and the absolute form is better. * /*from ww w.j a va 2 s. c om*/ * @param base * File that is the base for the result * @param name * File to be "relativized" * @return the relative name * @throws IOException * if files have no common sub-directories, i.e. at best share * the root prefix "/" or "C:\" */ public static String getRelativePath(File base, File name) throws IOException { File parent = base.getParentFile(); if (parent == null) { throw new IOException("No common directory"); } String bpath = base.getCanonicalPath(); String fpath = name.getCanonicalPath(); if (fpath.startsWith(bpath)) { return fpath.substring(bpath.length() + 1); } else { return (".." + File.separator + getRelativePath(parent, name)); } }
From source file:ch.admin.suis.msghandler.util.ZipUtils.java
/** * Decompress the given file to the specified directory. * * @param zipFile the ZIP file to decompress * @param toDir the directory where the files from the archive must be placed; the * file will be replaced if it already exists * @return a list of files that were extracted into the destination directory * @throws IllegalArgumentException if the provided file does not exist or the specified destination * is not a directory * @throws IOException if an IO error has occured (probably, a corrupted ZIP file?) *//* w w w. j a v a 2 s . c om*/ public static List<File> decompress(File zipFile, File toDir) throws IOException { Validate.isTrue(zipFile.exists(), "ZIP file does not exist", zipFile.getAbsolutePath()); Validate.isTrue(toDir.isDirectory(), toDir.getAbsolutePath() + " is not a directory"); final ArrayList<File> files = new ArrayList<>(); try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)))) { // read the entries ZipEntry entry; while (null != (entry = zis.getNextEntry())) { if (entry.isDirectory()) { LOG.error(MessageFormat.format( "cannot extract the entry {0} from the {1}. because it is a directory", entry.getName(), zipFile.getAbsolutePath())); continue; } // extract the file to the provided destination // we have to watch out for a unique name of the file to be extracted: // it can happen, that several at the same time incoming messages have a file with the same name File extracted = new File(FileUtils.getFilename(toDir, entry.getName())); if (!extracted.getParentFile().mkdirs()) { LOG.debug("cannot make all the necessary directories for the file " + extracted.getAbsolutePath() + " or " + "the path is already created "); } try (BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(extracted), BUFFER_SIZE)) { byte[] data = new byte[BUFFER_SIZE]; int count; while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } files.add(extracted); } } } return files; }
From source file:com.thinkit.operationsys.util.FileUtil.java
/** * @param topath /*from ww w.ja v a 2 s.c om*/ * @param frompath * @throws IOException */ public static void startCopy(String src, String dest) throws IOException { System.out.println("copy"); //?? File srcfile = new File(src); File destfile = new File(dest); if (srcfile.exists() && destfile.exists()) { FileUtils.copyFile(srcfile, destfile); } else { srcfile.getParentFile().mkdirs(); try { srcfile.createNewFile(); } catch (IOException e) { logger.info("create file error"); e.printStackTrace(); } destfile.getParentFile().mkdirs(); try { destfile.createNewFile(); } catch (IOException e) { logger.info("create file error"); e.printStackTrace(); } } }
From source file:net.rim.ejde.internal.legacy.Util.java
static public Workspace getDefaultLegacyWorkspace() { Workspace workspace = null;/*from w w w . j a v a 2 s . c o m*/ File file = ILegacy.Workspace.getMetaFile(); try { if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } workspace = new Workspace(file); save(workspace, true); } catch (Throwable t) { log.error(t.getMessage(), t); } return workspace; }