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:examples.utils.CifarReader.java
public static void downloadAndExtract() { if (new File("data", TEST_DATA_FILE).exists() == false) { try {/*ww w . j av a 2 s .com*/ if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) { URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE); FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE); fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE); fos.close(); } TarArchiveInputStream tar = new TarArchiveInputStream( new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE))); TarArchiveEntry entry = null; while ((entry = tar.getNextTarEntry()) != null) { if (entry.isDirectory()) { new File("data", entry.getName()).mkdirs(); } else { byte data[] = new byte[2048]; int count; BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File("data/", entry.getName())), 2048); while ((count = tar.read(data, 0, 2048)) != -1) { bos.write(data, 0, count); } bos.close(); } } tar.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void copyFile(File sourceFile, File targetFile) throws IOException { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try {//from ww w. ja va 2 s . c om inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); byte[] buffer = new byte[BUFFER]; int length; while ((length = inBuff.read(buffer)) != -1) { outBuff.write(buffer, 0, length); } outBuff.flush(); } finally { if (inBuff != null) { inBuff.close(); } if (outBuff != null) { outBuff.close(); } } }
From source file:com.worldline.easycukes.commons.helpers.FileHelper.java
/** * Extracts a specific file from a zip folder * * @param zip a {@link ZipInputStream} corresponding to the zip folder to be * extracted/*ww w . ja v a 2 s .c om*/ * @param filePath the path of the file to be extracted from the zip folder * @throws IOException if anything's going wrong while extracting the content of the * zip */ private static void extractFile(@NonNull ZipInputStream zip, @NonNull String filePath) throws IOException { @Cleanup final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); final byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = zip.read(bytesIn)) != -1) bos.write(bytesIn, 0, read); }
From source file:Main.java
/** * extracts a zip file to the given dir/*from ww w.j a v a2s . co m*/ * @param archive * @param destDir * @throws IOException * @throws ZipException * @throws Exception */ public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException { if (!destDir.exists()) { destDir.mkdir(); } ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); byte[] buffer = new byte[16384]; int len; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryFileName = entry.getName(); File dir = buildDirectoryHierarchyFor(entryFileName, destDir); if (!dir.exists()) { dir.mkdirs(); } if (!entry.isDirectory()) { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, entryFileName))); BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); while ((len = bis.read(buffer)) > 0) { bos.write(buffer, 0, len); } bos.flush(); bos.close(); bis.close(); } } }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry;/*from ww w. j a v a 2 s . c om*/ int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; } Path outFile = outDir.resolve(entryNameWithoutLeadingDir); if (entry.isDirectory()) { outFile.toFile().mkdirs(); continue; } int count; byte data[] = new byte[bufferSize]; BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()), bufferSize); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { fios.write(data, 0, count); } fios.close(); } tarInput.close(); gzipStream.close(); }
From source file:com.manning.androidhacks.hack037.MediaUtils.java
public static void saveRaw(Context context, int raw, String path) { File completePath = new File(Environment.getExternalStorageDirectory(), path); try {//from ww w .j av a2s .c om completePath.getParentFile().mkdirs(); completePath.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath))); BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw)); byte[] buff = new byte[32 * 1024]; int len; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } bos.flush(); bos.close(); } catch (IOException io) { Log.e(TAG, "Error: " + io); } }
From source file:Main.java
public static void downLoadFile(final String strUrl, final String fileURL, final int bufferLength) { BufferedInputStream in = null; BufferedOutputStream out = null; try {//from ww w .j av a 2s . c om in = new BufferedInputStream(new URL(strUrl).openStream()); File img = new File(fileURL); out = new BufferedOutputStream(new FileOutputStream(img)); byte[] buf = new byte[bufferLength]; int count = in.read(buf); while (count != -1) { out.write(buf, 0, count); count = in.read(buf); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static boolean copyFile(File fromFile, File toFile) { BufferedInputStream bis = null; BufferedOutputStream fos = null; try {// ww w. j av a2 s.co m FileInputStream is = new FileInputStream(fromFile); bis = new BufferedInputStream(is); fos = new BufferedOutputStream(new FileOutputStream(toFile)); byte[] buf = new byte[2048]; int i; while ((i = bis.read(buf)) != -1) { fos.write(buf, 0, i); } fos.flush(); return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (fos != null) { fos.close(); } } catch (IOException e1) { e1.printStackTrace(); } } return false; }
From source file:com.pieframework.runtime.utils.Zipper.java
public static void unzip(String zipFile, String toDir) throws ZipException, IOException { int BUFFER = 2048; File file = new File(zipFile); ZipFile zip = new ZipFile(file); String newPath = toDir;//from www . j a v a2s.co m File toDirectory = new File(newPath); if (!toDirectory.exists()) { toDirectory.mkdir(); } Enumeration zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, FilenameUtils.separatorsToSystem(currentEntry)); //System.out.println(currentEntry); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } zip.close(); }
From source file:Main.java
public static boolean copyFile(final File srcFile, final File saveFile) { File parentFile = saveFile.getParentFile(); if (!parentFile.exists()) { if (!parentFile.mkdirs()) return false; }/*from w w w. j a va 2s . c om*/ BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(srcFile)); outputStream = new BufferedOutputStream(new FileOutputStream(saveFile)); byte[] buffer = new byte[1024 * 4]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.flush(); } catch (IOException e) { e.printStackTrace(); return false; } finally { close(inputStream, outputStream); } return true; }