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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

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

/**
 * Read from compressed file//  w  w w . j ava  2  s.co  m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    ArArchiveInputStream ais = new ArArchiveInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ArArchiveEntry entry = ais.getNextArEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextArEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

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

public void testRead() throws Exception {
    final File archive = new File(getClass().getResource("data.ar").toURI());

    final ArArchiveInputStream ar = new ArArchiveInputStream(new FileInputStream(archive));
    final ArArchiveEntry entry1 = ar.getNextArEntry();

    assertEquals("data.tgz", entry1.getName());
    assertEquals(148, entry1.getLength());

    for (int i = 0; i < entry1.getLength(); i++) {
        ar.read();// w  w w  .j av a  2 s . c o m
    }

    final ArArchiveEntry entry2 = ar.getNextArEntry();

    assertNull(entry2);

    ar.close();
}

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

public void testCreation() throws Exception {

    final Processor processor = new Processor(new Console() {
        public void println(String s) {
        }//from  w  ww. ja v  a2 s. c  om
    }, null);

    final File control = new File(getClass().getResource("deb/control/control").toURI());
    final File archive1 = new File(getClass().getResource("deb/data.tgz").toURI());
    final File archive2 = new File(getClass().getResource("deb/data.tar.bz2").toURI());
    final File directory = new File(getClass().getResource("deb/data").toURI());

    final DataProducer[] data = new DataProducer[] { new DataProducerArchive(archive1, null, null, null),
            new DataProducerArchive(archive2, null, null, null),
            new DataProducerDirectory(directory, null, new String[] { "**/.svn/**" }, null) };

    final File deb = File.createTempFile("jdeb", ".deb");

    final PackageDescriptor packageDescriptor = processor.createDeb(new File[] { control }, data, deb, "gzip");

    assertTrue(packageDescriptor.isValid());

    final Set filesInDeb = new HashSet();

    final ArArchiveInputStream ar = new ArArchiveInputStream(new FileInputStream(deb));
    while (true) {
        final ArArchiveEntry arEntry = ar.getNextArEntry();
        if (arEntry == null) {
            break;
        }

        if ("data.tar.gz".equals(arEntry.getName())) {

            final TarInputStream tar = new TarInputStream(new GZIPInputStream(new NonClosingInputStream(ar)));

            while (true) {
                final TarEntry tarEntry = tar.getNextEntry();
                if (tarEntry == null) {
                    break;
                }

                filesInDeb.add(tarEntry.getName());
            }

            tar.close();
            break;
        }
        for (int i = 0; i < arEntry.getLength(); i++) {
            ar.read();
        }
    }

    ar.close();

    assertTrue("testfile wasn't found in the package", filesInDeb.contains("./test/testfile"));
    assertTrue("testfile2 wasn't found in the package", filesInDeb.contains("./test/testfile2"));
    assertTrue("testfile3 wasn't found in the package", filesInDeb.contains("./test/testfile3"));

    assertTrue("Cannot delete the file " + deb, deb.delete());
}