Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveEntry#setModTime()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveEntry#setModTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DebianPackageWriter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private static void applyInfo ( final TarArchiveEntry entry, final EntryInformation entryInformation, TimestampProvider timestampProvider )
{
    if ( entryInformation == null )
    {
        return;
    }

    if ( entryInformation.getUser () != null )
    {
        entry.setUserName ( entryInformation.getUser () );
    }
    if ( entryInformation.getGroup () != null )
    {
        entry.setGroupName ( entryInformation.getGroup () );
    }
    entry.setMode ( entryInformation.getMode () );
    entry.setModTime ( timestampProvider.getModTime () );
}
 
Example 2
Source File: DebianPackageWriter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode ) throws IOException
{
    if ( content == null || !content.hasContent () )
    {
        return;
    }

    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 ();
}
 
Example 3
Source File: TarGzipPacker.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param zipFile the archive file to be copied to the new archive
 * @param dirPrefixForTar sub path inside the archive
 */
public boolean addZipToArchive(String zipFile, String dirPrefixForTar) {
  try {
    // construct input stream
    ZipFile zipFileObj = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> entries = zipFileObj.entries();

    // copy the existing entries from source gzip file
    while (entries.hasMoreElements()) {
      ZipEntry nextEntry = entries.nextElement();
      TarArchiveEntry entry = new TarArchiveEntry(dirPrefixForTar + nextEntry.getName());
      entry.setSize(nextEntry.getSize());
      entry.setModTime(nextEntry.getTime());

      tarOutputStream.putArchiveEntry(entry);
      IOUtils.copy(zipFileObj.getInputStream(nextEntry), tarOutputStream);
      tarOutputStream.closeArchiveEntry();
    }

    zipFileObj.close();
    return true;
  } catch (IOException ioe) {
    LOG.log(Level.SEVERE, "Archive File can not be added: " + zipFile, ioe);
    return false;
  }
}
 
Example 4
Source File: DebianPackageWriter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode, final Supplier<Instant> timestampSupplier ) throws IOException
{
    if ( content == null || !content.hasContent () )
    {
        return;
    }

    final TarArchiveEntry entry = new TarArchiveEntry ( name );
    if ( mode >= 0 )
    {
        entry.setMode ( mode );
    }

    entry.setUserName ( "root" );
    entry.setGroupName ( "root" );
    entry.setSize ( content.getSize () );
    entry.setModTime ( timestampSupplier.get ().toEpochMilli () );
    out.putArchiveEntry ( entry );
    try ( InputStream stream = content.createInputStream () )
    {
        IOUtils.copy ( stream, out );
    }
    out.closeArchiveEntry ();
}
 
Example 5
Source File: TarUtils.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private static void addFileEntry(
    TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException {
  final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
  if (modTime >= 0) {
    tarEntry.setModTime(modTime);
  }
  tarOut.putArchiveEntry(tarEntry);
  try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
    final byte[] buf = new byte[BUF_SIZE];
    int r;
    while ((r = in.read(buf)) != -1) {
      tarOut.write(buf, 0, r);
    }
  }
  tarOut.closeArchiveEntry();
}
 
Example 6
Source File: ArchiveWorkItemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager manager) {
    String archive = (String) workItem.getParameter("Archive");
    List<File> files = (List<File>) workItem.getParameter("Files");

    try {
        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        OutputStream outputStream = new FileOutputStream(new File(archive));
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
                                                                                      outputStream);

        if (files != null) {
            for (File file : files) {
                final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
                entry.setModTime(0);
                entry.setSize(file.length());
                entry.setUserId(0);
                entry.setGroupId(0);
                entry.setMode(0100000);
                os.putArchiveEntry(entry);
                IOUtils.copy(new FileInputStream(file),
                             os);
            }
        }
        os.closeArchiveEntry();
        os.close();
        manager.completeWorkItem(workItem.getId(),
                                 null);
    } catch (Exception e) {
        handleException(e);
        manager.abortWorkItem(workItem.getId());
    }
}
 
Example 7
Source File: MCRTarServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendCompressedDirectory(MCRPath file, BasicFileAttributes attrs,
    TarArchiveOutputStream container) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(getFilename(file), TarConstants.LF_DIR);
    entry.setModTime(attrs.lastModifiedTime().toMillis());
    container.putArchiveEntry(entry);
    container.closeArchiveEntry();
}
 
Example 8
Source File: MCRTarServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs,
    TarArchiveOutputStream container) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(getFilename(file));
    entry.setModTime(attrs.lastModifiedTime().toMillis());
    entry.setSize(attrs.size());
    container.putArchiveEntry(entry);
    try {
        Files.copy(file, container);
    } finally {
        container.closeArchiveEntry();
    }
}
 
Example 9
Source File: MCRTarServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified,
    TarArchiveOutputStream container) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(fileName);
    entry.setModTime(lastModified);
    entry.setSize(content.length);
    container.putArchiveEntry(entry);
    container.write(content);
    container.closeArchiveEntry();
}
 
