List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
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 .ja v a 2 s . c om*/ } 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(); } } }
From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java
public static void unzip2folder(File zipFile, File toFolder) throws FileExistsException { if (!toFolder.exists()) { toFolder.mkdirs();/*from w w w . j a v a 2 s .c o m*/ } 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: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 w w .j ava2 s. c om*/ 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:examples.utils.CifarReader.java
public static void downloadAndExtract() { if (new File("data", TEST_DATA_FILE).exists() == false) { try {// w w w. j a v a2 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:com.geewhiz.pacify.test.TestUtil.java
private static ByteArrayOutputStream readContent(ArchiveInputStream ais) throws IOException { byte[] content = new byte[2048]; ByteArrayOutputStream result = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(result); int len;//from w w w.ja v a 2 s . c o m while ((len = ais.read(content)) != -1) { bos.write(content, 0, len); } bos.close(); content = null; return result; }
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 {/*from www. j a v a 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:Main.java
public static void unzip(String strZipFile) { try {// ww w.j a va2 s . co m /* * 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.manning.androidhacks.hack037.MediaUtils.java
public static void saveRaw(Context context, int raw, String path) { File completePath = new File(Environment.getExternalStorageDirectory(), path); try {//w ww . j a v a2 s . c o m 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:com.petpet.c3po.gatherer.FileExtractor.java
/** * Unpack data from the stream to specified directory. * /*from w w w . j a v a 2 s .c o m*/ * @param in * stream with tar data * @param outputDir * destination directory * @return true in case of success, otherwise - false */ private static void extract(ArchiveInputStream in, File outputDir) { try { ArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { // replace : for windows OS. final File file = new File(outputDir, entry.getName().replaceAll(":", "_")); if (entry.isDirectory()) { if (!file.exists()) { file.mkdirs(); } } else { file.getParentFile().mkdirs(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE); try { IOUtils.copy(in, out); out.flush(); } finally { try { out.close(); } catch (IOException e) { LOG.debug("An error occurred while closing the output stream for file '{}'. Error: {}", file, e.getMessage()); } } } } } catch (IOException e) { LOG.debug("An error occurred while handling archive file. Error: {}", e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { LOG.debug("An error occurred while closing the archive stream . Error: {}", e.getMessage()); } } } }
From source file:Main.java
/** * extracts a zip file to the given dir/*from w w w .ja v a 2 s . c o 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(); } } }