List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:com.spotify.docker.client.DefaultDockerClientTest.java
@Test public void integrationTest() throws Exception { // Pull image sut.pull(BUSYBOX_LATEST);/*from w w w .ja v a 2 s.c om*/ // Create container final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST) .cmd("sh", "-c", "while :; do sleep 1; done").build(); final String name = randomName(); final ContainerCreation creation = sut.createContainer(config, name); final String id = creation.id(); assertThat(creation.getWarnings(), anyOf(is(empty()), is(nullValue()))); assertThat(id, is(any(String.class))); // Inspect using container ID { final ContainerInfo info = sut.inspectContainer(id); assertThat(info.id(), equalTo(id)); assertThat(info.config().image(), equalTo(config.image())); assertThat(info.config().cmd(), equalTo(config.cmd())); } // Inspect using container name { final ContainerInfo info = sut.inspectContainer(name); assertThat(info.config().image(), equalTo(config.image())); assertThat(info.config().cmd(), equalTo(config.cmd())); } // Start container sut.startContainer(id); final String dockerDirectory = Resources.getResource("dockerSslDirectory").getPath(); // Copy files to container // Docker API should be at least v1.20 to support extracting an archive of files or folders // to a directory in a container if (compareVersion(sut.version().apiVersion(), "1.20") >= 0) { try { sut.copyToContainer(Paths.get(dockerDirectory), id, "/tmp"); } catch (Exception e) { fail("error copying files to container"); } // Copy the same files from container final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder(); try (TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.copyContainer(id, "/tmp"))) { TarArchiveEntry entry; while ((entry = tarStream.getNextTarEntry()) != null) { filesDownloaded.add(entry.getName()); } } // Check that we got back what we put in final File folder = new File(dockerDirectory); final File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (!file.isDirectory()) { Boolean found = false; for (String fileDownloaded : filesDownloaded.build()) { if (fileDownloaded.contains(file.getName())) { found = true; } } assertTrue(found); } } } } // Kill container sut.killContainer(id); try { // Remove the container sut.removeContainer(id); } catch (DockerRequestException e) { // CircleCI doesn't let you remove a container :( if (!CIRCLECI) { // Verify that the container is gone exception.expect(ContainerNotFoundException.class); sut.inspectContainer(id); } } }
From source file:net.staticsnow.nexus.repository.apt.internal.hosted.AptHostedFacet.java
@Transactional(retryOn = { ONeedRetryException.class }) public void ingestAsset(Payload body) throws IOException, PGPException { AptFacet aptFacet = getRepository().facet(AptFacet.class); StorageTx tx = UnitOfWork.currentTx(); Bucket bucket = tx.findBucket(getRepository()); ControlFile control = null;// w w w . j a va 2 s.co m try (TempStreamSupplier supplier = new TempStreamSupplier(body.openInputStream()); ArArchiveInputStream is = new ArArchiveInputStream(supplier.get())) { ArchiveEntry debEntry; while ((debEntry = is.getNextEntry()) != null) { InputStream controlStream; switch (debEntry.getName()) { case "control.tar": controlStream = new CloseShieldInputStream(is); break; case "control.tar.gz": controlStream = new GZIPInputStream(new CloseShieldInputStream(is)); break; case "control.tar.xz": controlStream = new XZCompressorInputStream(new CloseShieldInputStream(is)); default: continue; } try (TarArchiveInputStream controlTarStream = new TarArchiveInputStream(controlStream)) { ArchiveEntry tarEntry; while ((tarEntry = controlTarStream.getNextEntry()) != null) { if (tarEntry.getName().equals("control") || tarEntry.getName().equals("./control")) { control = new ControlFileParser().parseControlFile(controlTarStream); } } } } if (control == null) { throw new IllegalOperationException("Invalid Debian package supplied"); } String name = control.getField("Package").map(f -> f.value).get(); String version = control.getField("Version").map(f -> f.value).get(); String architecture = control.getField("Architecture").map(f -> f.value).get(); String assetName = name + "_" + version + "_" + architecture + ".deb"; String assetPath = "pool/" + name.substring(0, 1) + "/" + name + "/" + assetName; Content content = aptFacet.put(assetPath, new StreamPayload(() -> supplier.get(), body.getSize(), body.getContentType())); Asset asset = Content.findAsset(tx, bucket, content); String indexSection = buildIndexSection(control, asset.size(), asset.getChecksums(FacetHelper.hashAlgorithms), assetPath); asset.formatAttributes().set(P_ARCHITECTURE, architecture); asset.formatAttributes().set(P_PACKAGE_NAME, name); asset.formatAttributes().set(P_PACKAGE_VERSION, version); asset.formatAttributes().set(P_INDEX_SECTION, indexSection); asset.formatAttributes().set(P_ASSET_KIND, "DEB"); tx.saveAsset(asset); List<AssetChange> changes = new ArrayList<>(); changes.add(new AssetChange(AssetAction.ADDED, asset)); for (Asset removed : selectOldPackagesToRemove(name, architecture)) { tx.deleteAsset(removed); changes.add(new AssetChange(AssetAction.REMOVED, removed)); } rebuildIndexesInTransaction(tx, changes.stream().toArray(AssetChange[]::new)); } }
From source file:net.yacy.document.parser.tarParser.java
@Override public Document[] parse(final DigestURL location, final String mimeType, final String charset, final VocabularyScraper scraper, final int timezoneOffset, InputStream source) throws Parser.Failure, InterruptedException { final String filename = location.getFileName(); final String ext = MultiProtocolURL.getFileExtension(filename); if (ext.equals("gz") || ext.equals("tgz")) { try {//from www . j av a 2 s . c o m source = new GZIPInputStream(source); } catch (final IOException e) { throw new Parser.Failure("tar parser: " + e.getMessage(), location); } } TarArchiveEntry entry; final TarArchiveInputStream tis = new TarArchiveInputStream(source); // create maindoc for this bzip container final Document maindoc = new Document(location, mimeType, charset, this, null, null, AbstractParser .singleList(filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename)), // title null, null, null, null, 0.0d, 0.0d, (Object) null, null, null, null, false, new Date()); // loop through the elements in the tar file and parse every single file inside while (true) { try { File tmp = null; entry = tis.getNextTarEntry(); if (entry == null) break; if (entry.isDirectory() || entry.getSize() <= 0) continue; final String name = entry.getName(); final int idx = name.lastIndexOf('.'); final String mime = TextParser.mimeOf((idx > -1) ? name.substring(idx + 1) : ""); try { tmp = FileUtils.createTempFile(this.getClass(), name); FileUtils.copy(tis, tmp, entry.getSize()); final Document[] subDocs = TextParser.parseSource(AnchorURL.newAnchor(location, "#" + name), mime, null, scraper, timezoneOffset, 999, tmp); if (subDocs == null) continue; maindoc.addSubDocuments(subDocs); } catch (final Parser.Failure e) { AbstractParser.log.warn("tar parser entry " + name + ": " + e.getMessage()); } finally { if (tmp != null) FileUtils.deletedelete(tmp); } } catch (final IOException e) { AbstractParser.log.warn("tar parser:" + e.getMessage()); break; } } return new Document[] { maindoc }; }
From source file:net.yacy.utils.tarTools.java
/** * Untar for any tar archive, overwrites existing data. Closes the * InputStream once terminated.//from www . j ava 2 s . co m * * @param in * input stream. Must not be null. (use * {@link #getInputStream(String)} for convenience) * @param untarDir * destination path. Must not be null. * @throws IOException * when a read/write error occurred * @throws FileNotFoundException * when the untarDir does not exists * @throws NullPointerException * when a parameter is null */ public static void unTar(final InputStream in, final String untarDir) throws IOException { ConcurrentLog.info("UNTAR", "starting"); if (new File(untarDir).exists()) { final TarArchiveInputStream tin = new TarArchiveInputStream(in); try { TarArchiveEntry tarEntry = tin.getNextTarEntry(); if (tarEntry == null) { throw new IOException("tar archive is empty or corrupted"); } while (tarEntry != null) { final File destPath = new File(untarDir + File.separator + tarEntry.getName()); if (!tarEntry.isDirectory()) { new File(destPath.getParent()).mkdirs(); // create missing subdirectories final FileOutputStream fout = new FileOutputStream(destPath); IOUtils.copyLarge(tin, fout, 0, tarEntry.getSize()); fout.close(); } else { destPath.mkdir(); } tarEntry = tin.getNextTarEntry(); } } finally { try { tin.close(); } catch (IOException ignored) { ConcurrentLog.warn("UNTAR", "InputStream could not be closed"); } } } else { // untarDir doesn't exist ConcurrentLog.warn("UNTAR", "destination " + untarDir + " doesn't exist."); /* Still have to close the input stream */ try { in.close(); } catch (IOException ignored) { ConcurrentLog.warn("UNTAR", "InputStream could not be closed"); } throw new FileNotFoundException("Output untar directory not found : " + untarDir); } ConcurrentLog.info("UNTAR", "finished"); }
From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java
public static void uncompressArchive(File archive, File target) throws IOException, CompressorException { CompressorInputStream compressor = new GzipCompressorInputStream(new FileInputStream(archive)); TarArchiveInputStream stream = new TarArchiveInputStream(compressor); TarArchiveEntry entry;/* w ww . jav a2 s. c o m*/ while ((entry = stream.getNextTarEntry()) != null) { File f = new File(target.getCanonicalPath(), entry.getName()); if (f.exists()) { Bukkit.getLogger().warning("The file " + f.getCanonicalPath() + " already exists, deleting it."); if (!f.delete()) { Bukkit.getLogger().warning("Cannot remove, skipping file."); } } if (entry.isDirectory()) { f.mkdirs(); continue; } f.getParentFile().mkdirs(); f.createNewFile(); try { try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(f))) { final byte[] buf = new byte[8192]; int bytesRead; while (-1 != (bytesRead = stream.read(buf))) fos.write(buf, 0, bytesRead); } Bukkit.getLogger().info("Extracted file " + f.getName() + "..."); } catch (IOException ioe) { f.delete(); throw ioe; } } }
From source file:org.anurag.compress.ExtractTarFile.java
public ExtractTarFile(final Context ctx, final Item zFile, final int width, String extractDir, final File file, final int mode) { // TODO Auto-generated constructor stub running = false;/*from w w w .j av a 2 s .c o m*/ errors = false; prog = 0; read = 0; final Dialog dialog = new Dialog(ctx, Constants.DIALOG_STYLE); dialog.setCancelable(true); dialog.setContentView(R.layout.extract_file); dialog.getWindow().getAttributes().width = width; DEST = extractDir; final ProgressBar progress = (ProgressBar) dialog.findViewById(R.id.zipProgressBar); final TextView to = (TextView) dialog.findViewById(R.id.zipFileName); final TextView from = (TextView) dialog.findViewById(R.id.zipLoc); final TextView cfile = (TextView) dialog.findViewById(R.id.zipSize); final TextView zsize = (TextView) dialog.findViewById(R.id.zipNoOfFiles); final TextView status = (TextView) dialog.findViewById(R.id.zipFileLocation); if (extractDir == null) to.setText(ctx.getString(R.string.extractingto) + " Cache directory"); else to.setText(ctx.getString(R.string.extractingto) + " " + DEST); from.setText(ctx.getString(R.string.extractingfrom) + " " + file.getName()); if (mode == 2) { //TAR ENTRY HAS TO BE SHARED VIA BLUETOOTH,ETC... TextView t = null;//= (TextView)dialog.findViewById(R.id.preparing); t.setText(ctx.getString(R.string.preparingtoshare)); } try { if (file.getName().endsWith(".tar.gz")) tar = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(file))); else tar = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(file))); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); tar = null; } final Handler handle = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: progress.setProgress(0); cfile.setText(ctx.getString(R.string.extractingfile) + " " + name); break; case 1: status.setText(name); progress.setProgress((int) prog); break; case 2: try { tar.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (running) { dialog.dismiss(); if (mode == 0) { //after extracting file ,it has to be opened.... new OpenFileDialog(ctx, Uri.parse(dest)); } else if (mode == 2) { //FILE HAS TO BE SHARED.... new BluetoothChooser(ctx, new File(dest).getAbsolutePath(), null); } else { if (errors) Toast.makeText(ctx, ctx.getString(R.string.errorinext), Toast.LENGTH_SHORT).show(); Toast.makeText(ctx, ctx.getString(R.string.fileextracted), Toast.LENGTH_SHORT).show(); } } break; case 3: zsize.setText(size); progress.setMax((int) max); break; case 4: status.setText(ctx.getString(R.string.preparing)); break; case 5: running = false; Toast.makeText(ctx, ctx.getString(R.string.extaborted), Toast.LENGTH_SHORT).show(); } } }; final Thread thread = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if (running) { if (DEST == null) { DEST = Environment.getExternalStorageDirectory() + "/Android/data/org.anurag.file.quest"; new File(DEST).mkdirs(); } TarArchiveEntry ze; try { while ((ze = tar.getNextTarEntry()) != null) { if (ze.isDirectory()) continue; handle.sendEmptyMessage(4); if (!zFile.isDirectory()) { //EXTRACTING A SINGLE FILE FROM AN ARCHIVE.... if (ze.getName().equalsIgnoreCase(zFile.t_getEntryName())) { try { //SENDING CURRENT FILE NAME.... try { name = zFile.getName(); } catch (Exception e) { name = zFile.t_getEntryName(); } handle.sendEmptyMessage(0); dest = DEST; dest = dest + "/" + name; FileOutputStream out = new FileOutputStream((dest)); max = ze.getSize(); size = AppBackup.size(max, ctx); handle.sendEmptyMessage(3); InputStream fin = tar; while ((read = fin.read(data)) != -1 && running) { out.write(data, 0, read); prog += read; name = AppBackup.status(prog, ctx); handle.sendEmptyMessage(1); } out.flush(); out.close(); fin.close(); break; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); //errors = true; } catch (IOException e) { errors = true; } } } else { //EXTRACTING A DIRECTORY FROM TAR ARCHIVE.... String p = zFile.getPath(); if (p.startsWith("/")) p = p.substring(1, p.length()); if (ze.getName().startsWith(p)) { prog = 0; dest = DEST; name = ze.getName(); String path = name; name = name.substring(name.lastIndexOf("/") + 1, name.length()); handle.sendEmptyMessage(0); String foname = zFile.getPath(); if (!foname.startsWith("/")) foname = "/" + foname; if (!path.startsWith("/")) path = "/" + path; path = path.substring(foname.lastIndexOf("/"), path.lastIndexOf("/")); if (!path.startsWith("/")) path = "/" + path; dest = dest + path; new File(dest).mkdirs(); dest = dest + "/" + name; FileOutputStream out; try { max = ze.getSize(); out = new FileOutputStream((dest)); size = AppBackup.size(max, ctx); handle.sendEmptyMessage(3); // InputStream fin = tar; while ((read = tar.read(data)) != -1 && running) { out.write(data, 0, read); prog += read; name = AppBackup.status(prog, ctx); handle.sendEmptyMessage(1); } out.flush(); out.close(); //fin.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); // errors = true; } catch (IOException e) { errors = true; } catch (Exception e) { //errors = true; } } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); errors = true; } handle.sendEmptyMessage(2); } } }); /* Button cancel = (Button)dialog.findViewById(R.id.calcelButton); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dialog.dismiss(); handle.sendEmptyMessage(5); } }); Button st = (Button)dialog.findViewById(R.id.extractButton); st.setVisibility(View.GONE);*/ dialog.show(); running = true; thread.start(); dialog.setCancelable(false); progress.setVisibility(View.VISIBLE); }
From source file:org.anurag.compress.TarManager.java
/** * /*from w w w .j a va 2s.c om*/ * THE DIRECTORY ENTRIES CANNOT BE SKIPPED LIKE WE SKIPPED IN * RAR OR ZIP MANAGER.... * * @return * @throws IOException */ public ArrayList<Item> generateList() { list = new ArrayList<Item>(); TarArchiveEntry entry; try { if (f.getName().endsWith(".tar.gz")) tar = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(f)))); else if (f.getName().endsWith(".tar.bz2") || f.getName().endsWith(".TAR.BZ2")) tar = new TarArchiveInputStream( new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(f)))); else tar = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(f))); while ((entry = tar.getNextTarEntry()) != null) { boolean added = false; int len = list.size(); String name = entry.getName(); if (name.startsWith("/")) name = name.substring(1, name.length()); if (entry.isDirectory()) name = name.substring(0, name.length() - 1); if (path.equalsIgnoreCase("/")) { while (name.contains("/")) name = name.substring(0, name.lastIndexOf("/")); for (int i = 0; i < len; ++i) { if (list.get(i).getName().equalsIgnoreCase(name)) { added = true; break; } } if (!added && !name.equalsIgnoreCase("")) list.add(new Item(entry, name, "", ctx)); } else { try { name = name.substring(path.length() + 1, name.length()); while (name.contains("/")) name = name.substring(0, name.lastIndexOf("/")); if (len > 0) { for (int i = 0; i < len; ++i) if (list.get(i).getName().equalsIgnoreCase(name)) { added = true; break; } if (!added && entry.getName().startsWith(path)) list.add(new Item(entry, name, path, ctx)); } else if (entry.getName().startsWith(path)) list.add(new Item(entry, name, path, ctx)); } catch (Exception e) { } } } tar.close(); sort(); return list; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return null; } }
From source file:org.apache.bookkeeper.tests.integration.utils.DockerUtils.java
public static void dumpContainerLogDirToTarget(DockerClient docker, String containerId, String path) { final int readBlockSize = 10000; try (InputStream dockerStream = docker.copyArchiveFromContainerCmd(containerId, path).exec(); TarArchiveInputStream stream = new TarArchiveInputStream(dockerStream)) { TarArchiveEntry entry = stream.getNextTarEntry(); while (entry != null) { if (entry.isFile()) { File output = new File(getTargetDirectory(containerId), entry.getName().replace("/", "-")); try (FileOutputStream os = new FileOutputStream(output)) { byte[] block = new byte[readBlockSize]; int read = stream.read(block, 0, readBlockSize); while (read > -1) { os.write(block, 0, read); read = stream.read(block, 0, readBlockSize); }/* w ww. j a v a2 s .com*/ } } entry = stream.getNextTarEntry(); } } catch (RuntimeException | IOException e) { LOG.error("Error reading bk logs from container {}", containerId, e); } }
From source file:org.apache.camel.processor.aggregate.tarfile.AggregationStrategyWithPreservationTest.java
@Test public void testSplitter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry"); mock.expectedMessageCount(1);// ww w. j av a 2 s.c o m assertMockEndpointsSatisfied(); Thread.sleep(500); File[] files = new File("target/out").listFiles(); assertTrue("Should be a file in target/out directory", files.length > 0); File resultFile = files[0]; Set<String> expectedTarFiles = new HashSet<String>( Arrays.asList("another/hello.txt", "other/greetings.txt", "chiau.txt", "hi.txt", "hola.txt")); TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(resultFile)); try { int fileCount = 0; for (TarArchiveEntry te = tin.getNextTarEntry(); te != null; te = tin.getNextTarEntry()) { expectedTarFiles.remove(te.getName()); fileCount++; } assertTrue("Tar file should contains " + AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES + " files", fileCount == AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES); assertEquals("Should have found all of the tar files in the file.", 0, expectedTarFiles.size()); } finally { IOHelper.close(tin); } }
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategyTest.java
@Test public void testSplitter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry"); mock.expectedMessageCount(1);//from w w w .j a va 2s .c om mock.expectedHeaderReceived("foo", "bar"); assertMockEndpointsSatisfied(); Thread.sleep(500); File[] files = new File("target/out").listFiles(); assertTrue(files != null); assertTrue("Should be a file in target/out directory", files.length > 0); File resultFile = files[0]; TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(resultFile)); try { int fileCount = 0; for (TarArchiveEntry te = tin.getNextTarEntry(); te != null; te = tin.getNextTarEntry()) { fileCount = fileCount + 1; } assertEquals("Tar file should contains " + TarAggregationStrategyTest.EXPECTED_NO_FILES + " files", TarAggregationStrategyTest.EXPECTED_NO_FILES, fileCount); } finally { IOHelper.close(tin); } }