List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getModTime
public Date getModTime()
From source file:lucee.commons.io.compress.CompressUtil.java
private static void extractTar(Resource tarFile, Resource targetDir) throws IOException { if (!targetDir.exists() || !targetDir.isDirectory()) throw new IOException(targetDir + " is not a existing directory"); if (!tarFile.exists()) throw new IOException(tarFile + " is not a existing file"); if (tarFile.isDirectory()) { Resource[] files = tarFile.listResources(new ExtensionResourceFilter("tar")); if (files == null) throw new IOException("directory " + tarFile + " is empty"); extract(FORMAT_TAR, files, targetDir); return;// ww w .ja v a2 s. com } // read the zip file and build a query from its contents TarArchiveInputStream tis = null; try { tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream())); TarArchiveEntry entry; int mode; while ((entry = tis.getNextTarEntry()) != null) { //print.ln(entry); Resource target = targetDir.getRealResource(entry.getName()); if (entry.isDirectory()) { target.mkdirs(); } else { Resource parent = target.getParentResource(); if (!parent.exists()) parent.mkdirs(); IOUtil.copy(tis, target, false); } target.setLastModified(entry.getModTime().getTime()); mode = entry.getMode(); if (mode > 0) target.setMode(mode); //tis.closeEntry() ; } } finally { IOUtil.closeEL(tis); } }
From source file:cpcc.vvrte.services.TarArchiveDemo.java
@Test public void shouldWriteTarFile() throws IOException, ArchiveException { byte[] c1 = "content1\n".getBytes("UTF-8"); byte[] c2 = "content2 text\n".getBytes("UTF-8"); Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L)); Date t2 = new Date(t1.getTime() - 30000); FileOutputStream fos = new FileOutputStream("bugger1.tar"); ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8"); ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos); TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1"); archiveEntry1.setModTime(t1);/* w w w . j av a2 s. c om*/ archiveEntry1.setSize(c1.length); archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE); archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE); outStream.putArchiveEntry(archiveEntry1); outStream.write(c1); outStream.closeArchiveEntry(); TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2"); archiveEntry2.setModTime(t2); archiveEntry2.setSize(c2.length); archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO); archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO); outStream.putArchiveEntry(archiveEntry2); outStream.write(c2); outStream.closeArchiveEntry(); outStream.close(); FileInputStream fis = new FileInputStream("bugger1.tar"); ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis); TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry(); assertThat(entry1.getModTime()).isEqualTo(t1); assertThat(entry1.getSize()).isEqualTo(c1.length); assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE); assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE); assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE); assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE); ByteArrayOutputStream b1 = new ByteArrayOutputStream(); IOUtils.copy(inStream, b1); b1.close(); assertThat(b1.toByteArray().length).isEqualTo(c1.length); assertThat(b1.toByteArray()).isEqualTo(c1); TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry(); assertThat(entry2.getModTime()).isEqualTo(t2); assertThat(entry2.getSize()).isEqualTo(c2.length); assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO); assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO); assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO); assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO); ByteArrayOutputStream b2 = new ByteArrayOutputStream(); IOUtils.copy(inStream, b2); b2.close(); assertThat(b2.toByteArray().length).isEqualTo(c2.length); assertThat(b2.toByteArray()).isEqualTo(c2); TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry(); assertThat(entry3).isNull(); inStream.close(); }
From source file:com.facebook.buck.util.unarchive.Untar.java
/** Sets the modification time and the execution bit on a file */ private void setAttributes(ProjectFilesystem filesystem, Path path, TarArchiveEntry entry) throws IOException { Path filePath = filesystem.getRootPath().resolve(path); File file = filePath.toFile(); file.setLastModified(entry.getModTime().getTime()); Set<PosixFilePermission> posixPermissions = MorePosixFilePermissions.fromMode(entry.getMode()); if (posixPermissions.contains(PosixFilePermission.OWNER_EXECUTE)) { // setting posix file permissions on a symlink does not work, so use File API instead if (entry.isSymbolicLink()) { file.setExecutable(true, true); } else {/*from w ww.j a v a 2 s. c o m*/ MostFiles.makeExecutable(filePath); } } }
From source file:de.uzk.hki.da.pkg.TarGZArchiveBuilder.java
public void unarchiveFolder(File srcTar, File destFolder) throws Exception { FileInputStream fin = new FileInputStream(srcTar); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tIn = new TarArchiveInputStream(gzIn); HashMap<String, Long> modDateMap = new HashMap<String, Long>(); TarArchiveEntry entry; do {//from w w w . j a v a 2s . c om entry = tIn.getNextTarEntry(); if (entry == null) break; logger.debug(entry.getName()); String dstName = destFolder.getAbsolutePath() + "/" + entry.getName(); File entryFile = new File(dstName); if (entry.isDirectory()) { entryFile.mkdirs(); modDateMap.put(dstName, new Long(entry.getModTime().getTime())); } else { new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/'))) .mkdirs(); FileOutputStream out = new FileOutputStream(entryFile); IOUtils.copy(tIn, out); out.close(); entryFile.setLastModified(entry.getModTime().getTime()); } } while (true); tIn.close(); gzIn.close(); in.close(); fin.close(); for (Map.Entry<String, Long> moddi : modDateMap.entrySet()) { String key = moddi.getKey(); Long value = moddi.getValue(); (new File(key)).setLastModified(value); } }
From source file:de.uzk.hki.da.pkg.NativeJavaTarArchiveBuilder.java
public void unarchiveFolder(File srcTar, File destFolder) throws Exception { FileInputStream fin = new FileInputStream(srcTar); BufferedInputStream in = new BufferedInputStream(fin); TarArchiveInputStream tIn = new TarArchiveInputStream(in); HashMap<String, Long> modDateMap = new HashMap<String, Long>(); TarArchiveEntry entry; do {/*from w w w.j a v a 2s .com*/ entry = tIn.getNextTarEntry(); if (entry == null) break; logger.debug(entry.getName()); String dstName = destFolder.getAbsolutePath() + "/" + entry.getName(); File entryFile = new File(dstName); if (entry.isDirectory()) { entryFile.mkdirs(); modDateMap.put(dstName, new Long(entry.getModTime().getTime())); } else { new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/'))) .mkdirs(); FileOutputStream out = new FileOutputStream(entryFile); IOUtils.copy(tIn, out); out.close(); entryFile.setLastModified(entry.getModTime().getTime()); } } while (true); tIn.close(); in.close(); fin.close(); for (Map.Entry<String, Long> moddi : modDateMap.entrySet()) { String key = moddi.getKey(); Long value = moddi.getValue(); (new File(key)).setLastModified(value); } }
From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java
/** * @param inStream the input stream containing a virtual vehicle chunk. * @param entry the previously read archive entry. * @param virtualVehicleHolder the holder object for the virtual vehicle read. * @throws IOException thrown in case of errors. *///from w ww . j a v a 2 s .c o m private void storeStorageEntry(InputStream inStream, TarArchiveEntry entry, VirtualVehicle virtualVehicle) throws IOException { String name = entry.getName().substring(8, entry.getName().length()); VirtualVehicleStorage item = vvRepository.findStorageItemByVirtualVehicleAndName(virtualVehicle, name); if (item == null) { item = new VirtualVehicleStorage(); item.setName(name); item.setVirtualVehicle(virtualVehicle); } item.setModificationTime(entry.getModTime()); item.setContentAsByteArray(IOUtils.toByteArray(inStream)); sessionManager.getSession().saveOrUpdate(item); }
From source file:com.facebook.buck.util.unarchive.Untar.java
@VisibleForTesting ImmutableSet<Path> extractArchive(Path archiveFile, ProjectFilesystem filesystem, Path filesystemRelativePath, Optional<Path> stripPath, ExistingFileMode existingFileMode, PatternsMatcher entriesToExclude, boolean writeSymlinksAfterCreatingFiles) throws IOException { ImmutableSet.Builder<Path> paths = ImmutableSet.builder(); HashSet<Path> dirsToTidy = new HashSet<>(); TreeMap<Path, Long> dirCreationTimes = new TreeMap<>(); DirectoryCreator creator = new DirectoryCreator(filesystem); // On windows, we create hard links instead of symlinks. This is fine, but the // destination file may not exist yet, which is an error. So, just hold onto the paths until // all files are extracted, and /then/ try to do the links Map<Path, Path> windowsSymlinkMap = new HashMap<>(); try (TarArchiveInputStream archiveStream = getArchiveInputStream(archiveFile)) { TarArchiveEntry entry; while ((entry = archiveStream.getNextTarEntry()) != null) { String entryName = entry.getName(); if (entriesToExclude.matchesAny(entryName)) { continue; }//w w w.j a v a 2 s .c o m Path destFile = Paths.get(entryName); Path destPath; if (stripPath.isPresent()) { if (!destFile.startsWith(stripPath.get())) { continue; } destPath = filesystemRelativePath.resolve(stripPath.get().relativize(destFile)).normalize(); } else { destPath = filesystemRelativePath.resolve(destFile).normalize(); } if (entry.isDirectory()) { dirsToTidy.add(destPath); mkdirs(creator, destPath); dirCreationTimes.put(destPath, entry.getModTime().getTime()); } else if (entry.isSymbolicLink()) { if (writeSymlinksAfterCreatingFiles) { recordSymbolicLinkForWindows(creator, destPath, entry, windowsSymlinkMap); } else { writeSymbolicLink(creator, destPath, entry); } paths.add(destPath); setAttributes(filesystem, destPath, entry); } else if (entry.isFile()) { writeFile(creator, archiveStream, destPath); paths.add(destPath); setAttributes(filesystem, destPath, entry); } } writeWindowsSymlinks(creator, windowsSymlinkMap); } catch (CompressorException e) { throw new IOException(String.format("Could not get decompressor for archive at %s", archiveFile), e); } setDirectoryModificationTimes(filesystem, dirCreationTimes); ImmutableSet<Path> filePaths = paths.build(); if (existingFileMode == ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES) { // Clean out directories of files that were not in the archive tidyDirectories(filesystem, dirsToTidy, filePaths); } return filePaths; }
From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java
/** * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the * <i>skipTopFolder</i> is set, then don't assume that the archive contains one * single folder and unpack the content of that folder, not including the folder * itself.//from w w w . j a v a2 s . c o m * * @param source * The input source. Must be in <i>TAR</i> format. * @param targetFolder * The destination folder for the unpack. Not used when a <tt>fileCatcher</tt> is provided * @param skipTopFolder * Set to <code>true</code> to unpack beneath the top folder * of the archive. The archive must consist of one single folder and nothing else * in order for this to work. * @param fileCatcher * Used when specific files should be picked from the archive without writing them to disk. Can be * <tt>null</tt>. * @throws IOException */ public static void unpack(InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher) throws IOException { String topFolderName = null; Map<File, Map<Integer, List<String>>> chmodMap = new HashMap<File, Map<Integer, List<String>>>(); TarArchiveInputStream in = new TarArchiveInputStream(source); try { TarArchiveEntry te = in.getNextTarEntry(); if (te == null) { throw new IOException("No entry in the tar file"); } do { if (te.isGlobalPaxHeader()) continue; String name = te.getName(); if (skipTopFolder) { int firstSlash = name.indexOf('/'); if (firstSlash < 0) throw new IOException("Archive doesn't contain one single folder"); String tfName = name.substring(0, firstSlash); if (topFolderName == null) topFolderName = tfName; else if (!tfName.equals(topFolderName)) throw new IOException("Archive doesn't contain one single folder"); name = name.substring(firstSlash + 1); } if (name.length() == 0) continue; String linkName = te.getLinkName(); if (linkName != null) { if (linkName.trim().equals("")) linkName = null; } if (fileCatcher != null) { if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) { if (fileCatcher.catchData(name, in)) // We're done here return; } continue; } File outFile = new File(targetFolder, name); if (linkName != null) { if (!OsUtil.link(targetFolder, name, te.getLinkName())) throw new IOException("Archive contains links but they are not supported on this platform"); } else { if (te.isDirectory()) { outFile.mkdirs(); } else { outFile.getParentFile().mkdirs(); OutputStream target = new FileOutputStream(outFile); StreamUtil.copy(in, target); target.close(); outFile.setLastModified(te.getModTime().getTime()); } registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name); } } while ((te = in.getNextTarEntry()) != null); } finally { StreamUtil.close(in); } chmod(chmodMap); }
From source file:org.apache.hadoop.fs.tar.TarFileSystem.java
@Override public FileStatus[] listStatus(Path f) throws IOException { ArrayList<FileStatus> ret = new ArrayList<FileStatus>(); Path abs = makeAbsolute(f);/*w w w .j a v a 2s .co m*/ Path baseTar = getBaseTarPath(abs); String inFile = getFileInArchive(abs); FileStatus underlying = underlyingFS.getFileStatus(baseTar); // if subfile exists in the path, just return the status of that if (inFile != null) { ret.add(getFileStatus(abs)); } else { FSDataInputStream in = underlyingFS.open(baseTar); byte[] buffer = new byte[512]; for (long offset : index.getOffsetList()) { in.seek(offset - 512); // adjust for the header TarArchiveEntry entry = readHeaderEntry(in, buffer); // Construct a FileStatus object FileStatus fstatus = new FileStatus(entry.getSize(), entry.isDirectory(), (int) underlying.getReplication(), underlying.getBlockSize(), entry.getModTime().getTime(), underlying.getAccessTime(), new FsPermission((short) entry.getMode()), entry.getUserName(), entry.getGroupName(), new Path(abs.toUri().toASCIIString() + TAR_INFILESEP + entry.getName())); ret.add(fstatus); } } // copy back FileStatus[] retArray = new FileStatus[ret.size()]; ret.toArray(retArray); return retArray; }
From source file:org.apache.hadoop.fs.tar.TarFileSystem.java
@Override public FileStatus getFileStatus(Path f) throws IOException { FileStatus fstatus = null;/*from ww w . j av a 2s . c om*/ Path abs = makeAbsolute(f); Path baseTar = getBaseTarPath(abs); String inFile = getFileInArchive(abs); FileStatus underlying = underlyingFS.getFileStatus(baseTar); if (inFile == null) { // return the status of the tar itself but make it a dir fstatus = new FileStatus(underlying.getLen(), true, underlying.getReplication(), underlying.getBlockSize(), underlying.getModificationTime(), underlying.getAccessTime(), underlying.getPermission(), underlying.getOwner(), underlying.getGroup(), abs); } else { long offset = index.getOffset(inFile); FSDataInputStream in = underlyingFS.open(baseTar); in.seek(offset - 512); TarArchiveEntry entry = readHeaderEntry(in); if (!entry.getName().equals(inFile)) { LOG.fatal("Index file is corrupt." + "Requested filename is present in index " + "but absent in TAR."); throw new IOException("NBU-TAR: FATAL: entry file name " + "does not match requested file name"); } // Construct a FileStatus object fstatus = new FileStatus(entry.getSize(), entry.isDirectory(), (int) underlying.getReplication(), underlying.getBlockSize(), entry.getModTime().getTime(), underlying.getAccessTime(), new FsPermission((short) entry.getMode()), entry.getUserName(), entry.getGroupName(), abs); } return fstatus; }