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:eu.scape_project.arc2warc.utils.ArcUtils.java
/** * Read the ARC record content into a byte array. Note that the record * content can be only read once, it is "consumed" afterwards. * * @param arcRecord ARC record./*from w w w. java 2s. c o m*/ * @return Content byte array. * @throws IOException If content is too large to be stored in a byte array. */ public static byte[] arcRecordPayloadToByteArray(ARCRecord arcRecord) throws IOException { // Byte point where the content of the ARC record begins int contentBegin = (int) arcRecord.getMetaData().getContentBegin(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream buffis = new BufferedInputStream(arcRecord); BufferedOutputStream buffos = new BufferedOutputStream(baos); byte[] tempBuffer = new byte[BUFFER_SIZE]; int bytesRead; // skip header content buffis.skip(contentBegin); while ((bytesRead = buffis.read(tempBuffer)) != -1) { buffos.write(tempBuffer, 0, bytesRead); } buffis.close(); buffos.flush(); buffos.close(); return baos.toByteArray(); }
From source file:Main.java
public static void copyFile(File oldLocation, File newLocation) throws IOException { if (oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try {// w w w .java 2 s. com byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { throw new IOException( "IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { Log.e(TAG, "Error closing files when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } } } else { throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } }
From source file:Main.java
public static void copyFile(File in, File out) throws IOException { // avoids copying a file to itself if (in.equals(out)) { return;/*from w w w. ja v a 2 s . c o m*/ } // ensure the output file location exists out.getCanonicalFile().getParentFile().mkdirs(); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in)); BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out)); // a temporary buffer to read into byte[] tmpBuffer = new byte[8192]; int len = 0; while ((len = fis.read(tmpBuffer)) != -1) { // add the temp data to the output fos.write(tmpBuffer, 0, len); } // close the input stream fis.close(); // close the output stream fos.flush(); fos.close(); }
From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java
public static void unzip2folder(File zipFile, File toFolder) throws FileExistsException { if (!toFolder.exists()) { toFolder.mkdirs();/*w w w. j a v a 2 s .c om*/ } else if (toFolder.isFile()) { throw new FileExistsException(toFolder.getName()); } try { ZipEntry entry; @SuppressWarnings("resource") ZipFile zipfile = new ZipFile(zipFile); Enumeration<? extends ZipEntry> e = zipfile.entries(); while (e.hasMoreElements()) { entry = e.nextElement(); // String newDir; // if (entry.getName().indexOf("/") == -1) { // newDir = zipFile.getName().substring(0, zipFile.getName().indexOf(".")); // } else { // newDir = entry.getName().substring(0, entry.getName().indexOf("/")); // } // String folder = toFolder + (File.separatorChar+"") + newDir; // System.out.println(folder); // if ((new File(folder).mkdir())) { // System.out.println("Done."); // } System.out.println("Extracting: " + entry); if (entry.isDirectory()) { new File(toFolder + (File.separatorChar + "") + entry.getName()).mkdir(); } else { BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[] = new byte[2048]; String fileName = toFolder + (File.separatorChar + "") + entry.getName(); FileOutputStream fos = new FileOutputStream(fileName); BufferedOutputStream dest = new BufferedOutputStream(fos, 2048); while ((count = is.read(data, 0, 2048)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); is.close(); System.out.println("extracted to: " + fileName); } } } catch (ZipException e1) { zipFile.delete(); e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { } }
From source file:Main.java
public static void unzip(URL _url, URL _dest, boolean _remove_top) { int BUFFER_SZ = 2048; File file = new File(_url.getPath()); String dest = _dest.getPath(); new File(dest).mkdir(); try {// w w w . j a va 2 s . com ZipFile zip = new ZipFile(file); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); if (_remove_top) currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length()); File destFile = new File(dest, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is; is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER_SZ]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dst = new BufferedOutputStream(fos, BUFFER_SZ); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) { dst.write(data, 0, currentByte); } dst.flush(); dst.close(); is.close(); } /* if (currentEntry.endsWith(".zip")) { // found a zip file, try to open extractFolder(destFile.getAbsolutePath()); }*/ } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
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; }/*from w w w . j a v a 2 s . co 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:at.illecker.sentistorm.commons.util.io.IOUtils.java
public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) { try {//from www . jav a 2s . c om GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); // read Tar entries TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { if (logging) { LOG.info("Extracting: " + outDir + File.separator + entry.getName()); } if (entry.isDirectory()) { // create directory File f = new File(outDir + File.separator + entry.getName()); f.mkdirs(); } else { // decompress file int count; byte data[] = new byte[EXTRACT_BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE); while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } // close input stream tarIn.close(); } catch (IOException e) { LOG.error("IOException: " + e.getMessage()); } }
From source file:Main.java
public static boolean unzipFile(InputStream fis, String destDir) { final byte[] buffer = new byte[4096]; ZipInputStream zis = null;/* ww w. j a v a 2 s.c o m*/ Log.e("Unzip", "destDir = " + destDir); try { // make sure the directory is existent File dstFile = new File(destDir); if (!dstFile.exists()) { dstFile.mkdirs(); } else { int fileLenght = dstFile.listFiles().length; if (fileLenght >= 2) { return true; } } zis = new ZipInputStream(fis); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { new File(destDir, fileName).mkdirs(); } else { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, fileName))); int lenRead; while ((lenRead = zis.read(buffer)) != -1) { bos.write(buffer, 0, lenRead); } bos.close(); } zis.closeEntry(); } return true; } catch (IOException e) { e.printStackTrace(); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java
public static void unzip(String zipname) throws IOException { FileInputStream fis = new FileInputStream(zipname); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); //?? GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis)); // get directory of the zip file if (zipname.contains("\\")) zipname = zipname.replace("\\", "/"); // String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ; String zipDirectory = zipname.substring(0, zipname.lastIndexOf(".")); new File(zipDirectory).mkdir(); RunData.getInstance().setMetadataDirectory(zipDirectory); ZipEntry entry;//from w ww.j a v a2s . co m while ((entry = zis.getNextEntry()) != null) { System.out.println("Unzipping: " + entry.getName()); if (entry.getName().contains("metadata")) { RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName()); } else if (entry.getName().contains("schemes")) { RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName()); } else if (entry.getName().contains("access")) { RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName()); } int size; byte[] buffer = new byte[2048]; FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); } zis.close(); fis.close(); }
From source file:msec.org.TarUtil.java
private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int count;/* w w w . j ava2s . c o m*/ byte data[] = new byte[BUFFERSZ]; while ((count = tais.read(data, 0, BUFFERSZ)) != -1) { bos.write(data, 0, count); } bos.close(); }