List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry setModTime
public void setModTime(Date time)
From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java
/** * tar a file/*ww w . ja v a2 s . c om*/ * * @param entry the file to tar * @param tOut the output stream * @param vPath the path name of the file to tar * @throws IOException on error */ protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath) throws ArchiverException, IOException { // don't add "" to the archive if (vPath.length() <= 0) { return; } if (entry.getResource().isDirectory() && !vPath.endsWith("/")) { vPath += "/"; } if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) { int l = vPath.length(); if (l <= 1) { // we would end up adding "" to the archive return; } vPath = vPath.substring(1, l); } int pathLength = vPath.length(); InputStream fIn = null; try { TarArchiveEntry te; if (!longFileMode.isGnuMode() && pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) { int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN; if (longFileMode.isPosixMode()) { } else if (longFileMode.isPosixWarnMode()) { if (pathLength > maxPosixPathLen) { getLogger().warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters."); if (!longWarningGiven) { getLogger().warn("Resulting tar file can only be processed " + "successfully by GNU compatible tar commands"); longWarningGiven = true; } } } else if (longFileMode.isOmitMode()) { getLogger().info("Omitting: " + vPath); return; } else if (longFileMode.isWarnMode()) { getLogger().warn("Entry: " + vPath + " longer than " + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters."); if (!longWarningGiven) { getLogger().warn("Resulting tar file can only be processed " + "successfully by GNU compatible tar commands"); longWarningGiven = true; } } else if (longFileMode.isFailMode()) { throw new ArchiverException("Entry: " + vPath + " longer than " + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters."); } else { throw new IllegalStateException("Non gnu mode should never get here?"); } } if (entry.getType() == ArchiveEntry.SYMLINK) { final SymlinkDestinationSupplier plexusIoSymlinkResource = (SymlinkDestinationSupplier) entry .getResource(); te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK); te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination()); } else { te = new TarArchiveEntry(vPath); } long teLastModified = entry.getResource().getLastModified(); te.setModTime(teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE ? System.currentTimeMillis() : teLastModified); if (entry.getType() == ArchiveEntry.SYMLINK) { te.setSize(0); } else if (!entry.getResource().isDirectory()) { final long size = entry.getResource().getSize(); te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size); } te.setMode(entry.getMode()); PlexusIoResourceAttributes attributes = entry.getResourceAttributes(); te.setUserName((attributes != null && attributes.getUserName() != null) ? attributes.getUserName() : options.getUserName()); te.setGroupName((attributes != null && attributes.getGroupName() != null) ? attributes.getGroupName() : options.getGroup()); final int userId = (attributes != null && attributes.getUserId() != null) ? attributes.getUserId() : options.getUid(); if (userId >= 0) { te.setUserId(userId); } final int groupId = (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId() : options.getGid(); if (groupId >= 0) { te.setGroupId(groupId); } tOut.putArchiveEntry(te); try { if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) { fIn = entry.getInputStream(); Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR"); } } catch (Throwable e) { getLogger().warn("When creating tar entry", e); } finally { tOut.closeArchiveEntry(); } } finally { IOUtil.close(fIn); } }
From source file:org.dcm4chee.storage.tar.TarContainerProvider.java
@Override public void writeEntriesTo(StorageContext context, List<ContainerEntry> entries, OutputStream out) throws IOException { TarArchiveOutputStream tar = new TarArchiveOutputStream(out); String checksumEntry = container.getChecksumEntry(); if (checksumEntry != null) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ContainerEntry.writeChecksumsTo(entries, bout); TarArchiveEntry tarEntry = new TarArchiveEntry(checksumEntry); tarEntry.setSize(bout.size());/* w ww. jav a 2 s .co m*/ tar.putArchiveEntry(tarEntry); tar.write(bout.toByteArray()); tar.closeArchiveEntry(); } for (ContainerEntry entry : entries) { Path path = entry.getSourcePath(); TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName()); tarEntry.setModTime(Files.getLastModifiedTime(path).toMillis()); tarEntry.setSize(Files.size(path)); tar.putArchiveEntry(tarEntry); Files.copy(path, tar); tar.closeArchiveEntry(); } tar.finish(); }
From source file:org.dspace.pack.bagit.Bag.java
private void fillArchive(File dirFile, String relBase, ArchiveOutputStream out) throws IOException { for (File file : dirFile.listFiles()) { String relPath = relBase + File.separator + file.getName(); if (file.isDirectory()) { fillArchive(file, relPath, out); } else {//from ww w .j av a 2 s . com TarArchiveEntry entry = new TarArchiveEntry(relPath); entry.setSize(file.length()); entry.setModTime(0L); out.putArchiveEntry(entry); FileInputStream fin = new FileInputStream(file); Utils.copy(fin, out); out.closeArchiveEntry(); fin.close(); } } }
From source file:org.eclipse.che.api.vfs.TarArchiver.java
private void addTarEntry(VirtualFile virtualFile, TarArchiveOutputStream tarOutputStream) throws ServerException { try {/*w w w. j a va2 s. c om*/ TarArchiveEntry tarEntry = new TarArchiveEntry(getTarEntryName(virtualFile)); if (virtualFile.isFolder()) { tarEntry.setModTime(0); tarOutputStream.putArchiveEntry(tarEntry); } else { tarEntry.setSize(virtualFile.getLength()); tarEntry.setModTime(virtualFile.getLastModificationDate()); tarOutputStream.putArchiveEntry(tarEntry); try (InputStream content = virtualFile.getContent()) { ByteStreams.copy(content, tarOutputStream); } } tarOutputStream.closeArchiveEntry(); } catch (ForbiddenException e) { throw new ServerException(e.getServiceError()); } catch (IOException e) { throw new ServerException(e.getMessage(), e); } }
From source file:org.eclipse.scada.utils.pkg.deb.DebianPackageWriter.java
private static void applyInfo(final TarArchiveEntry entry, final EntryInformation entryInformation, TimestampProvider timestampProvider) { if (entryInformation == null) { return;//w w w. jav a2s . c om } if (entryInformation.getUser() != null) { entry.setUserName(entryInformation.getUser()); } if (entryInformation.getGroup() != null) { entry.setGroupName(entryInformation.getGroup()); } entry.setMode(entryInformation.getMode()); entry.setModTime(timestampProvider.getModTime()); }
From source file:org.eclipse.scada.utils.pkg.deb.DebianPackageWriter.java
private void addControlContent(final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode) throws IOException { if (content == null || !content.hasContent()) { return;//from w ww .j a v a2s . co m } final TarArchiveEntry entry = new TarArchiveEntry(name); if (mode >= 0) { entry.setMode(mode); } entry.setUserName("root"); entry.setGroupName("root"); entry.setSize(content.getSize()); entry.setModTime(this.getTimestampProvider().getModTime()); out.putArchiveEntry(entry); try (InputStream stream = content.createInputStream()) { ByteStreams.copy(stream, out); } out.closeArchiveEntry(); }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java
private TarArchiveEntry createTarEntry(File tarRootDir, File source) throws IOException { String pathInTar = slashify(tarRootDir.toPath().relativize(source.toPath())); log.debug("Adding entry " + pathInTar); TarArchiveEntry tarEntry; if (isSymbolicLink(source) && resolvesBelow(source, tarRootDir)) { // only create symlink entry if link target is inside archive tarEntry = new TarArchiveEntry(pathInTar, TarArchiveEntry.LF_SYMLINK); tarEntry.setLinkName(slashify(getRelativeSymLinkTarget(source, source.getParentFile()))); } else {// w ww. j av a2 s .c om tarEntry = new TarArchiveEntry(source, pathInTar); } PosixFileAttributes attrs = getAttributes(source); if (attrs != null) { tarEntry.setUserName(attrs.owner().getName()); tarEntry.setGroupName(attrs.group().getName()); tarEntry.setMode(FilePermissionHelper.toOctalFileMode(attrs.permissions())); } tarEntry.setModTime(source.lastModified()); return tarEntry; }
From source file:org.fabrician.maven.plugins.CompressUtils.java
private static ArchiveEntry createArchiveEntry(ArchiveEntry entry, OutputStream out, String alternateBaseDir) throws IOException { String substitutedName = substituteAlternateBaseDir(entry, alternateBaseDir); if (out instanceof TarArchiveOutputStream) { TarArchiveEntry newEntry = new TarArchiveEntry(substitutedName); newEntry.setSize(entry.getSize()); newEntry.setModTime(entry.getLastModifiedDate()); if (entry instanceof TarArchiveEntry) { TarArchiveEntry old = (TarArchiveEntry) entry; newEntry.setSize(old.getSize()); newEntry.setIds(old.getUserId(), old.getGroupId()); newEntry.setNames(old.getUserName(), old.getGroupName()); }/*from ww w . ja va 2 s . c om*/ return newEntry; } else if (entry instanceof ZipArchiveEntry) { ZipArchiveEntry old = (ZipArchiveEntry) entry; ZipArchiveEntry zip = new ZipArchiveEntry(substitutedName); zip.setInternalAttributes(old.getInternalAttributes()); zip.setExternalAttributes(old.getExternalAttributes()); zip.setExtraFields(old.getExtraFields(true)); return zip; } else { return new ZipArchiveEntry(substitutedName); } }
From source file:org.haiku.haikudepotserver.pkg.job.AbstractPkgResourceExportArchiveJobRunner.java
/** * <p>Adds a little informational file into the tar-ball.</p> *///from ww w . j ava 2 s . com private void appendArchiveInfo(State state) throws IOException { ArchiveInfo archiveInfo = new ArchiveInfo( DateTimeHelper.secondAccuracyDatePlusOneSecond(state.latestModifiedTimestamp), runtimeInformationService.getProjectVersion()); byte[] payload = objectMapper.writeValueAsBytes(archiveInfo); TarArchiveEntry tarEntry = new TarArchiveEntry(getPathComponentTop() + "/info.json"); tarEntry.setSize(payload.length); tarEntry.setModTime(roundTimeToSecondPlusOne(state.latestModifiedTimestamp)); state.tarArchiveOutputStream.putArchiveEntry(tarEntry); state.tarArchiveOutputStream.write(payload); state.tarArchiveOutputStream.closeArchiveEntry(); }
From source file:org.haiku.haikudepotserver.pkg.job.PkgIconExportArchiveJobRunner.java
private void append(State state, String pkgName, Number size, String mediaTypeCode, byte[] payload, Date modifyTimestamp) throws IOException { String filename = String.join("/", PATH_COMPONENT_TOP, pkgName, PkgIcon.deriveFilename(mediaTypeCode, null == size ? null : size.intValue())); TarArchiveEntry tarEntry = new TarArchiveEntry(filename); tarEntry.setSize(payload.length);/*w w w. j a va2 s. c o m*/ tarEntry.setModTime(roundTimeToSecond(modifyTimestamp)); state.tarArchiveOutputStream.putArchiveEntry(tarEntry); state.tarArchiveOutputStream.write(payload); state.tarArchiveOutputStream.closeArchiveEntry(); if (modifyTimestamp.after(state.latestModifiedTimestamp)) { state.latestModifiedTimestamp = modifyTimestamp; } }