List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream read
public int read(byte b[]) throws IOException
b
. From source file:org.jboss.tools.runtime.core.extract.internal.UntarUtility.java
public IStatus extract(File dest, IOverwrite overwriteQuery, IProgressMonitor monitor) throws CoreException { String possibleRoot = null;//from w w w.jav a2s . c o m try { dest.mkdir(); TarArchiveInputStream tarIn = getTarArchiveInputStream(file); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); while (tarEntry != null) { String name = tarEntry.getName(); File destPath = new File(dest, name); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) { int length = 0; while ((length = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, length); } } } // Lets check for a possible root, to avoid scanning the archive again later possibleRoot = checkForPossibleRootEntry(possibleRoot, name); tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); } catch (IOException ioe) { throw new CoreException(new Status(IStatus.ERROR, RuntimeCoreActivator.PLUGIN_ID, 0, NLS.bind("Error extracting runtime {0}", ioe.getLocalizedMessage()), ioe)); //$NON-NLS-1$ } this.discoveredRoot = possibleRoot; return Status.OK_STATUS; }
From source file:org.jwifisd.eyefi.EyeFiServer.java
/** * eyefi sends the files tarred w will have to untar it first. * /*w w w . j a v a2s . c o m*/ * @param tarFile * the tar file * @param out * output stream to write the untarred contents. * @return true if successful * @throws Exception * if the file could not be untarred. */ private boolean extractTarFile(File tarFile, OutputStream out) throws Exception { boolean result = false; byte[] tarBytes = IOUtils.toByteArray(new FileInputStream(tarFile)); // This is stangege but posix was not correctly detected by the apache // lib, so we change some bytes so the detection will work. System.arraycopy(TarConstants.MAGIC_POSIX.getBytes("US-ASCII"), 0, tarBytes, TarConstants.MAGIC_OFFSET, TarConstants.MAGICLEN); TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(tarBytes)); final byte[] buffer = new byte[BUFFER_SIZE]; try { ArchiveEntry tarEntry = tar.getNextEntry(); if (tarEntry != null) { int n = 0; while ((n = tar.read(buffer)) >= 0) { out.write(buffer, 0, n); } result = true; } } finally { tar.close(); out.close(); } return result; }
From source file:org.lobid.lodmill.TarReader.java
@Override public void process(final Reader reader) { TarArchiveInputStream tarInputStream = null; try {//from www . j a v a 2 s. co m tarInputStream = new TarArchiveInputStream(new ReaderInputStream(reader)); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { byte[] buffer = new byte[(int) entry.getSize()]; while ((tarInputStream.read(buffer)) > 0) { getReceiver().process(new StringReader(new String(buffer))); } } } tarInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(tarInputStream); } }
From source file:org.opentestsystem.delivery.testreg.transformer.TarUnbundler.java
public byte[][] unbundle(File bundledFile) throws IOException { TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new FileInputStream(bundledFile)); List<byte[]> byteArrays = new ArrayList<byte[]>(); ByteArrayOutputStream bytesOut = null; TarArchiveEntry entry = null;//from www . ja v a 2s.c o m while ((entry = tarInputStream.getNextTarEntry()) != null) { byte[] buffer = new byte[4096]; int len = 0; bytesOut = new ByteArrayOutputStream(); while ((len = tarInputStream.read(buffer)) > 0) { bytesOut.write(buffer, 0, len); } byteArrays.add(entry.getName().getBytes()); byteArrays.add(bytesOut.toByteArray()); bytesOut.close(); } tarInputStream.close(); return byteArrays.toArray(new byte[0][]); }
From source file:org.robovm.compilerhelper.Archiver.java
public static void unarchive(File archiveFile, File destinationDirectory) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)))); destinationDirectory.mkdirs();/* w w w .ja v a2 s . c o m*/ TarArchiveEntry entry = tar.getNextTarEntry(); while (entry != null) { File f = new File(destinationDirectory, entry.getName()); if (entry.isDirectory()) { f.mkdirs(); entry = tar.getNextTarEntry(); continue; } // TODO make this a bit cleaner String parentDir = f.getPath(); if (parentDir != null) { new File(parentDir.substring(0, parentDir.lastIndexOf(File.separator))).mkdirs(); } f.createNewFile(); byte[] bytes = new byte[1024]; int count; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); while ((count = tar.read(bytes)) > 0) { out.write(bytes, 0, count); } out.flush(); out.close(); entry = tar.getNextTarEntry(); } }
From source file:org.zaproxy.libs.DownloadTools.java
public static void downloadDriver(String urlStr, String destDir, String destFile) { File dest = new File(destDir + destFile); if (dest.exists()) { System.out.println("Already exists: " + dest.getAbsolutePath()); return;//from w w w . jav a 2s. c o m } File parent = dest.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { System.out.println("Failed to create directory : " + dest.getParentFile().getAbsolutePath()); } byte[] buffer = new byte[1024]; if (urlStr.endsWith(".zip")) { try { URL url = new URL(urlStr); ZipInputStream zipIn = new ZipInputStream(url.openStream()); ZipEntry entry; boolean isFound = false; while ((entry = zipIn.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { isFound = true; FileOutputStream out = new FileOutputStream(dest); int read = 0; while ((read = zipIn.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } else { System.out.println("Found " + entry.getName()); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } else if (urlStr.endsWith(".tar.gz")) { try { URL url = new URL(urlStr); GZIPInputStream gzis = new GZIPInputStream(url.openStream()); File tarFile = new File(dest.getAbsolutePath() + ".tar"); FileOutputStream out = new FileOutputStream(tarFile); int len; while ((len = gzis.read(buffer)) > 0) { out.write(buffer, 0, len); } gzis.close(); out.close(); TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(tarFile)); ArchiveEntry entry; boolean isFound = false; while ((entry = tar.getNextEntry()) != null) { if (destFile.equals(entry.getName())) { out = new FileOutputStream(dest); isFound = true; int read = 0; while ((read = tar.read(buffer)) != -1) { out.write(buffer, 0, read); } out.close(); System.out.println("Updated: " + dest.getAbsolutePath()); } } tar.close(); tarFile.delete(); if (!isFound) { System.out.println("Failed to find " + destFile); System.exit(1); } } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } }
From source file:uk.ac.man.cs.mdsd.webgen.ui.wizards.NewWebgenProjectOperation.java
@SuppressWarnings("unused") private void extractTGZ(final URL sourceURL, final IPath rootPath, final int removeSegemntsCount, final IPath[] ignoreFilter, final Map<IPath, IPath> renames) { try {/*from ww w . j a v a 2s. co m*/ final File tgzPath = new File(FileLocator.resolve(sourceURL).toURI()); final TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzPath)))); TarArchiveEntry entry = (TarArchiveEntry) tarIn.getNextEntry(); while (entry != null) { IPath path = new Path(entry.getName()).removeFirstSegments(removeSegemntsCount); if (renames.containsKey(path)) { path = renames.get(path); } if (rootPath != null) { path = new Path(rootPath.toString() + path.toString()); } if (!path.isEmpty() && !ignoreEntry(path, ignoreFilter)) { if (entry.isDirectory()) { final IFolder folder = projectHandle.getFolder(path); if (!folder.exists()) { folder.create(false, true, null); } } else { byte[] contents = new byte[(int) entry.getSize()]; tarIn.read(contents); final IFile file = projectHandle.getFile(path); file.create(new ByteArrayInputStream(contents), false, null); } } entry = (TarArchiveEntry) tarIn.getNextEntry(); } tarIn.close(); } catch (URISyntaxException | IOException | CoreException e) { WebgenUiPlugin.log(e); } }