Example usage for org.apache.commons.compress.archivers.ar ArArchiveOutputStream close

List of usage examples for org.apache.commons.compress.archivers.ar ArArchiveOutputStream close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.ar ArArchiveOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Calls finish if necessary, and then closes the OutputStream

Usage

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

/**
 * Compress data//from   w w w.j  av  a  2s  .  co m
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArArchiveOutputStream aos = new ArArchiveOutputStream(baos);
    try {
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            ArArchiveEntry entry = new ArArchiveEntry(binaryFile.getDesPath(), 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:com.kloudtek.buildmagic.tools.createinstaller.deb.CreateDebTask.java

@Override
public void execute() throws BuildException {
    try {/*  w w w  . ja  v a2 s  .c  o m*/
        prepare();
        final ArArchiveOutputStream debFile = new ArArchiveOutputStream(new FileOutputStream(destfile));
        // create debian-binary
        debFile.putArchiveEntry(new ArArchiveEntry("debian-binary", DEBVERSION.length));
        debFile.write(DEBVERSION);
        debFile.closeArchiveEntry();
        // create control.tar.gz
        DataBuffer controlFile = createControlArchive();
        ArchiveHelper.write(debFile, controlFile, "control.tar.gz");
        controlFile.clear();
        // create data.tar.gz
        DataBuffer dataArchive = createDataArchive();
        ArchiveHelper.write(debFile, dataArchive, "data.tar.gz");
        dataArchive.clear();
        debFile.close();
        log("Created " + destfile.getPath());
    } catch (RuntimeException e) {
        throw new BuildException(e);
    } catch (IOException e) {
        throw new BuildException(e);
    } finally {
        dataBufferManager.clear();
    }
}

From source file:com.moss.simpledeb.core.DebWriter.java

public void write(OutputStream out) throws Exception {

    if (out == null) {
        throw new NullPointerException();
    }//  ww  w . ja v  a  2s  .co  m

    DebState state = new DebState();

    for (DebAction action : pkg.actions()) {
        action.run(state);
    }

    ArArchiveOutputStream ar = new ArArchiveOutputStream(out);

    /*
     * Version
     */

    byte[] versionData = "2.0\n".getBytes();

    ArArchiveEntry versionEntry = new ArArchiveEntry("debian-binary", versionData.length, 0, 0, 0664,
            System.currentTimeMillis());

    ar.putArchiveEntry(versionEntry);
    ar.write(versionData);
    ar.closeArchiveEntry();

    /*
     * Control
     */

    byte[] controlFileData = buildGzipTar(state.controlPaths);

    ArArchiveEntry controlEntry = new ArArchiveEntry("control.tar.gz", controlFileData.length, 0, 0, 0664,
            System.currentTimeMillis());

    ar.putArchiveEntry(controlEntry);
    ar.write(controlFileData);
    ar.closeArchiveEntry();

    /*
     * Data
     */

    byte[] contentData = buildGzipTar(state.contentPaths);

    ArArchiveEntry contentEntry = new ArArchiveEntry("data.tar.gz", contentData.length, 0, 0, 0664,
            System.currentTimeMillis());

    ar.putArchiveEntry(contentEntry);
    ar.write(contentData);
    ar.closeArchiveEntry();

    ar.close();
}

From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java

/**
 * Create debian package/* w  ww .  jav  a2s.  c o m*/
 *
 * @param p Path to write to
 */
public String build(File p) throws Exception, IOException, FileNotFoundException {
    File ftemp;
    String fileName = m_packName + ".deb";
    File filePack = new File(p, fileName);
    long sysTime = System.currentTimeMillis();
    FileOutputStream fos = new FileOutputStream(filePack);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ArArchiveOutputStream aros = new ArArchiveOutputStream(bos);
    byte[] debBin = { (byte) '2', (byte) '.', (byte) '0', (byte) 0x0a };

    // Write the file 'debian-binary'
    aros.putArchiveEntry(new ArArchiveEntry("debian-binary", 4, 0, 0, 0x81a4, sysTime));
    aros.write(debBin);
    aros.closeArchiveEntry();

    // Create control.tar.gz
    ftemp = File.createTempFile(sysTime + "", "control.tar.gz");
    doCreateControlTarGZip(ftemp);
    aros.putArchiveEntry(new ArArchiveEntry("control.tar.gz", ftemp.length(), 0, 0, 0x81a4, sysTime));
    BuilderUtil.getInstance().copyFileToOutputStream(aros, ftemp);
    aros.closeArchiveEntry();
    ftemp.delete();

    // Create data.tar.gz
    ftemp = File.createTempFile(sysTime + "", "data.tar.gz");
    doAddFilesToTarGZip(ftemp);
    aros.putArchiveEntry(new ArArchiveEntry("data.tar.gz", ftemp.length(), 0, 0, 0x81a4, sysTime));
    BuilderUtil.getInstance().copyFileToOutputStream(aros, ftemp);
    aros.closeArchiveEntry();
    ftemp.delete();

    // Done
    aros.close();
    bos.close();
    fos.close();

    // Return absolute path
    return filePack.getName();
}

From source file:org.vafer.jdeb.ar.ArOutputStreamTestCase.java

public void testWrite() throws Exception {
    final File out1 = File.createTempFile("jdeb", ".ar");

    final ArArchiveOutputStream os = new ArArchiveOutputStream(new FileOutputStream(out1));
    os.putArchiveEntry(new ArArchiveEntry("data", 4));
    os.write("data".getBytes());
    os.closeArchiveEntry();/*  w  w  w. ja v  a 2  s .  c  o  m*/
    os.close();

    assertTrue(out1.delete());
}

