List of usage examples for java.io File setExecutable
public boolean setExecutable(boolean executable)
From source file:eu.scape_project.tool.toolwrapper.core.utils.Utils.java
/** * Method useful to write content that was previously merged to a certain * Velocity template into a file/*from w w w . ja va 2 s .c om*/ * * @param outputDirectory * directory where to write to * @param childDirectory * child directory if the information being written should be * placed in here * @param filename * name of the file being created * @param w * {@link StringWriter} that holds the information that is going * to be written * @param makeItExecutable * if the file being created should be executable * @return true if the file was successfully written, false otherwise * */ public static boolean writeTemplateContent(File outputDirectory, String childDirectory, String filename, StringWriter w, boolean makeItExecutable) { boolean res = true; File directory; if (childDirectory != null) { directory = new File(outputDirectory, childDirectory); } else { directory = outputDirectory; } File file = new File(directory, filename); FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(w.toString().getBytes(Charset.defaultCharset())); } catch (FileNotFoundException e) { log.error(e); res = false; } catch (IOException e) { log.error(e); res = false; } finally { if (fos != null) { try { fos.close(); file.setExecutable(makeItExecutable); } catch (IOException e) { log.error(e); res = false; } } } return res; }
From source file:jenkins.plugins.tanaguru.TanaguruRunnerBuilder.java
/** * Create a temporary file within a temporary folder, created in the * contextDir if not exists (first time) * @param contextDir/*from w ww .j a v a2 s . c o m*/ * @param fileName * @param fileContent * @return * @throws IOException */ public static File createTempFile(File contextDir, String fileName, String fileContent) throws IOException { File contextDirTemp = new File(contextDir.getAbsolutePath() + "/tmp"); if (!contextDirTemp.exists()) { if (contextDirTemp.mkdir()) { contextDirTemp.setExecutable(true); contextDirTemp.setWritable(true); } } File tempFile = new File(contextDirTemp.getAbsolutePath() + "/" + fileName); FileUtils.writeStringToFile(tempFile, fileContent); if (tempFile.exists()) { tempFile.setExecutable(true); tempFile.setWritable(true); } return tempFile; }
From source file:jenkins.plugins.asqatasun.AsqatasunRunnerBuilder.java
/** * Create a temporary file within a temporary folder, created in the * contextDir if not exists (first time) * @param contextDir//from w w w . j a v a2s.c o m * @param fileName * @param fileContent * @return * @throws IOException */ public static File createTempFile(File contextDir, String fileName, String fileContent) throws IOException { File contextDirTemp = new File(contextDir.getAbsolutePath() + "/tmp"); if (!contextDirTemp.exists() && contextDirTemp.mkdir()) { contextDirTemp.setExecutable(true); contextDirTemp.setWritable(true); } File tempFile = new File(contextDirTemp.getAbsolutePath() + "/" + fileName); FileUtils.writeStringToFile(tempFile, fileContent); if (tempFile.exists()) { tempFile.setExecutable(true); tempFile.setWritable(true); } return tempFile; }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp/*from w w w . ja va 2 s . co m*/ * @param * @throws FileNotFoundException * @throws IOException */ private static void unzip(File file, File dir, PropertySetter ps, JProgressBar bar) throws FileNotFoundException, IOException { final int BUFFER = 2048; BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; int value = 0; while ((entry = zis.getNextEntry()) != null) { bar.setValue(value++); int count; byte data[] = new byte[BUFFER]; // write the files to the disk String outputFile = dir.getAbsolutePath() + File.separator + entry.getName(); if (entry.isDirectory()) { new File(outputFile).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); File oFile = new File(outputFile); if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) { // FIXME: we need to make everything executable as somehow // the executable bit is not preserved in // OSX oFile.setExecutable(true); } if (ps.filterFilename(oFile)) { ps.setProperty(outputFile); } } } zis.close(); file.delete(); }
From source file:org.robovm.idea.RoboVmPlugin.java
private static void extractArchive(String archive, File dest) { archive = "/" + archive; TarArchiveInputStream in = null;//from www . j a v a 2 s .co m boolean isSnapshot = Version.getVersion().toLowerCase().contains("snapshot"); try { in = new TarArchiveInputStream(new GZIPInputStream(RoboVmPlugin.class.getResourceAsStream(archive))); ArchiveEntry entry = null; while ((entry = in.getNextEntry()) != null) { File f = new File(dest, entry.getName()); if (entry.isDirectory()) { f.mkdirs(); } else { if (!isSnapshot && f.exists()) { continue; } f.getParentFile().mkdirs(); OutputStream out = null; try { out = new FileOutputStream(f); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(out); } } } logInfo(null, "Installed RoboVM SDK %s to %s", Version.getVersion(), dest.getAbsolutePath()); // make all files in bin executable for (File file : new File(getSdkHome(), "bin").listFiles()) { file.setExecutable(true); } } catch (Throwable t) { logError(null, "Couldn't extract SDK to %s", dest.getAbsolutePath()); throw new RuntimeException("Couldn't extract SDK to " + dest.getAbsolutePath(), t); } finally { IOUtils.closeQuietly(in); } }
From source file:io.crate.testing.Utils.java
static void uncompressTarGZ(File tarFile, File dest) throws IOException { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { Path entryPath = Paths.get(tarEntry.getName()); if (entryPath.getNameCount() == 1) { tarEntry = tarIn.getNextTarEntry(); continue; }// w ww . java 2 s .c o m Path strippedPath = entryPath.subpath(1, entryPath.getNameCount()); File destPath = new File(dest, strippedPath.toString()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); if (destPath.getParent().equals(dest.getPath() + "/bin")) { destPath.setExecutable(true); } } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }
From source file:edu.stanford.epad.epadws.dcm4chee.Dcm4CheeOperations.java
public static boolean deleteStudy(String studyUID) { InputStream is = null;//from w w w . j a va 2 s.co m InputStreamReader isr = null; BufferedReader br = null; boolean success = false; try { log.info("Deleting study " + studyUID + " files - command: ./dcmdeleteStudy " + studyUID); String[] command = { "./dcmdeleteStudy", studyUID }; ProcessBuilder pb = new ProcessBuilder(command); String dicomScriptsDir = EPADConfig.getEPADWebServerDICOMScriptsDir() + "bin/"; File script = new File(dicomScriptsDir, "dcmdeleteStudy"); if (!script.exists()) dicomScriptsDir = EPADConfig.getEPADWebServerMyScriptsDir(); script = new File(dicomScriptsDir, "dcmdeleteStudy"); // Java 6 - Runtime.getRuntime().exec("chmod u+x "+script.getAbsolutePath()); script.setExecutable(true); pb.directory(new File(dicomScriptsDir)); Process process = pb.start(); process.getOutputStream(); is = process.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { log.info("./dcmdeleteStudy: " + line); sb.append(line).append("\n"); } try { int exitValue = process.waitFor(); log.info("DICOM delete study exit value is: " + exitValue); if (exitValue == 0) success = true; } catch (Exception e) { log.warning("Failed to delete DICOM study " + studyUID, e); } String cmdLineOutput = sb.toString(); if (cmdLineOutput.toLowerCase().contains("error")) { throw new IllegalStateException("Failed for: " + parseError(cmdLineOutput)); } } catch (Exception e) { log.warning("Failed to delete DICOM study " + studyUID, e); } return success; }
From source file:edu.uw.apl.nativelibloader.NativeLoader.java
static private File extractLibraryFile(String resourceName, File outDir) throws IOException { /*/*from w w w .j a v a 2s. c o m*/ Attach UUID to the native library file to essentially randomize its name. This ensures multiple class loaders can read it multiple times. */ String uuid = UUID.randomUUID().toString(); String extractedLibFileName = resourceName.substring(1).replaceAll("/", "."); // In Maven terms, the uuid acts like a 'classifier' extractedLibFileName += "-" + uuid; File extractedLibFile = new File(outDir, extractedLibFileName); log.debug("Extracting " + resourceName + " to " + extractedLibFile); InputStream is = NativeLoader.class.getResourceAsStream(resourceName); FileUtils.copyInputStreamToFile(is, extractedLibFile); is.close(); extractedLibFile.deleteOnExit(); // Set executable (x) flag to enable Java to load the native library extractedLibFile.setReadable(true); //extractedLibFile.setWritable(true, true); extractedLibFile.setExecutable(true); return extractedLibFile; }
From source file:edu.stanford.epad.epadws.dcm4chee.Dcm4CheeOperations.java
/** * TODO This does not work. The ./dcmdeleteSeries script invoked the dcm4chee twiddle command but it appears that the * moveSeriesToTrash operation it calls has no effect in this version of dcm4chee. See: * http://www.dcm4che.org/jira/browse/WEB-955 */// ww w . j a v a2 s. c o m public static boolean deleteSeries(String seriesUID, String seriesPk) { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; boolean success = false; try { log.info("Deleting series " + seriesUID + " seriesPK:" + seriesPk); String[] command = { "./dcmdeleteSeries", seriesPk, EPADConfig.xnatUploadProjectPassword }; ProcessBuilder processBuilder = new ProcessBuilder(command); String dicomScriptsDir = EPADConfig.getEPADWebServerDICOMScriptsDir() + "bin/"; File script = new File(dicomScriptsDir, "dcmdeleteSeries"); if (!script.exists()) dicomScriptsDir = EPADConfig.getEPADWebServerMyScriptsDir(); script = new File(dicomScriptsDir, "dcmdeleteSeries"); // Java 6 - Runtime.getRuntime().exec("chmod u+x "+script.getAbsolutePath()); script.setExecutable(true); processBuilder.directory(new File(dicomScriptsDir)); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); process.getOutputStream(); is = process.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line).append("\n"); log.info("./dcmdeleteSeries: " + line); } try { int exitValue = process.waitFor(); if (exitValue == 0) { log.info("Deleted DICOM series " + seriesUID + " pk:" + seriesPk); success = true; } else { log.warning("Failed to delete DICOM series " + seriesUID + " pk=" + seriesPk + "; exitValue=" + exitValue + "\n" + sb.toString()); } } catch (Exception e) { log.warning("Failed to delete DICOM series " + seriesUID, e); } String cmdLineOutput = sb.toString(); if (cmdLineOutput.toLowerCase().contains("error")) { throw new IllegalStateException("Failed for: " + parseError(cmdLineOutput)); } } catch (IOException e) { log.warning("Failed to delete DICOM series " + seriesUID, e); } finally { IOUtils.closeQuietly(br); IOUtils.closeQuietly(isr); IOUtils.closeQuietly(is); } return success; }
From source file:org.jboss.tools.openshift.reddeer.utils.FileHelper.java
public static void extractTarGz(File archive, File outputDirectory) { InputStream inputStream = null; try {/*from w w w. j a v a2 s . c o m*/ logger.info("Opening stream to gzip archive"); inputStream = new GzipCompressorInputStream(new FileInputStream(archive)); } catch (IOException ex) { throw new OpenShiftToolsException( "Exception occured while processing tar.gz file.\n" + ex.getMessage()); } logger.info("Opening stream to tar archive"); BufferedOutputStream outputStream = null; TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream); TarArchiveEntry currentEntry = null; try { while ((currentEntry = tarArchiveInputStream.getNextTarEntry()) != null) { if (currentEntry.isDirectory()) { logger.info("Creating directory: " + currentEntry.getName()); createDirectory(new File(outputDirectory, currentEntry.getName())); } else { File outputFile = new File(outputDirectory, currentEntry.getName()); if (!outputFile.getParentFile().exists()) { logger.info("Creating directory: " + outputFile.getParentFile()); createDirectory(outputFile.getParentFile()); } outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); logger.info("Extracting file: " + currentEntry.getName()); copy(tarArchiveInputStream, outputStream, (int) currentEntry.getSize()); outputStream.close(); outputFile.setExecutable(true); outputFile.setReadable(true); outputFile.setWritable(true); } } } catch (IOException e) { throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + e.getMessage()); } finally { try { tarArchiveInputStream.close(); } catch (Exception ex) { } try { outputStream.close(); } catch (Exception ex) { } } }