List of usage examples for java.io BufferedOutputStream write
@Override public synchronized void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this buffered output stream. From source file:Main.java
/** * Extract a zip resource into real files and directories * /*from w ww. j a v a 2s.c om*/ * @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 saveISToFile(InputStream is, String fileName) throws IOException { File file = new File(fileName); file.getParentFile().mkdirs();//from w w w . ja v a 2 s. co m File tempFile = new File(fileName + ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedInputStream bis = new BufferedInputStream(is); byte[] barr = new byte[32768]; int read = 0; while ((read = bis.read(barr)) > 0) { bos.write(barr, 0, read); } bis.close(); bos.flush(); fos.flush(); bos.close(); fos.close(); file.delete(); tempFile.renameTo(file); }
From source file:io.hightide.TemplateFetcher.java
public static Path extractTemplate(Path tempFile, Path targetDir) throws IOException { try (FileInputStream fin = new FileInputStream(tempFile.toFile()); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)) { TarArchiveEntry entry;// ww w .j a v a 2 s . c o m Path rootDir = null; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { System.out.println("Extracting: " + entry.getName()); if (entry.isDirectory()) { if (isNull(rootDir)) { rootDir = targetDir.resolve(entry.getName()); } Files.createDirectory(targetDir.resolve(entry.getName())); } else { int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(targetDir.resolve(entry.getName()).toFile()); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.close(); } } return rootDir; } }
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
/** * Generate des key.//from w w w.j a v a 2 s .c om * * @param file the file * @throws java.io.IOException Signals that an I/O exception has occurred. */ public static void generateDESKey(String file) throws IOException { DESedeKeyGenerator kg = new DESedeKeyGenerator(); KeyGenerationParameters kgp = new KeyGenerationParameters(new SecureRandom(), DESedeParameters.DES_EDE_KEY_LENGTH * 8); kg.init(kgp); byte[] key = kg.generateKey(); BufferedOutputStream keystream = new BufferedOutputStream(new FileOutputStream(file)); byte[] keyhex = Hex.encode(key); keystream.write(keyhex, 0, keyhex.length); keystream.flush(); keystream.close(); }
From source file:Main.java
public static void unzip(String strZipFile) { try {/*from w w w .j av a 2s. com*/ /* * 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.ccoe.build.utils.CompressUtils.java
/** * Uncompress a zip to files/*w w w. ja v a 2s .c o m*/ * @param zip * @param unzipdir * @param isNeedClean * @return * @throws FileNotFoundException * @throws IOException */ public static List<File> unCompress(File zip, String unzipdir) throws IOException { ArrayList<File> unzipfiles = new ArrayList<File>(); FileInputStream fi = new FileInputStream(zip); ZipInputStream zi = new ZipInputStream(new BufferedInputStream(fi)); try { ZipEntry entry; while ((entry = zi.getNextEntry()) != null) { System.out.println("Extracting: " + entry); int count; byte data[] = new byte[BUFFER]; File unzipfile = new File(unzipdir + File.separator + entry.getName()); FileOutputStream fos = new FileOutputStream(unzipfile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((count = zi.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); unzipfiles.add(unzipfile); } } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(zi); } return unzipfiles; }
From source file:com.microsoft.azure.hdinsight.util.HDInsightJobViewUtils.java
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); }/*from ww w. j a v a2s . co m*/ bos.close(); }
From source file:io.vertx.config.vault.utils.VaultDownloader.java
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); }/* w w w . jav a2 s . c om*/ bos.close(); }
From source file:Main.java
/** * Move the file in oldLocation to newLocation. *//*from w w w.j a v a2 s . c o m*/ public static void moveFile(String tempLocation, String newLocation) throws IOException { File oldLocation = new File(tempLocation); if (oldLocation != null && oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try { byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { } } } else { } }
From source file:Main.java
private static void fileUnZip(ZipInputStream zis, File file) throws FileNotFoundException, IOException { java.util.zip.ZipEntry zip = null; while ((zip = zis.getNextEntry()) != null) { String name = zip.getName(); File f = new File(file.getAbsolutePath() + File.separator + name); if (zip.isDirectory()) { f.mkdirs();/*ww w . j a va2 s . co m*/ } else { f.getParentFile().mkdirs(); f.createNewFile(); BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(f)); byte b[] = new byte[2048]; int aa = 0; while ((aa = zis.read(b)) != -1) { bos.write(b, 0, aa); } bos.flush(); } finally { bos.close(); } bos.close(); } } }