List of usage examples for java.nio.file Path getParent
Path getParent();
From source file:duthientan.mmanm.com.CipherRSA.java
@Override public void encrypt(String filePath) { try {/*ww w . j av a 2s .com*/ Cipher ecipher = Cipher.getInstance("RSA"); ecipher.init(Cipher.ENCRYPT_MODE, publicKey); Path path = Paths.get(filePath); String encryptFilePath = path.getParent().toString() + "/" + "RSA" + "_" + path.getFileName().toString(); byte[] data = Files.readAllBytes(path); byte[] textEncrypted = null; int chunkSize = 245; if (data.length < 245) { textEncrypted = ecipher.doFinal(data); } else { for (int i = 0; i < data.length; i += chunkSize) { byte[] segment = Arrays.copyOfRange(data, i, i + chunkSize > data.length ? data.length : i + chunkSize); byte[] segmentEncrypted = ecipher.doFinal(segment); textEncrypted = ArrayUtils.addAll(textEncrypted, segmentEncrypted); } } FileOutputStream fos = new FileOutputStream(encryptFilePath); fos.write(textEncrypted); fos.close(); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidKeyException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalBlockSizeException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (BadPaddingException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.apache.marmotta.platform.ldp.services.LdpBinaryStoreServiceImpl.java
@Override public boolean store(String resource, InputStream stream) { try {/*w ww . j av a 2 s .com*/ Path file = getFile(resource); Files.createDirectories(file.getParent()); try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { IOUtils.copy(stream, outputStream); } return true; } catch (URISyntaxException | IOException e) { log.error("{} resource cannot be stored on disk: {}", resource, e.getMessage()); return false; } }
From source file:fi.jumi.launcher.daemon.DirBasedStewardTest.java
@Test public void creates_a_daemon_directory() { Path daemonDir = steward.createDaemonDir(jumiHome); assertThat("should be under $JUMI_HOME/daemons", daemonDir.getParent(), is(jumiHome.resolve("daemons"))); assertThat("should be a directory", Files.isDirectory(daemonDir), is(true)); }
From source file:org.apache.ranger.plugin.classloader.RangerPluginClassLoaderUtil.java
private String getPluginImplLibPath(String serviceType, Class<?> pluginClass) throws Exception { String ret = null;//from ww w.j a va 2 s. c o m if (LOG.isDebugEnabled()) { LOG.debug("==> RangerPluginClassLoaderUtil.getPluginImplLibPath for Class (" + pluginClass.getName() + ")"); } URI uri = pluginClass.getProtectionDomain().getCodeSource().getLocation().toURI(); Path path = Paths.get(URI.create(uri.toString())); ret = path.getParent().toString() + File.separatorChar + rangerPluginLibDir.replaceAll("%", serviceType); if (LOG.isDebugEnabled()) { LOG.debug("<== RangerPluginClassLoaderUtil.getPluginImplLibPath for Class (" + pluginClass.getName() + " PATH :" + ret + ")"); } return ret; }
From source file:net.daboross.bukkitdev.skywars.world.WorldUnzipper.java
public void doWorldUnzip(Logger logger) throws StartupFailedException { Validate.notNull(logger, "Logger cannot be null"); Path outputDir = Bukkit.getWorldContainer().toPath().resolve(Statics.BASE_WORLD_NAME); if (Files.exists(outputDir)) { return;//w ww . j ava2s. co m } try { Files.createDirectories(outputDir); } catch (IOException e) { throw new StartupFailedException("Couldn't create directory " + outputDir.toAbsolutePath() + "."); } InputStream fis = WorldUnzipper.class.getResourceAsStream(Statics.ZIP_FILE_PATH); if (fis == null) { throw new StartupFailedException("Couldn't get resource.\nError creating world. Please delete " + Statics.BASE_WORLD_NAME + " and restart server."); } try { try (ZipInputStream zis = new ZipInputStream(fis)) { ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); Path newFile = outputDir.resolve(fileName); Path parent = newFile.getParent(); if (parent != null) { Files.createDirectories(parent); } if (ze.isDirectory()) { logger.log(Level.FINER, "Making dir {0}", newFile); Files.createDirectories(newFile); } else if (Files.exists(newFile)) { logger.log(Level.FINER, "Already exists {0}", newFile); } else { logger.log(Level.FINER, "Copying {0}", newFile); try (FileOutputStream fos = new FileOutputStream(newFile.toFile())) { try { int next; while ((next = zis.read()) != -1) { fos.write(next); } fos.flush(); } catch (IOException ex) { logger.log(Level.WARNING, "Error copying file from zip", ex); throw new StartupFailedException("Error creating world. Please delete " + Statics.BASE_WORLD_NAME + " and restart server."); } fos.close(); } } try { ze = zis.getNextEntry(); } catch (IOException ex) { throw new StartupFailedException( "Error getting next zip entry\nError creating world. Please delete " + Statics.BASE_WORLD_NAME + " and restart server.", ex); } } } } catch (IOException | RuntimeException ex) { throw new StartupFailedException( "\nError unzipping world. Please delete " + Statics.BASE_WORLD_NAME + " and restart server.", ex); } }
From source file:org.sakuli.starter.helper.SahiProxyTest.java
@Test public void testInjectCustomJavaScriptFiles() throws Exception { Path configOrig = Paths.get(this.getClass().getResource("mock-files/inject_top.txt").toURI()); Path config = Paths.get(configOrig.getParent().toString() + File.separator + "inject_top_temp.txt"); Path target = Paths.get(configOrig.getParent().toString() + File.separator + "inject.js"); Path source = Paths.get(SahiProxy.class.getResource("inject_source.js").toURI()); FileUtils.copyFile(configOrig.toFile(), config.toFile()); when(props.getSahiJSInjectConfigFile()).thenReturn(config); when(props.getSahiJSInjectTargetFile()).thenReturn(target); when(props.getSahiJSInjectSourceFile()).thenReturn(source); assertFalse(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG)); testling.injectCustomJavaScriptFiles(); assertTrue(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG)); assertContains(FileUtils.readFileToString(target.toFile()), "Sahi.prototype.originalEx = Sahi.prototype.ex;"); assertContains(FileUtils.readFileToString(target.toFile()), "Sahi.prototype.ex = function (isStep) {"); }
From source file:com.ibm.vicos.client.ClientCommand.java
@CliCommand(value = "getblob", help = "Downloads a blob to destination path") public String getBlob( @CliOption(key = { "container" }, mandatory = true, help = "container name") final String container, @CliOption(key = { "blob" }, mandatory = true, help = "blob name") final String blob, @CliOption(key = { "dest_path" }, mandatory = true, help = "destination path") final String path) { Path fullPath = FileSystems.getDefault().getPath(path, blob); fullPath.getParent().toFile().mkdirs(); try {/*w w w. j a v a 2 s.c o m*/ InputStream inputStream = storage.getObject(container, blob); OutputStream outputStream = new FileOutputStream(fullPath.toFile()); long total = ByteStreams.copy(inputStream, outputStream); checkState(fullPath.toFile().exists(), "Could not create file"); return "Successfully downloaded " + fullPath.toString() + " [" + total + " bytes]"; } catch (Exception e) { e.printStackTrace(); return "Error while getting the " + container + "/" + blob; } }
From source file:org.hara.sodra.utils.SodraUtilsTest.java
@Before public void setUp() throws Exception { this.environmentVariables.set(SodraConstants.SODRA_DATA_DIR, "/tmp/sodra/data"); Path solrHome = SodraUtils.getSolrHome(); Files.createDirectories(solrHome); Path solrTemplateConfDir = Paths.get(solrHome.getParent().toString(), "index_template_config", "conf"); Files.createDirectories(solrTemplateConfDir); Files.createTempFile(solrTemplateConfDir, "prefix.", ".suffix"); }
From source file:org.abysm.onionzip.ZipFileExtractHelper.java
void extractZipArchiveEntries() throws IOException { ZipFile zipFile = new ZipFile(zipFilename, encoding); try {/*from www.j a v a2s . com*/ for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) { ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement(); System.out.println(entry.getName()); if (entry.isDirectory()) { Path directory = Paths.get(entry.getName()); Files.createDirectories(directory); } else if (entry.isUnixSymlink()) { Path symlink = Paths.get(entry.getName()); Path parentDirectory = symlink.getParent(); Path target = Paths.get(zipFile.getUnixSymlink(entry)); if (parentDirectory != null) { Files.createDirectories(parentDirectory); } Files.createSymbolicLink(symlink, target); } else { Path file = Paths.get(entry.getName()); Path parentDirectory = file.getParent(); if (parentDirectory != null) { Files.createDirectories(parentDirectory); } InputStream contentInputStream = zipFile.getInputStream(entry); FileOutputStream extractedFileOutputStream = new FileOutputStream(entry.getName()); try { IOUtils.copy(contentInputStream, extractedFileOutputStream); } finally { IOUtils.closeQuietly(contentInputStream); IOUtils.closeQuietly(extractedFileOutputStream); } FileTime fileTime = FileTime.fromMillis(entry.getLastModifiedDate().getTime()); Files.setLastModifiedTime(file, fileTime); } } } finally { ZipFile.closeQuietly(zipFile); } }
From source file:org.sakuli.starter.helper.SahiProxyTest.java
@Test public void testInjectCustomJavaScriptFilesOverridingNewer() throws Exception { Path configOrig = Paths.get(this.getClass().getResource("mock-files/inject_top.txt").toURI()); Path config = Paths.get(configOrig.getParent().toString() + File.separator + "inject_top_temp.txt"); Path targetOrig = Paths.get(this.getClass().getResource("mock-old-js/inject.js").toURI()); Path target = Paths.get(targetOrig.getParent().toString() + File.separator + "inject_temp.js"); FileUtils.copyFile(configOrig.toFile(), config.toFile()); FileUtils.copyFile(targetOrig.toFile(), target.toFile()); Files.setLastModifiedTime(target, FileTime.fromMillis(DateTime.now().minusYears(10).getMillis())); Path source = Paths.get(SahiProxy.class.getResource("inject_source.js").toURI()); when(props.getSahiJSInjectConfigFile()).thenReturn(config); when(props.getSahiJSInjectTargetFile()).thenReturn(target); when(props.getSahiJSInjectSourceFile()).thenReturn(source); assertFalse(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG)); assertContains(FileUtils.readFileToString(target.toFile()), "//old inject.js"); testling.injectCustomJavaScriptFiles(); assertTrue(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG)); assertContains(FileUtils.readFileToString(target.toFile()), "Sahi.prototype.originalEx = Sahi.prototype.ex;"); assertContains(FileUtils.readFileToString(target.toFile()), "Sahi.prototype.ex = function (isStep) {"); }