From source file:org.vafer.jdeb.DebMaker.java

/**
 * Create the debian archive with from the provided control files and data producers.
 *
 * @param pControlFiles/*from w  w  w.j  ava 2 s  .c  o  m*/
 * @param pData
 * @param deb
 * @param compression   the compression method used for the data file
 * @return BinaryPackageControlFile
 * @throws PackagingException
 */
public BinaryPackageControlFile createDeb(Compression compression) throws PackagingException {
    File tempData = null;
    File tempControl = null;

    try {
        tempData = File.createTempFile("deb", "data");
        tempControl = File.createTempFile("deb", "control");

        console.info("Building data");
        DataBuilder dataBuilder = new DataBuilder(console);
        StringBuilder md5s = new StringBuilder();
        BigInteger size = dataBuilder.buildData(dataProducers, tempData, md5s, compression);

        console.info("Building control");
        ControlBuilder controlBuilder = new ControlBuilder(console, variableResolver);
        BinaryPackageControlFile packageControlFile = controlBuilder
                .createPackageControlFile(new File(control, "control"), size);
        if (packageControlFile.get("Package") == null) {
            packageControlFile.set("Package", packageName);
        }
        if (packageControlFile.get("Depends") == null) {
            packageControlFile.set("Depends", depends);
        }
        if (packageControlFile.get("Section") == null) {
            packageControlFile.set("Section", section);
        }
        if (packageControlFile.get("Description") == null) {
            packageControlFile.set("Description", description);
        }
        if (packageControlFile.get("Homepage") == null) {
            packageControlFile.set("Homepage", homepage);
        }

        controlBuilder.buildControl(packageControlFile, control.listFiles(), md5s, tempControl);

        if (!packageControlFile.isValid()) {
            throw new PackagingException("Control file fields are invalid " + packageControlFile.invalidFields()
                    + ". The following fields are mandatory: " + packageControlFile.getMandatoryFields()
                    + ". Please check your pom.xml/build.xml and your control file.");
        }

        deb.getParentFile().mkdirs();

        ArArchiveOutputStream ar = new ArArchiveOutputStream(new FileOutputStream(deb));

        addTo(ar, "debian-binary", "2.0\n");
        addTo(ar, "control.tar.gz", tempControl);
        addTo(ar, "data.tar" + compression.getExtension(), tempData);

        ar.close();

        return packageControlFile;

    } catch (Exception e) {
        throw new PackagingException("Could not create deb package", e);
    } finally {
        if (tempData != null) {
            if (!tempData.delete()) {
                console.warn("Could not delete the temporary file " + tempData);
            }
        }
        if (tempControl != null) {
            if (!tempControl.delete()) {
                console.warn("Could not delete the temporary file " + tempControl);
            }
        }
    }
}

From source file:org.vafer.jdeb.Processor.java

/**
 * Create the debian archive with from the provided control files and data producers.
 *
 * @param pControlFiles/*from   www. j  a  v a  2  s  .  co m*/
 * @param pData
 * @param pOutput
 * @param compression the compression method used for the data file (gzip, bzip2 or anything else for no compression)
 * @return PackageDescriptor
 * @throws PackagingException
 */
public PackageDescriptor createDeb(final File[] pControlFiles, final DataProducer[] pData, final File pOutput,
        String compression) throws PackagingException, InvalidDescriptorException {

    File tempData = null;
    File tempControl = null;

    try {
        tempData = File.createTempFile("deb", "data");
        tempControl = File.createTempFile("deb", "control");

        console.println("Building data");
        final StringBuffer md5s = new StringBuffer();
        final BigInteger size = buildData(pData, tempData, md5s, compression);

        console.println("Building control");
        final PackageDescriptor packageDescriptor = buildControl(pControlFiles, size, md5s, tempControl);

        if (!packageDescriptor.isValid()) {
            throw new InvalidDescriptorException(packageDescriptor);
        }

        pOutput.getParentFile().mkdirs();
        final InformationOutputStream md5output = new InformationOutputStream(new FileOutputStream(pOutput),
                MessageDigest.getInstance("MD5"));
        //Add chain of filters in order to calculate sha1 and sha256 for 1.8 format
        final InformationOutputStream sha1output = new InformationOutputStream(md5output,
                MessageDigest.getInstance("SHA1"));
        final InformationOutputStream sha256output = new InformationOutputStream(sha1output,
                MessageDigest.getInstance("SHA-256"));

        final ArArchiveOutputStream ar = new ArArchiveOutputStream(sha256output);

        addTo(ar, "debian-binary", "2.0\n");
        addTo(ar, "control.tar.gz", tempControl);
        addTo(ar, "data.tar" + getExtension(compression), tempData);

        ar.close();

        // intermediate values
        packageDescriptor.set("MD5", md5output.getHexDigest());
        packageDescriptor.set("SHA1", sha1output.getHexDigest());
        packageDescriptor.set("SHA256", sha256output.getHexDigest());
        packageDescriptor.set("Size", "" + md5output.getSize());
        packageDescriptor.set("File", pOutput.getName());

        return packageDescriptor;

    } catch (InvalidDescriptorException e) {
        throw e;
    } catch (Exception e) {
        throw new PackagingException("Could not create deb package", e);
    } finally {
        if (tempData != null) {
            if (!tempData.delete()) {
                throw new PackagingException("Could not delete " + tempData);
            }
        }
        if (tempControl != null) {
            if (!tempControl.delete()) {
                throw new PackagingException("Could not delete " + tempControl);
            }
        }
    }
}