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

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

Introduction

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

Prototype

public TarArchiveEntry(byte[] headerBuf) 

Source Link

Document

Construct an entry from an archive's header bytes.

Usage

From source file:com.espringtran.compressor4j.processor.TarProcessor.java

/**
 * Compress data//w  w w . jav  a2s .  c  o  m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TarArchiveOutputStream aos = new TarArchiveOutputStream(baos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:msec.org.TarUtil.java

private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception {

    TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());

    entry.setSize(file.length());//from   ww  w  . ja v  a2  s . c  om

    taos.putArchiveEntry(entry);

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int count;
    byte data[] = new byte[BUFFERSZ];
    while ((count = bis.read(data, 0, BUFFERSZ)) != -1) {
        taos.write(data, 0, count);
    }

    bis.close();

    taos.closeArchiveEntry();
}

From source file:com.espringtran.compressor4j.processor.XzProcessor.java

/**
 * Compress data//from   w  w w .ja v a2 s  .  c o  m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XZCompressorOutputStream cos = new XZCompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.TarGzProcessor.java

/**
 * Compress data/*from  ww  w.  j a v  a  2  s .  co m*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.TarBz2Processor.java

/**
 * Compress data//  www  .j a v  a  2s. c o  m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos);
    TarArchiveOutputStream aos = new TarArchiveOutputStream(cos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            TarArchiveEntry entry = new TarArchiveEntry(binaryFile.getDesPath());
            entry.setSize(binaryFile.getActualSize());
            aos.putArchiveEntry(entry);
            aos.write(binaryFile.getData());
            aos.closeArchiveEntry();
        }
        aos.flush();
        aos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        aos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction.//from  w  ww.j a v  a 2s.  c o m
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}

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. java 2s.c  o m
    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.eleybourn.bookcatalogue.backup.tar.TarBackupWriter.java

/**
 * Save the books export file//from  w  w  w. j a v  a2  s. com
 */
@Override
public void putBooks(File books) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(new File(TarBackupContainer.BOOKS_FILE));
    entry.setModTime(books.lastModified());
    entry.setSize(books.length());
    mOutput.putArchiveEntry(entry);
    FileInputStream in = new FileInputStream(books);
    streamToArchive(in);
}

From source file:ezbake.deployer.cli.commands.SSLCertsCommand.java

@Override
public void call() throws IOException, TException {
    String[] args = globalParameters.unparsedArgs;
    minExpectedArgs(2, args, this);
    String securityId = args[0];//from ww w.  ja v  a  2  s  . com
    String filePath = args[1];

    List<ArtifactDataEntry> certs = new ArrayList<>();
    EzSecurityRegistration.Client client = null;
    ThriftClientPool pool = poolSupplier.get();
    try {
        client = pool.getClient(EzSecurityRegistrationConstants.SERVICE_NAME,
                EzSecurityRegistration.Client.class);

        AppCerts s = client.getAppCerts(
                getSecurityToken(pool.getSecurityId(EzSecurityRegistrationConstants.SERVICE_NAME)), securityId);
        for (AppCerts._Fields fields : AppCerts._Fields.values()) {
            Object o = s.getFieldValue(fields);
            if (o instanceof byte[]) {
                String fieldName = fields.getFieldName().replace("_", ".");
                TarArchiveEntry tae = new TarArchiveEntry(
                        new File(new File(SSL_CONFIG_DIRECTORY, securityId), fieldName));
                certs.add(new ArtifactDataEntry(tae, (byte[]) o));
            }
        }

        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        FileOutputStream fos = new FileOutputStream(filePath);
        GZIPOutputStream gzs = new GZIPOutputStream(fos);
        try (TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            for (ArtifactDataEntry entry : certs) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
            aos.finish();
            gzs.finish();
        } catch (ArchiveException ex) {
            throw new DeploymentException(ex.getMessage());
        } finally {
            IOUtils.closeQuietly(fos);
        }
    } finally {
        pool.returnToPool(client);
    }
}

From source file:hudson.util.io.TarArchiver.java

public void visit(File file, String relativePath) throws IOException {
    if (Functions.isWindows())
        relativePath = relativePath.replace('\\', '/');

    if (file.isDirectory())
        relativePath += '/';
    TarArchiveEntry te = new TarArchiveEntry(relativePath);
    int mode = IOUtils.mode(file);
    if (mode != -1)
        te.setMode(mode);//from  w  ww. j a va  2s .  co  m
    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++;
}