List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:com.eleybourn.bookcatalogue.backup.tar.TarBackupReader.java
/** * Constructor/*from ww w. ja va2 s . co m*/ * * @param container Parent * * @throws IOException */ public TarBackupReader(TarBackupContainer container) throws IOException { mContainer = container; // Open the file and create the archive stream FileInputStream in = new FileInputStream(container.getFile()); mInput = new TarArchiveInputStream(in); // Process the INFO entry. Should be first. ReaderEntity info = nextEntity(); if (info.getType() != BackupEntityType.Info) throw new RuntimeException("Not a valid backup"); // Save the INFO mInfo = new BackupInfo(info.getBundle()); // Skip any following INFOs. Later versions may store more. while (info != null && info.getType() == BackupEntityType.Info) { info = nextEntity(); } // Save the 'peeked' entity mPushedEntity = info; }
From source file:br.com.thiaguten.archive.GzipArchive.java
@Override protected ArchiveInputStream createArchiveInputStream(InputStream inputStream) throws IOException { return new TarArchiveInputStream(new GzipCompressorInputStream(inputStream)); }
From source file:ezbake.deployer.utilities.VersionHelper.java
public static String getVersionFromArtifact(ByteBuffer artifact) throws IOException { String versionNumber = null;/*w w w. j av a2 s . co m*/ try (CompressorInputStream uncompressedInput = new GzipCompressorInputStream( new ByteArrayInputStream(artifact.array()))) { ArchiveInputStream input = new TarArchiveInputStream(uncompressedInput); TarArchiveEntry entry; while ((entry = (TarArchiveEntry) input.getNextEntry()) != null) { if (!entry.isDirectory() && entry.getName().endsWith(VERSION_FILE)) { versionNumber = IOUtils.toString(input, Charsets.UTF_8).trim(); break; } } } return versionNumber; }
From source file:com.google.devtools.build.lib.bazel.repository.TarGzFunction.java
@Nullable @Override/*from ww w . j a v a 2s. com*/ public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctionException { DecompressorDescriptor descriptor = (DecompressorDescriptor) skyKey.argument(); Optional<String> prefix = descriptor.prefix(); boolean foundPrefix = false; try (GZIPInputStream gzipStream = new GZIPInputStream( new FileInputStream(descriptor.archivePath().getPathFile()))) { TarArchiveInputStream tarStream = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry; while ((entry = tarStream.getNextTarEntry()) != null) { StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix); foundPrefix = foundPrefix || entryPath.foundPrefix(); if (entryPath.skip()) { continue; } Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment()); FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory()); if (entry.isDirectory()) { FileSystemUtils.createDirectoryAndParents(filename); } else { if (entry.isSymbolicLink()) { PathFragment linkName = new PathFragment(entry.getLinkName()); if (linkName.isAbsolute()) { linkName = linkName.relativeTo(PathFragment.ROOT_DIR); linkName = descriptor.repositoryPath().getRelative(linkName).asFragment(); } FileSystemUtils.ensureSymbolicLink(filename, linkName); } else { Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING); filename.chmod(entry.getMode()); } } } } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } if (prefix.isPresent() && !foundPrefix) { throw new RepositoryFunctionException( new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"), Transience.PERSISTENT); } return new DecompressorValue(descriptor.repositoryPath()); }
From source file:com.amaze.filemanager.asynchronous.asynctasks.compress.GzipHelperTask.java
@Override void addElements(ArrayList<CompressedObjectParcelable> elements) { TarArchiveInputStream tarInputStream = null; try {// w w w . j ava 2 s . c om tarInputStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(filePath))); TarArchiveEntry entry; while ((entry = tarInputStream.getNextTarEntry()) != null) { String name = entry.getName(); if (!CompressedHelper.isEntryPathValid(name)) { AppConfig.toast(context.get(), context.get().getString(R.string.multiple_invalid_archive_entries)); continue; } if (name.endsWith(SEPARATOR)) name = name.substring(0, name.length() - 1); boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR); boolean isInRelativeDir = name.contains(SEPARATOR) && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath); if (isInBaseDir || isInRelativeDir) { elements.add(new CompressedObjectParcelable(entry.getName(), entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory())); } } } catch (IOException e) { e.printStackTrace(); } }
From source file:net.rwx.maven.asciidoc.utils.FileUtils.java
public static String uncompress(InputStream is, String destination) throws IOException { BufferedInputStream in = new BufferedInputStream(is); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = tarInput.getNextTarEntry(); do {/* w w w . ja va2 s . c o m*/ File f = new File(destination + "/" + entry.getName()); FileUtils.forceMkdir(f.getParentFile()); if (!f.isDirectory()) { OutputStream os = new FileOutputStream(f); byte[] content = new byte[(int) entry.getSize()]; int byteRead = 0; while (byteRead < entry.getSize()) { byteRead += tarInput.read(content, byteRead, content.length - byteRead); os.write(content, 0, byteRead); } os.close(); forceDeleteOnExit(f); } entry = tarInput.getNextTarEntry(); } while (entry != null); gzIn.close(); return destination; }
From source file:es.ucm.fdi.util.archive.TarFormat.java
public void expand(File source, File destDir) throws IOException { try (InputStream is = getTarInputStream(source); TarArchiveInputStream tis = new TarArchiveInputStream(is)) { byte[] b = new byte[512]; for (TarArchiveEntry e; (e = tis.getNextTarEntry()) != null; /**/) { // backslash-protection: zip format expects only 'fw' slashes //System.err.println("Found entry: "+e.getName()); String name = FileUtils.toCanonicalPath(e.getName()); if (e.isDirectory()) { //log.debug("\tExtracting directory "+e.getName()); File dir = new File(destDir, name); dir.mkdirs();// ww w . j a v a 2s. c om continue; } //System.err.println("\tExtracting file "+name); File outFile = new File(destDir, name); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } try (FileOutputStream fos = new FileOutputStream(outFile)) { int len; while ((len = tis.read(b)) != -1) { fos.write(b, 0, len); } } } } }
From source file:jetbrains.exodus.util.CompressBackupUtilTest.java
@Test public void testFileArchived() throws Exception { File src = new File(randName + ".txt"); FileWriter fw = new FileWriter(src); fw.write("12345"); fw.close();//from ww w. j a va 2 s .co m CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry = tai.getNextEntry(); Assert.assertNotNull("No entry found in destination archive", entry); Assert.assertEquals("Entry has wrong size", 5, entry.getSize()); }
From source file:indexing.WTDocIndexer.java
void indexTarArchive() throws Exception { GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(new File(tarArchivePath))); TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream); TarArchiveEntry tarEntry = tarArchiveInputStream.getNextTarEntry(); while (tarEntry != null) { if (!tarEntry.isDirectory()) { System.out.println("Extracting " + tarEntry.getName()); final OutputStream outputFileStream = new FileOutputStream(tarEntry.getName()); } else {// w w w . j a va 2s. co m tarEntry = tarArchiveInputStream.getNextTarEntry(); } } }
From source file:com.torchmind.upm.bundle.BasicResource.java
/** * {@inheritDoc}/*from www .j av a2 s. c o m*/ */ @Nonnull @Override public ArchiveInputStream createArchiveInputStream() throws IllegalStateException, IOException { switch (this.type) { case RAW: throw new IllegalStateException("Cannot convert RAW resource into archive"); case JAR: return new JarArchiveInputStream(this.createInputStream()); case TAR_ARCHIVE: return new TarArchiveInputStream(this.createInputStream()); case TAR_GZ_ARCHIVE: return new TarArchiveInputStream(new GzipCompressorInputStream(this.createInputStream())); case TAR_XZ_ARCHIVE: return new TarArchiveInputStream(new XZCompressorInputStream(this.createInputStream())); case ZIP_ARCHIVE: return new ZipArchiveInputStream(this.createInputStream()); } throw new UnsupportedOperationException("No such resource type: " + this.type); }