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

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

Introduction

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

Prototype

public TarArchiveOutputStream(OutputStream os, int blockSize, int recordSize) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

@Override
public void encrypt(String fileName, int size, InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException {
    final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream, 512,
            ENCODING);/*  w  ww. ja va  2 s . c om*/

    final BufferedBlockCipher cipher = createCipher(tarArchiveOutputStream);

    startEncryptedFile(fileName, size, tarArchiveOutputStream, cipher);

    //Setup cipher output stream, has to protect from close as the cipher stream must close but the tar cannot get closed yet
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new CloseShieldOutputStream(tarArchiveOutputStream), cipher);

    //Setup digester
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Perform streaming encryption and hashing of the file
    IOUtils.copy(inputStream, new TeeOutputStream(cipherOutputStream, digestOutputStream));
    cipherOutputStream.close();

    tarArchiveOutputStream.closeArchiveEntry();

    //Capture the hash code of the encrypted file
    digestOutputStream.close();
    final byte[] hashBytes = digestOutputStream.getDigest();

    this.writeHashfile(tarArchiveOutputStream, hashBytes);

    //Close the TAR stream, nothing else should be written to it
    tarArchiveOutputStream.close();
}