List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:org.eclipse.che.api.vfs.TarArchiver.java
@Override public void extract(InputStream tarInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException { try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) { InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream); TarArchiveEntry tarEntry;//from w w w. j ava2 s.c om while ((tarEntry = tarInputStream.getNextTarEntry()) != null) { VirtualFile extractFolder = folder; Path relativePath = Path.of(tarEntry.getName()); if (stripNumber > 0) { if (relativePath.length() <= stripNumber) { continue; } relativePath = relativePath.subPath(stripNumber); } if (tarEntry.isDirectory()) { if (!extractFolder.hasChild(relativePath)) { extractFolder.createFolder(relativePath.toString()); } continue; } if (relativePath.length() > 1) { Path neededParentPath = relativePath.getParent(); VirtualFile neededParent = extractFolder.getChild(neededParentPath); if (neededParent == null) { neededParent = extractFolder.createFolder(neededParentPath.toString()); } extractFolder = neededParent; } String fileName = relativePath.getName(); VirtualFile file = extractFolder.getChild(Path.of(fileName)); if (file == null) { extractFolder.createFile(fileName, notClosableInputStream); } else { if (overwrite) { file.updateContent(notClosableInputStream); } else { throw new ConflictException(String.format("File '%s' already exists", file.getPath())); } } } } }
From source file:org.eclipse.che.api.vfs.TarArchiverTest.java
private Map<String, String> readArchiveEntries(InputStream archive) throws Exception { Map<String, String> entries = newHashMap(); try (TarArchiveInputStream tarIn = new TarArchiveInputStream(archive)) { TarArchiveEntry tarArchiveEntry; while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) { String name = tarArchiveEntry.getName(); String content = tarArchiveEntry.isDirectory() ? "<none>" : new String(ByteStreams.toByteArray(tarIn)); entries.put(name, content);/*from w ww . ja v a2s . c o m*/ } } return entries; }
From source file:org.eclipse.che.api.vfs.TarArchiverTest.java
private void assertThatTarArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception { try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) { TarArchiveEntry tarArchiveEntry; while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) { String name = tarArchiveEntry.getName(); assertTrue(String.format("Unexpected entry %s in TAR", name), entries.containsKey(name)); if (!tarArchiveEntry.isDirectory()) { String content = new String(ByteStreams.toByteArray(tarIn)); assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content); }/*from w w w. ja va 2s.c o m*/ entries.remove(name); } } assertTrue(String.format("Expected but were not found in TAR %s", entries), entries.isEmpty()); }
From source file:org.eclipse.che.commons.lang.TarUtils.java
public static void untar(InputStream in, File targetDir) throws IOException { final TarArchiveInputStream tarIn = new TarArchiveInputStream(in); byte[] b = new byte[BUF_SIZE]; TarArchiveEntry tarEntry;// www. j ava2s .c o m while ((tarEntry = tarIn.getNextTarEntry()) != null) { final File file = new File(targetDir, tarEntry.getName()); if (tarEntry.isDirectory()) { if (!file.mkdirs()) { throw new IOException("Unable to create folder " + file.getAbsolutePath()); } } else { final File parent = file.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create folder " + parent.getAbsolutePath()); } } try (FileOutputStream fos = new FileOutputStream(file)) { int r; while ((r = tarIn.read(b)) != -1) { fos.write(b, 0, r); } } } } }
From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarFile.java
/** * Create a new TarFile for the given file. * * @param file the file//from www.j a va 2s .c o m * @throws IOException on i/o error (bad format, etc) */ public TarFile(File file) throws IOException { this.file = file; fInputStream = new FileInputStream(file); // First, check if it's a GZIPInputStream. try { fInputStream = new GzipCompressorInputStream(fInputStream); } catch (IOException e) { //If it is not compressed we close //the old one and recreate fInputStream.close(); fInputStream = new FileInputStream(file); } entryEnumerationStream = new TarArchiveInputStream(fInputStream); try { curEntry = (TarArchiveEntry) entryEnumerationStream.getNextEntry(); if (!curEntry.isCheckSumOK()) { throw new IOException("Error detected parsing initial entry header"); //$NON-NLS-1$ } } catch (IOException e) { fInputStream.close(); throw e; } }
From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarFile.java
/** * Returns a new InputStream for the given file in the tar archive. * * @param entry the entry to get the InputStream from * @return an input stream for the given file * @throws IOException on i/o error (bad format, etc) *//* www . j ava 2s . c o m*/ public InputStream getInputStream(TarArchiveEntry entry) throws IOException { if (entryStream == null || !skipToEntry(entryStream, entry)) { if (internalEntryStream != null) { internalEntryStream.close(); } internalEntryStream = new FileInputStream(file); // First, check if it's a GzipCompressorInputStream. try { internalEntryStream = new GzipCompressorInputStream(internalEntryStream); } catch (IOException e) { //If it is not compressed we close //the old one and recreate internalEntryStream.close(); internalEntryStream = new FileInputStream(file); } entryStream = new TarArchiveInputStream(internalEntryStream) { @Override public void close() { // Ignore close() since we want to reuse the stream. } }; skipToEntry(entryStream, entry); } return entryStream; }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java
private Map<String, TarArchiveEntry> getTarEntries() throws IOException, FileNotFoundException { TarArchiveInputStream tarStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarGzArchive))); Map<String, TarArchiveEntry> entries = new HashMap<String, TarArchiveEntry>(); try {/*from w w w . j a va 2 s . c o m*/ TarArchiveEntry tarEntry = null; while ((tarEntry = tarStream.getNextTarEntry()) != null) { entries.put(tarEntry.getName(), tarEntry); } } finally { tarStream.close(); } return entries; }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java
private byte[] getTarEntry(String name) throws IOException { TarArchiveInputStream tarStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarGzArchive))); try {// w ww. j a v a 2s .co m TarArchiveEntry tarEntry = null; while ((tarEntry = tarStream.getNextTarEntry()) != null) { if (name.equals(tarEntry.getName())) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(tarStream, baos); return baos.toByteArray(); } } } finally { tarStream.close(); } throw new IOException(name + " not found in " + tarGzArchive); }
From source file:org.efaps.esjp.admin.update.UpdatePack.java
/** * Check revisions.//from w w w. ja v a 2s. c o m * * @param _parameter Parameter as passed by the eFaps API * @return the return * @throws EFapsException on error * @throws InstallationException on error */ public Return execute(final Parameter _parameter) throws EFapsException, InstallationException { final Context context = Context.getThreadContext(); final Context.FileParameter fileItem = context.getFileParameters().get("pack"); final boolean compress = GzipUtils.isCompressedFilename(fileItem.getName()); try (final TarArchiveInputStream tarInput = new TarArchiveInputStream( compress ? new GzipCompressorInputStream(fileItem.getInputStream()) : fileItem.getInputStream());) { File tmpfld = AppConfigHandler.get().getTempFolder(); if (tmpfld == null) { final File temp = File.createTempFile("eFaps", ".tmp"); tmpfld = temp.getParentFile(); temp.delete(); } final File updateFolder = new File(tmpfld, Update.TMPFOLDERNAME); if (!updateFolder.exists()) { updateFolder.mkdirs(); } final File dateFolder = new File(updateFolder, ((Long) new Date().getTime()).toString()); dateFolder.mkdirs(); final Map<String, URL> files = new HashMap<>(); TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); while (currentEntry != null) { final byte[] bytess = new byte[(int) currentEntry.getSize()]; tarInput.read(bytess); final File file = new File(dateFolder.getAbsolutePath() + "/" + currentEntry.getName()); file.getParentFile().mkdirs(); final FileOutputStream output = new FileOutputStream(file); output.write(bytess); output.close(); files.put(currentEntry.getName(), file.toURI().toURL()); currentEntry = tarInput.getNextTarEntry(); } final Map<RevItem, InstallFile> installFiles = new HashMap<>(); final URL json = files.get("revisions.json"); final ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule()); final List<RevItem> items = mapper.readValue(new File(json.toURI()), mapper.getTypeFactory().constructCollectionType(List.class, RevItem.class)); final List<RevItem> allItems = new ArrayList<>(); allItems.addAll(items); installFiles.putAll(getInstallFiles(files, items, CIAdmin.Abstract)); installFiles.putAll(getInstallFiles(files, items, CIAdminUser.Abstract)); installFiles.putAll(getInstallFiles(files, items, CIAdminAccess.AccessSet)); installFiles.putAll(getInstallFiles(files, items, CICommon.DBPropertiesBundle)); final Iterator<RevItem> iter = items.iterator(); int i = 0; while (iter.hasNext()) { final RevItem item = iter.next(); LOG.info("Adding unfound Item {} / {}: {}", i, items.size(), item.getIdentifier()); final InstallFile installFile = new InstallFile().setName(item.getName4InstallFile()) .setURL(item.getURL(files)).setType(item.getFileType().getType()) .setRevision(item.getRevision()).setDate(item.getDate()); installFiles.put(item, installFile); i++; } final List<InstallFile> installFileList = new ArrayList<>(installFiles.values()); Collections.sort(installFileList, new Comparator<InstallFile>() { @Override public int compare(final InstallFile _installFile0, final InstallFile _installFile1) { return _installFile0.getName().compareTo(_installFile1.getName()); } }); final List<InstallFile> dependendFileList = new ArrayList<>(); // check if a object that depends on another object must be added to the update final Map<String, String> depenMap = getDependendMap(); final Set<String> tobeAdded = new HashSet<>(); for (final RevItem item : installFiles.keySet()) { if (depenMap.containsKey(item.getIdentifier())) { tobeAdded.add(depenMap.get(item.getIdentifier())); } } if (!tobeAdded.isEmpty()) { // check if the object to be added is already part ot the list for (final RevItem item : installFiles.keySet()) { final Iterator<String> tobeiter = tobeAdded.iterator(); while (tobeiter.hasNext()) { final String ident = tobeiter.next(); if (item.getIdentifier().equals(ident)) { tobeiter.remove(); } } } } if (!tobeAdded.isEmpty()) { i = 1; // add the objects to the list taht are missing for (final RevItem item : allItems) { if (tobeAdded.contains(item.getIdentifier())) { LOG.info("Adding releated Item {} / {}: {}", i, tobeAdded.size(), item); final InstallFile installFile = new InstallFile().setName(item.getName4InstallFile()) .setURL(item.getURL(files)).setType(item.getFileType().getType()) .setRevision(item.getRevision()).setDate(item.getDate()); dependendFileList.add(installFile); i++; } } } if (!installFileList.isEmpty()) { final Install install = new Install(true); for (final InstallFile installFile : installFileList) { LOG.info("...Adding to Update: '{}' ", installFile.getName()); install.addFile(installFile); } install.updateLatest(null); } if (!dependendFileList.isEmpty()) { LOG.info("Update for related Items"); final Install install = new Install(true); for (final InstallFile installFile : dependendFileList) { LOG.info("...Adding to Update: '{}' ", installFile.getName()); install.addFile(installFile); } install.updateLatest(null); } LOG.info("Terminated update."); } catch (final IOException e) { LOG.error("Catched", e); } catch (final URISyntaxException e) { LOG.error("Catched", e); } return new Return(); }
From source file:org.exist.xquery.modules.compression.UnTarFunction.java
@Override protected Sequence processCompressedData(BinaryValue compressedData) throws XPathException, XMLDBException { TarArchiveInputStream tis = null;//from w ww . ja v a2 s. c o m try { tis = new TarArchiveInputStream(compressedData.getInputStream()); TarArchiveEntry entry = null; Sequence results = new ValueSequence(); while ((entry = tis.getNextTarEntry()) != null) { Sequence processCompressedEntryResults = processCompressedEntry(entry.getName(), entry.isDirectory(), tis, filterParam, storeParam); results.addAll(processCompressedEntryResults); } return results; } catch (IOException ioe) { LOG.error(ioe.getMessage(), ioe); throw new XPathException(this, ioe.getMessage(), ioe); } finally { if (tis != null) { try { tis.close(); } catch (IOException ioe) { LOG.warn(ioe.getMessage(), ioe); } } } }