Example usage for org.apache.commons.compress.archivers ArchiveOutputStream write

List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream write

Introduction

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

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this output stream.

Usage

From source file:big.BigZip.java

/**
 * Requires an InputStream, it will calculate the SHA1 checksum at the same
 * time that it writes data onto the big file. The input stream is expected
 * to be closed outside of this method./*w  ww. j a  v a  2  s  . co m*/
 * @param stream
 * @param filePathToWriteInTextLine
 * @throws java.io.IOException 
 */
public void quickWriteStreamStandalone(final InputStream stream, final String filePathToWriteInTextLine)
        throws Exception {
    // declare
    ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream();
    ByteArrayInputStream byteInput = null;
    // Create Archive Output Stream that attaches File Output Stream / and specifies type of compression
    ArchiveOutputStream logical_zip = new ArchiveStreamFactory()
            .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream);
    // Create Archive entry - write header information
    ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(filePathToWriteInTextLine);
    logical_zip.putArchiveEntry(zipArchiveEntry);
    // prepare the SHA1 signature generation
    final MessageDigest hash = MessageDigest.getInstance("SHA1");

    // Copy input file
    byte[] buffer = new byte[16384];
    int length;

    // decompress from the original zip file, compress to our zip format
    // calculate the SHA1 signature on the same loop to save resource
    while ((length = stream.read(buffer)) > 0) {
        logical_zip.write(buffer, 0, length);
        hash.update(buffer, 0, length);
    }

    // compute the file signature
    byte[] digest = hash.digest();
    final String SHA1 = utils.hashing.checksum.convertHash(digest);

    // close the zip related objects
    logical_zip.closeArchiveEntry();
    logical_zip.finish();
    logical_zip.flush();
    logical_zip.close();
    logical_zip = null;

    // define the line that will be written on the index file
    final String line = "\n".concat(utils.files.getPrettyFileSize(currentPosition)).concat(" ").concat(SHA1)
            .concat(" ").concat(filePathToWriteInTextLine);

    // get the bytes
    byteInput = new ByteArrayInputStream(outputZipStream.toByteArray());
    int counter = 0;

    // add the magic number to this file block
    outputStream.write(magicSignature.getBytes());
    // now copy the whole file into the BIG archive
    while ((length = byteInput.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
        counter += length;
    }
    // write a new line in our index file
    writerFileIndex.write(line);
    // increase the position counter
    currentPosition += counter + magicSignature.length();
    // close the streams that were created
    byteInput.close();
    outputZipStream.close();
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

/**
 * Packs the files and directories of a passed {@link File} to a passed
 * {@link ArchiveOutputStream}.// w  w  w  .j av a  2s.co m
 *
 * @throws IOException
 */
private void packRepository(File source, ArchiveOutputStream aos) throws IOException {
    int bufferSize = 2048;
    byte[] readBuffer = new byte[bufferSize];
    int bytesIn = 0;
    File[] files = source.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return !pathname.getName().equals(Constants.DOT_GIT);
        }
    });
    for (File file : files) {
        if (file.isDirectory()) {
            ArchiveEntry ae = aos.createArchiveEntry(file, getRelativePath(file.getAbsolutePath()));
            aos.putArchiveEntry(ae);
            aos.closeArchiveEntry();
            packRepository(file, aos);
        } else {
            FileInputStream fis = new FileInputStream(file);
            ArchiveEntry ae = aos.createArchiveEntry(file, getRelativePath(file.getAbsolutePath()));
            aos.putArchiveEntry(ae);
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                aos.write(readBuffer, 0, bytesIn);
            }
            aos.closeArchiveEntry();
            fis.close();
        }
    }
}