Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry setModTime

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry setModTime

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry setModTime.

Prototype

public void setModTime(Date time) 

Source Link

Document

Set this entry's modification time.

Usage

From source file:com.codenvy.commons.lang.TarUtils.java

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);
    }// ww  w .  j a  v  a2  s .  c o  m
    tarOut.putArchiveEntry(tarEntry);
    tarOut.closeArchiveEntry();
}

From source file:com.codenvy.commons.lang.TarUtils.java

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);
    }//w w w .j av a2  s  .  com
    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();
}

From source file:gobblin.util.io.StreamUtils.java

/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a directory entry to a given
 * {@link TarArchiveOutputStream}./*from  w w  w .  j  ava2 s  . c o m*/
 */
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();
}

From source file:jetbrains.exodus.util.CompressBackupUtil.java

/**
 * Adds the file to the tar archive represented by output stream. It's caller's responsibility to close output stream
 * properly./*from w  ww. j  a  va  2s .  c  o m*/
 *
 * @param out           target archive.
 * @param pathInArchive relative path in archive. It will lead the name of the file in the archive.
 * @param source        file to be added.
 * @param fileSize      size of the file (which is known in most cases).
 * @throws IOException in case of any issues with underlying store.
 */
public static void archiveFile(@NotNull final ArchiveOutputStream out, @NotNull final String pathInArchive,
        @NotNull final File source, final long fileSize) throws IOException {
    if (!source.isFile()) {
        throw new IllegalArgumentException("Provided source is not a file: " + source.getAbsolutePath());
    }
    //noinspection ChainOfInstanceofChecks
    if (out instanceof TarArchiveOutputStream) {
        final TarArchiveEntry entry = new TarArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setModTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else if (out instanceof ZipArchiveOutputStream) {
        final ZipArchiveEntry entry = new ZipArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else {
        throw new IOException("Unknown archive output stream");
    }
    try (InputStream input = new FileInputStream(source)) {
        IOUtil.copyStreams(input, fileSize, out, IOUtil.BUFFER_ALLOCATOR);
    }
    out.closeArchiveEntry();
}

From source file:com.facebook.buck.artifact_cache.ArtifactUploader.java

/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting//w w w.j  a va2  s.c  o m
static void compress(ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
        throws IOException {
    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 ? "" : "/"));
            e.setMode((int) projectFilesystem.getPosixFileMode(path));
            e.setModTime(ZipConstants.getFakeTime());

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

From source file:gobblin.util.io.StreamUtils.java

/**
 * 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.
 *//*from   www . j a  va  2 s  .  c o m*/
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();
    }
}

From source file:com.gitblit.utils.CompressionUtils.java

/**
 * Compresses/archives the contents of the tree at the (optionally)
 * specified revision and the (optionally) specified basepath to the
 * supplied outputstream.//w w  w  . ja  va 2s  .c o m
 * 
 * @param algorithm
 *            compression algorithm for tar (optional)
 * @param repository
 * @param basePath
 *            if unspecified, entire repository is assumed.
 * @param objectId
 *            if unspecified, HEAD is assumed.
 * @param os
 * @return true if repository was successfully zipped to supplied output
 *         stream
 */
private static boolean tar(String algorithm, Repository repository, String basePath, String objectId,
        OutputStream os) {
    RevCommit commit = JGitUtils.getCommit(repository, objectId);
    if (commit == null) {
        return false;
    }

    OutputStream cos = os;
    if (!StringUtils.isEmpty(algorithm)) {
        try {
            cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os);
        } catch (CompressorException e1) {
            error(e1, repository, "{0} failed to open {1} stream", algorithm);
        }
    }
    boolean success = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
        tw.reset();
        tw.addTree(commit.getTree());
        TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
        tos.setAddPaxHeadersForNonAsciiNames(true);
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        if (!StringUtils.isEmpty(basePath)) {
            PathFilter f = PathFilter.create(basePath);
            tw.setFilter(f);
        }
        tw.setRecursive(true);
        MutableObjectId id = new MutableObjectId();
        long modified = commit.getAuthorIdent().getWhen().getTime();
        while (tw.next()) {
            FileMode mode = tw.getFileMode(0);
            if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
                continue;
            }
            tw.getObjectId(id, 0);

            ObjectLoader loader = repository.open(id);
            if (FileMode.SYMLINK == mode) {
                TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                loader.copyTo(bos);
                entry.setLinkName(bos.toString());
                entry.setModTime(modified);
                tos.putArchiveEntry(entry);
                tos.closeArchiveEntry();
            } else {
                TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
                entry.setMode(mode.getBits());
                entry.setModTime(modified);
                entry.setSize(loader.getSize());
                tos.putArchiveEntry(entry);
                loader.copyTo(tos);
                tos.closeArchiveEntry();
            }
        }
        tos.finish();
        tos.close();
        cos.close();
        success = true;
    } catch (IOException e) {
        error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
    } finally {
        tw.release();
        rw.dispose();
    }
    return success;
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorTest.java

private static void appendEntryToStream(String entryName, byte[] content, ArchiveOutputStream os)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setModTime(new Date());
    entry.setSize(content.length);//from  www  .  j a  va  2  s . c  o  m
    entry.setIds(0, 0);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(content);
    os.closeArchiveEntry();
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void compressTar(String parent, Resource source, TarArchiveOutputStream tos, int mode)
        throws IOException {
    if (source.isFile()) {
        //TarEntry entry = (source instanceof FileResource)?new TarEntry((FileResource)source):new TarEntry(parent);
        TarArchiveEntry entry = new TarArchiveEntry(parent);

        entry.setName(parent);/*www. ja v  a2s  .co  m*/

        // mode
        //100777 TODO ist das so ok?
        if (mode > 0)
            entry.setMode(mode);
        else if ((mode = source.getMode()) > 0)
            entry.setMode(mode);

        entry.setSize(source.length());
        entry.setModTime(source.lastModified());
        tos.putArchiveEntry(entry);
        try {
            IOUtil.copy(source, tos, false);
        } finally {
            tos.closeArchiveEntry();
        }
    } else if (source.isDirectory()) {
        compressTar(parent, source.listResources(), tos, mode);
    }
}

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);
    archiveEntry1.setSize(c1.length);/*  ww w. j av a  2 s  .co m*/
    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();
}