List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:org.codehaus.mojo.unix.deb.DpkgDebTool.java
private static List<UnixFsObject> process(InputStream is) throws IOException { TarArchiveInputStream tarInputStream = new TarArchiveInputStream(is); List<UnixFsObject> objects = new ArrayList<UnixFsObject>(); TarArchiveEntry entry = (TarArchiveEntry) tarInputStream.getNextEntry(); while (entry != null) { Option<UnixFileMode> mode = some(UnixFileMode.fromInt(entry.getMode())); FileAttributes attributes = new FileAttributes(some(entry.getUserName()), some(entry.getGroupName()), mode);//ww w .j a v a 2 s . co m RelativePath path = relativePath(entry.getName()); LocalDateTime lastModified = LocalDateTime.fromDateFields(entry.getModTime()); UnixFsObject object; if (entry.isDirectory()) { object = directory(path, lastModified, attributes); } else if (entry.isSymbolicLink()) { object = symlink(path, lastModified, some(entry.getUserName()), some(entry.getGroupName()), entry.getLinkName()); } else if (entry.isFile()) { object = regularFile(path, lastModified, entry.getSize(), attributes); } else { throw new IOException("Unsupported link type: name=" + entry.getName()); } objects.add(object); entry = (TarArchiveEntry) tarInputStream.getNextEntry(); } return objects; }
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void createArchive(final int directoryMode, final int fileModes[]) throws Exception { int defaultFileMode = fileModes[0]; int oneFileMode = fileModes[1]; int twoFileMode = fileModes[2]; TarArchiver archiver = getPosixTarArchiver(); archiver.setDirectoryMode(directoryMode); archiver.setFileMode(defaultFileMode); archiver.addDirectory(getTestFile("src/main")); archiver.setFileMode(oneFileMode);/*w w w. j a va2 s . c o m*/ archiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt"); archiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt", twoFileMode); archiver.setDestFile(getTestFile("target/output/archive.tar")); archiver.addSymlink("link_to_test_destinaton", "../test_destination/"); archiver.createArchive(); TarArchiveInputStream tis; tis = new TarArchiveInputStream(bufferedInputStream(new FileInputStream(archiver.getDestFile()))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { if (te.isDirectory()) { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", directoryMode, te.getMode() & UnixStat.PERM_MASK); } else if (te.isSymbolicLink()) { assertEquals("../test_destination/", te.getLinkName()); assertEquals("link_to_test_destinaton", te.getName()); assertEquals(0640, te.getMode() & UnixStat.PERM_MASK); } else { if (te.getName().equals("one.txt")) { assertEquals(oneFileMode, te.getMode() & UnixStat.PERM_MASK); } else if (te.getName().equals("two.txt")) { assertEquals(twoFileMode, te.getMode() & UnixStat.PERM_MASK); } else { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", defaultFileMode, te.getMode() & UnixStat.PERM_MASK); } } } IOUtil.close(tis); }
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void testCreateArchiveWithJiustASymlink() throws Exception { TarArchiver archiver = getPosixTarArchiver(); archiver.setDirectoryMode(0500);/*from w w w. j a v a2s . c o m*/ archiver.setFileMode(0400); archiver.setFileMode(0640); archiver.setDestFile(getTestFile("target/output/symlinkarchive.tar")); archiver.addSymlink("link_to_test_destinaton", "../test_destination/"); archiver.createArchive(); TarArchiveInputStream tis; tis = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(archiver.getDestFile()))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { if (te.isDirectory()) { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0500, te.getMode() & UnixStat.PERM_MASK); } else if (te.isSymbolicLink()) { assertEquals("../test_destination/", te.getLinkName()); assertEquals("link_to_test_destinaton", te.getName()); assertEquals(0640, te.getMode() & UnixStat.PERM_MASK); } else { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0400, te.getMode() & UnixStat.PERM_MASK); } } tis.close(); }
From source file:org.codehaus.plexus.archiver.tar.TarUnArchiver.java
protected void execute(File sourceFile, File destDirectory) throws ArchiverException { TarArchiveInputStream tis = null;/*from w w w . ja va 2 s. com*/ try { getLogger().info("Expanding: " + sourceFile + " into " + destDirectory); TarFile tarFile = new TarFile(sourceFile); tis = new TarArchiveInputStream( decompress(compression, sourceFile, new BufferedInputStream(new FileInputStream(sourceFile)))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { TarResource fileInfo = new TarResource(tarFile, te); if (isSelected(te.getName(), fileInfo)) { final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null; extractFile(sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(), te.getMode() != 0 ? te.getMode() : null, symlinkDestination); } } getLogger().debug("expand complete"); } catch (IOException ioe) { throw new ArchiverException("Error while expanding " + sourceFile.getAbsolutePath(), ioe); } finally { IOUtil.close(tis); } }
From source file:org.culturegraph.mf.stream.source.TarReader.java
@Override public void process(final Reader reader) { TarArchiveInputStream tarInputStream = null; try {/*from w w w . j av a 2s . c o m*/ tarInputStream = new TarArchiveInputStream(new ReaderInputStream(reader)); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { final byte[] buffer = new byte[(int) entry.getSize()]; while ((tarInputStream.read(buffer)) > 0) { getReceiver().process(new StringReader(new String(buffer))); } } } tarInputStream.close(); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(tarInputStream); } }
From source file:org.dataconservancy.dcs.util.extraction.TarPackageExtractor.java
@Override public List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName) throws UnpackException { List<File> files = new ArrayList<>(); //use try-with-resources to make sure tar input stream is closed even in the event of an Exception try (TarArchiveInputStream tarInStream = TarArchiveInputStream.class .isAssignableFrom(packageInputStream.getClass()) ? (TarArchiveInputStream) packageInputStream : new TarArchiveInputStream(packageInputStream)) { TarArchiveEntry entry = tarInStream.getNextTarEntry(); //Get next tar entry returns null when there are no more entries while (entry != null) { //Directories are automatically handled by the base class so we can ignore them in this class. if (!entry.isDirectory()) { File entryFile = new File(packageDir, FilePathUtil.convertToPlatformSpecificSlash(entry.getName())); List<File> savedFiles = saveExtractedFile(entryFile, tarInStream); files.addAll(savedFiles); }/*from w ww.java2s .c om*/ entry = tarInStream.getNextTarEntry(); } tarInStream.close(); } catch (IOException e) { final String msg = "Error processing TarArchiveInputStream: " + e.getMessage(); log.error(msg, e); throw new UnpackException(msg, e); } return files; }
From source file:org.dataconservancy.ui.util.TarPackageExtractor.java
@Override protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName) throws UnpackException { final TarArchiveInputStream tarInStream; if (!TarArchiveInputStream.class.isAssignableFrom(packageInputStream.getClass())) { tarInStream = new TarArchiveInputStream(packageInputStream); } else {/* ww w.j a v a 2s. c o m*/ tarInStream = (TarArchiveInputStream) packageInputStream; } List<File> files = new ArrayList<File>(); try { TarArchiveEntry entry = tarInStream.getNextTarEntry(); //Get next tar entry returns null when there are no more entries while (entry != null) { //Directories are automatically handled by the base class so we can ignore them in this class. if (!entry.isDirectory()) { File entryFile = new File(packageDir, entry.getName()); if (entryFile != null) { List<File> savedFiles = saveExtractedFile(entryFile, tarInStream); files.addAll(savedFiles); } } entry = tarInStream.getNextTarEntry(); } tarInStream.close(); } catch (IOException e) { final String msg = "Error processing TarArchiveInputStream: " + e.getMessage(); log.error(msg, e); throw new UnpackException(msg, e); } return files; }
From source file:org.datavec.api.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to//from ww w .j a v a 2s . c om * @param dest the destination directory * @throws java.io.IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip") || file.endsWith(".jar")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } log.info("file unzip : " + newFile.getAbsoluteFile()); //create all non exists folders //else you will hit FileNotFoundException for compressed folder FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.flush(); fos.close(); zis.closeEntry(); ze = zis.getNextEntry(); } zis.close(); } else if (file.endsWith(".tar")) { BufferedInputStream in = new BufferedInputStream(fin); TarArchiveInputStream tarIn = new TarArchiveInputStream(in); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); ; IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } }
From source file:org.dcm4chee.storage.tar.TarContainerProvider.java
@Override public InputStream seekEntry(RetrieveContext ctx, String name, String entryName, InputStream in) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream(in); String checksumEntry = container.getChecksumEntry(); TarArchiveEntry nextEntry; while ((nextEntry = tar.getNextTarEntry()) != null) { String nextEntryName = nextEntry.getName(); if (nextEntry.isDirectory() || nextEntryName.equals(checksumEntry)) continue; if (nextEntryName.equals(entryName)) return tar; }/*from www. j a v a2s . c o m*/ throw new ObjectNotFoundException(ctx.getStorageSystem().getStorageSystemPath(), name, entryName); }
From source file:org.deeplearning4j.examples.multigpu.w2vsentiment.DataSetsBuilder.java
private static void extractTarGz(String filePath, String outputPath) throws IOException { int fileCount = 0; int dirCount = 0; System.out.print("Extracting files"); try (TarArchiveInputStream tais = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(filePath))))) { TarArchiveEntry entry; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) { //System.out.println("Extracting file: " + entry.getName()); //Create directories as required if (entry.isDirectory()) { new File(outputPath + entry.getName()).mkdirs(); dirCount++;/* ww w . java 2s. c om*/ } else { int count; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(outputPath + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = tais.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); fileCount++; } if (fileCount % 1000 == 0) System.out.print("."); } } System.out .println("\n" + fileCount + " files and " + dirCount + " directories extracted to: " + outputPath); }