Example 10
Source File: TarUtils.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static void addDirectoryEntry(
    TarArchiveOutputStream tarOut, String entryName, File directory, long modTime)
    throws IOException {
  final TarArchiveEntry tarEntry = new TarArchiveEntry(directory, entryName);
  if (modTime >= 0) {
    tarEntry.setModTime(modTime);
  }
  tarOut.putArchiveEntry(tarEntry);
  tarOut.closeArchiveEntry();
}
 
Example 11
Source File: TarStripper.java    From reproducible-build-maven-plugin with Apache License 2.0 5 votes vote down vote up
private TarArchiveEntry filterTarEntry(TarArchiveEntry entry)
{
    entry.setModTime(tarTime);
    entry.setGroupId(0);
    entry.setUserId(0);
    entry.setUserName("");
    entry.setGroupName("");
    return entry;
}
 
Example 12
Source File: StreamUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a directory entry to a given
 * {@link TarArchiveOutputStream}.
 */
private static void dirToTarArchiveOutputStream(Path destDir, TarArchiveOutputStream tarArchiveOutputStream)
    throws IOException {
  TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToDir(destDir));
  tarArchiveEntry.setModTime(System.currentTimeMillis());
  tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);
  tarArchiveOutputStream.closeArchiveEntry();
}
 
Example 13
Source File: StreamUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a file entry to a given
 * {@link TarArchiveOutputStream} and copies the contents of the file to the new entry.
 */
private static void fileToTarArchiveOutputStream(FileStatus fileStatus, FSDataInputStream fsDataInputStream,
    Path destFile, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
  TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToFile(destFile));
  tarArchiveEntry.setSize(fileStatus.getLen());
  tarArchiveEntry.setModTime(System.currentTimeMillis());
  tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);

  try {
    IOUtils.copy(fsDataInputStream, tarArchiveOutputStream);
  } finally {
    tarArchiveOutputStream.closeArchiveEntry();
  }
}
 
Example 14
Source File: ArtifactUploader.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting
static long compress(
    ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
    throws IOException {
  long fullSize = 0L;
  try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
      OutputStream z = new ZstdCompressorOutputStream(o);
      TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
    archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    for (Path path : pathsToIncludeInArchive) {
      boolean isRegularFile = !projectFilesystem.isDirectory(path);

      // Add a file entry.
      TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
      int mode = (int) projectFilesystem.getPosixFileMode(path);
      // If permissions don't allow for owner to r or w, update to u+=rw and g+=r
      e.setMode((mode & 384) == 0 ? (mode | 416) : mode);
      e.setModTime((long) ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP * 1000);

      if (isRegularFile) {
        long pathSize = projectFilesystem.getFileSize(path);
        e.setSize(pathSize);
        fullSize += pathSize;
        archive.putArchiveEntry(e);
        try (InputStream input = projectFilesystem.newFileInputStream(path)) {
          ByteStreams.copy(input, archive);
        }
      } else {
        archive.putArchiveEntry(e);
      }
      archive.closeArchiveEntry();
    }
    archive.finish();
  }

  return fullSize;
}
 
Example 15
Source File: TestTarImportCommand.java    From kite with Apache License 2.0 5 votes vote down vote up
private static void writeToTarFile(TarArchiveOutputStream tos, String name,
                                   String content) throws IOException {
  TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name);
  if (null != content) {
    tarArchiveEntry.setSize(content.length());
  }
  tarArchiveEntry.setModTime(System.currentTimeMillis());
  tos.putArchiveEntry(tarArchiveEntry);
  if (null != content) {
    byte[] buf = content.getBytes();
    tos.write(buf, 0, content.length());
    tos.flush();
  }
  tos.closeArchiveEntry();
}
 
Example 16
Source File: BuildImageStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 4 votes vote down vote up
public void visit(File file, String relativePath) throws IOException {
    if (recursiveMatch(dockerIgnore, file.toPath())) {
         return;
    }

    if (relativePath.contains("/")) {
        relativePath = relativePath.substring(relativePath.indexOf("/") + 1);
    } else {
        relativePath = ".";
    }

    if(Functions.isWindows()) {
        relativePath = relativePath.replace('\\', '/');
    }

    if(file.isDirectory()) {
        relativePath += '/';
    }

    TarArchiveEntry te = new TarArchiveEntry(file);
    te.setName(relativePath);

    int mode = IOUtils.mode(file);
    if (mode!=-1) {
        te.setMode(mode);
    }
    te.setModTime(file.lastModified());

    if(!file.isDirectory()) {
        te.setSize(file.length());
    }

    tar.putArchiveEntry(te);

    if (!file.isDirectory()) {
        FileInputStream in = new FileInputStream(file);
        try {
            int len;
            while((len=in.read(buf))>=0)
                tar.write(buf,0,len);
        } finally {
            in.close();
        }
    }

    tar.closeArchiveEntry();
    entriesWritten++;
}