List of usage examples for java.io FileOutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this file output stream. From source file:com.bukanir.android.utils.Utils.java
public static String unzipSubtitle(String zip, String path) { InputStream is;/*ww w .j a v a 2 s . co m*/ ZipInputStream zis; try { String filename = null; is = new FileInputStream(zip); zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; byte[] buffer = new byte[1024]; int count; while ((ze = zis.getNextEntry()) != null) { filename = ze.getName(); if (ze.isDirectory()) { File fmd = new File(path + "/" + filename); fmd.mkdirs(); continue; } if (filename.endsWith(".srt") || filename.endsWith(".sub")) { FileOutputStream fout = new FileOutputStream(path + "/" + filename); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); break; } zis.closeEntry(); } zis.close(); File z = new File(zip); z.delete(); return path + "/" + filename; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:deodex.tools.ZipTools.java
/** * extract an odex file from .xz file/* w w w . j a v a 2 s . c om*/ * * @returns success only if an odex file was extracted * @param odex * the odex file to decompress * @throws IOException * well we are using IOs Exception might be thrown */ public static boolean extractOdex(File odex) throws IOException { File Decomdex; if (odex.getName().endsWith(S.ODEX_EXT)) { Logger.appendLog("[ZipTools][I]Decompressing " + odex.getName() + " not needed"); return true; } else if (odex.getName().endsWith(S.COMP_GZ_ODEX_EXT)) { Logger.appendLog("[ZipTools][I]Decompressing " + odex.getName() + " gzip detected ..."); return TarGzUtils.unGzipOdex(odex, odex.getParentFile()); } else { Logger.appendLog("[ZipTools][I]Decompressing " + odex.getName() + " xz compression detected ..."); Decomdex = new File(odex.getParentFile().getAbsolutePath() + "/" + StringUtils.getCropString(odex.getName(), odex.getName().length() - 3)); Logger.appendLog("[ZipTools][I]Decompressing " + odex.getAbsolutePath() + " to " + Decomdex.getAbsolutePath()); FileInputStream fin = new FileInputStream(odex); BufferedInputStream in = new BufferedInputStream(fin); FileOutputStream out = new FileOutputStream(Decomdex); XZCompressorInputStream xzIn = new XZCompressorInputStream(in); final byte[] buffer = new byte[32768]; int n = 0; while (-1 != (n = xzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); xzIn.close(); } Logger.appendLog("[ZipTools][I]Decompressing " + odex.getAbsolutePath() + " to " + Decomdex.getAbsolutePath() + " success ? " + Decomdex.exists()); return Decomdex.exists(); }
From source file:ie.deri.unlp.javaservices.topicextraction.topicextractor.gate.TopicExtractorGate.java
/** * /* w w w . j ava 2 s. c o m*/ * Extract a directory in a JAR on the classpath to an output folder. * * Note: User's responsibility to ensure that the files are actually in a JAR. * * @param classInJar A class in the JAR file which is on the classpath * @param resourceDirectory Path to resource directory in JAR * @param outputDirectory Directory to write to * @return String containing the path to the outputDirectory * @throws IOException */ private static String extractDirectoryFromClasspathJAR(Class<?> classInJar, String resourceDirectory, String outputDirectory) throws IOException { resourceDirectory = StringUtils.strip(resourceDirectory, "\\/") + File.separator; URL jar = classInJar.getProtectionDomain().getCodeSource().getLocation(); JarFile jarFile = new JarFile(new File(jar.getFile())); byte[] buf = new byte[1024]; Enumeration<JarEntry> jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); if (jarEntry.isDirectory() || !jarEntry.getName().startsWith(resourceDirectory)) { continue; } String outputFileName = FilenameUtils.concat(outputDirectory, jarEntry.getName()); //Create directories if they don't exist new File(FilenameUtils.getFullPath(outputFileName)).mkdirs(); //Write file FileOutputStream fileOutputStream = new FileOutputStream(outputFileName); int n; InputStream is = jarFile.getInputStream(jarEntry); while ((n = is.read(buf, 0, 1024)) > -1) { fileOutputStream.write(buf, 0, n); } is.close(); fileOutputStream.close(); } jarFile.close(); String fullPath = FilenameUtils.concat(outputDirectory, resourceDirectory); return fullPath; }
From source file:com.alexoree.jenkins.Main.java
private static String download(String localName, String remoteUrl) throws Exception { URL obj = new URL(remoteUrl); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setReadTimeout(5000);//from www . j a va2 s . c o m System.out.println("Request URL ... " + remoteUrl); boolean redirect = false; // normally, 3xx is redirect int status = conn.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true; } if (redirect) { // get redirect url from "location" header field String newUrl = conn.getHeaderField("Location"); // get the cookie if need, for login String cookies = conn.getHeaderField("Set-Cookie"); // open the new connnection again conn = (HttpURLConnection) new URL(newUrl).openConnection(); String version = newUrl.substring(newUrl.lastIndexOf("/", newUrl.lastIndexOf("/") - 1) + 1, newUrl.lastIndexOf("/")); String pluginname = localName.substring(localName.lastIndexOf("/") + 1); String ext = ""; if (pluginname.endsWith(".war")) ext = ".war"; else ext = ".hpi"; pluginname = pluginname.replace(ext, ""); localName = localName.replace(pluginname + ext, "/download/plugins/" + pluginname + "/" + version + "/"); new File(localName).mkdirs(); localName += pluginname + ext; System.out.println("Redirect to URL : " + newUrl); } if (new File(localName).exists()) { System.out.println(localName + " exists, skipping"); return localName; } byte[] buffer = new byte[2048]; FileOutputStream baos = new FileOutputStream(localName); InputStream inputStream = conn.getInputStream(); int totalBytes = 0; int read = inputStream.read(buffer); while (read > 0) { totalBytes += read; baos.write(buffer, 0, read); read = inputStream.read(buffer); } System.out.println("Retrieved " + totalBytes + "bytes"); return localName; }
From source file:de.flapdoodle.embedmongo.Files.java
private static void copyFile(File source, File destination) throws IOException { FileInputStream reader = null; FileOutputStream writer = null; try {//from ww w. j a v a 2 s .c om reader = new FileInputStream(source); writer = new FileOutputStream(destination); int read; byte[] buf = new byte[512]; while ((read = reader.read(buf)) != -1) { writer.write(buf, 0, read); } } finally { if (reader != null) reader.close(); if (writer != null) writer.close(); } }
From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java
/** * Un zip a file to a destination folder. * @param zipFile the zip file//from w ww . ja v a2 s . co m * @param outputFolder the output folder */ public static void unZipFile(final String zipFile, final String outputFolder) { LogUtil.setLogString("UnZip the File", true); final byte[] buffer = new byte[1024]; int len; try { // create output directory is not exists final File folder = new File(outputFolder); if (!folder.exists()) { folder.mkdir(); } // get the zip file content final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); final StringBuilder outFolderPath = new StringBuilder(); final StringBuilder fileLogPath = new StringBuilder(); ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { final String fileName = zipEntry.getName(); final File newFile = new File( outFolderPath.append(outputFolder).append(File.separator).append(fileName).toString()); LogUtil.setLogString( fileLogPath.append("file unzip : ").append(newFile.getAbsoluteFile()).toString(), true); // create all non exists folders // else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); final FileOutputStream fos = new FileOutputStream(newFile); while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); fileLogPath.setLength(0); outFolderPath.setLength(0); } zis.closeEntry(); zis.close(); LogUtil.setLogString("Done", true); } catch (IOException ex) { LOGGER.error("Error in unzip file", ex); } }
From source file:com.bahmanm.karun.Utils.java
/** * Extracts a gzip'ed tar archive.//from w ww .j av a 2s . c om * * @param archivePath Path to archive * @param destDir Destination directory * @throws IOException */ public synchronized static void extractTarGz(String archivePath, File destDir) throws IOException, ArchiveException { // copy File tarGzFile = File.createTempFile("karuntargz", "", destDir); copyFile(archivePath, tarGzFile.getAbsolutePath()); // decompress File tarFile = File.createTempFile("karuntar", "", destDir); FileInputStream fin = new FileInputStream(tarGzFile); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream(tarFile); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { fout.write(buffer, 0, n); } bin.close(); fin.close(); gzIn.close(); fout.close(); // extract final InputStream is = new FileInputStream(tarFile); ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) { OutputStream out; if (entry.isDirectory()) { File f = new File(destDir, entry.getName()); f.mkdirs(); continue; } else out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(ain, out); out.close(); } ain.close(); is.close(); }
From source file:Util.java
public static boolean retreiveBinaryFileFromJar(String resourceName, String targetDirectory, Object resource) throws Exception { boolean found = false; if (resourceName != null) { InputStream is = resource.getClass().getResourceAsStream(resourceName); if (is == null) throw new Exception("Resource " + resourceName + " was not found."); BufferedReader br = new BufferedReader(new InputStreamReader(is)); FileOutputStream fos = new FileOutputStream(targetDirectory + File.separator + resourceName.substring(resourceName.lastIndexOf('/'), resourceName.length())); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); }/*from w ww . ja va2s . c o m*/ fos.flush(); br.close(); is.close(); found = true; } else { found = false; } return found; }
From source file:eu.optimis.tf.ip.service.utils.PropertiesUtils.java
private static void createDefaultConfigFile(File fileObject) throws Exception { log.info("TRUST: File " + fileObject.getAbsolutePath() + " didn't exist. Creating one with default values..."); // Create parent directories. log.info("TRUST: Creating parent directories."); new File(fileObject.getParent()).mkdirs(); // Create an empty file to copy the contents of the default file. log.info("TRUST: Creating empty file."); new File(fileObject.getAbsolutePath()).createNewFile(); // Copy file. log.info("TRUST: Copying file " + fileObject.getName()); InputStream streamIn = PropertiesUtils.class.getResourceAsStream("/" + fileObject.getName()); FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath()); byte[] buf = new byte[8192]; while (true) { int length = streamIn.read(buf); if (length < 0) { break; }// w ww . j a v a2s . c o m streamOut.write(buf, 0, length); } // Close streams after copying. try { streamIn.close(); } catch (IOException ignore) { log.error("TRUST: Couldn't close input stream"); } try { streamOut.close(); } catch (IOException ignore) { log.error("TRUST: Couldn't close file output stream"); } }
From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java
private static void saveUrl(String filename, String urlString) throws IOException { InputStream in = null;/*from w ww . j a va2 s . c o m*/ FileOutputStream fout = null; try { in = new URL(urlString).openStream(); fout = new FileOutputStream(filename); byte data[] = new byte[1024]; int count; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } }