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

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

Introduction

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

Prototype

public abstract void closeArchiveEntry() throws IOException;

Source Link

Document

Closes the archive entry, writing any trailer information that may be required.

Usage

From source file:gov.nih.nci.caarray.application.translation.geosoft.GeoSoftExporterBean.java

private void generateSoftFile(Experiment experiment, String permaLinkUrl, ArchiveOutputStream zout)
        throws IOException {
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    final Writer w = new OutputStreamWriter(bout, "UTF-8");
    final PrintWriter out = new PrintWriter(w);
    GeoSoftFileWriterUtil.writeSoftFile(experiment, permaLinkUrl, out);
    out.close();// w ww  .j  av a2 s. co  m

    final ArchiveEntry ae = this.fileAccessHelper.createArchiveEntry(zout,
            experiment.getPublicIdentifier() + ".soft.txt", bout.size());
    zout.putArchiveEntry(ae);
    bout.writeTo(zout);
    zout.closeArchiveEntry();
}

From source file:com.streamsets.pipeline.lib.parser.TestCompressionInputBuilder.java

@SuppressWarnings("unchecked")
private void testArchive(String archiveType) throws Exception {
    //create an archive with multiple files, files containing multiple objects
    File dir = new File("target", UUID.randomUUID().toString());
    dir.mkdirs();/*from   w  ww  .  ja v  a2 s .  c  om*/

    OutputStream archiveOut = new FileOutputStream(new File(dir, "myArchive"));
    ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiveType,
            archiveOut);

    File inputFile;
    ArchiveEntry archiveEntry;
    FileOutputStream fileOutputStream;
    for (int i = 0; i < 5; i++) {
        String fileName = "file-" + i + ".txt";
        inputFile = new File(dir, fileName);
        fileOutputStream = new FileOutputStream(inputFile);
        IOUtils.write(("StreamSets" + i).getBytes(), fileOutputStream);
        fileOutputStream.close();
        archiveEntry = archiveOutputStream.createArchiveEntry(inputFile, fileName);
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new FileInputStream(inputFile), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
    }
    archiveOutputStream.finish();
    archiveOut.close();

    //create compression input
    FileInputStream fileInputStream = new FileInputStream(new File(dir, "myArchive"));
    CompressionDataParser.CompressionInputBuilder compressionInputBuilder = new CompressionDataParser.CompressionInputBuilder(
            Compression.ARCHIVE, "*.txt", fileInputStream, "0");
    CompressionDataParser.CompressionInput input = compressionInputBuilder.build();

    // before reading
    Assert.assertNotNull(input);

    // The default wrapped offset before reading any file
    String wrappedOffset = "{\"fileName\": \"myfile\", \"fileOffset\":\"0\"}";

    Map<String, Object> archiveInputOffset = OBJECT_MAPPER.readValue(wrappedOffset, Map.class);
    Assert.assertNotNull(archiveInputOffset);
    Assert.assertEquals("myfile", archiveInputOffset.get("fileName"));
    Assert.assertEquals("0", archiveInputOffset.get("fileOffset"));

    Assert.assertEquals("0", input.getStreamPosition(wrappedOffset));

    // read and check wrapped offset
    BufferedReader reader = new BufferedReader(new InputStreamReader(input.getNextInputStream()));
    Assert.assertEquals("StreamSets0", reader.readLine());
    wrappedOffset = input.wrapOffset("4567");
    archiveInputOffset = OBJECT_MAPPER.readValue(wrappedOffset, Map.class);
    Assert.assertNotNull(archiveInputOffset);
    Assert.assertEquals("file-0.txt", archiveInputOffset.get("fileName"));
    Assert.assertEquals("4567", archiveInputOffset.get("fileOffset"));

    String recordIdPattern = "myFile/file-0.txt";
    Assert.assertEquals(recordIdPattern, input.wrapRecordId("myFile"));

}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number.//from www. ja v  a 2  s  . c o m
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleSourceCode(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber) throws IOException {
    if (virtualVehicle.getCode() == null) {
        return;
    }

    byte[] source = virtualVehicle.getCode().getBytes("UTF-8");

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_SOURCE_JS);
    entry.setModTime(new Date());
    entry.setSize(source.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(source);
    os.closeArchiveEntry();
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number./*from   w ww  .  ja  v  a2s.  c  om*/
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleContinuation(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber) throws IOException {
    byte[] continuation = virtualVehicle.getContinuation();

    if (continuation == null) {
        return;
    }

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_CONTINUATION_JS);
    entry.setModTime(new Date());
    entry.setSize(continuation.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(continuation);
    os.closeArchiveEntry();
}

From source file:edu.mit.lib.bagit.Filler.java

private void fillArchive(File dirFile, String relBase, ArchiveOutputStream out) throws IOException {
    for (File file : dirFile.listFiles()) {
        String relPath = relBase + File.separator + file.getName();
        if (file.isDirectory()) {
            fillArchive(file, relPath, out);
        } else {/*w  ww  .  jav a 2s.  c o m*/
            TarArchiveEntry entry = new TarArchiveEntry(relPath);
            entry.setSize(file.length());
            entry.setModTime(0L);
            out.putArchiveEntry(entry);
            Files.copy(file.toPath(), out);
            out.closeArchiveEntry();
        }
    }
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param os the output stream to write to.
 * @throws IOException thrown in case of errors.
 *//*from  w  w w  .jav  a 2 s . c  o  m*/
private void writeVirtualVehicleStorageChunk(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber, List<VirtualVehicleStorage> storageChunk) throws IOException {
    for (VirtualVehicleStorage se : storageChunk) {
        logger.debug("Writing storage entry '" + se.getName() + "'");

        byte[] content = se.getContentAsByteArray();
        TarArchiveEntry entry = new TarArchiveEntry("storage/" + se.getName());
        entry.setModTime(se.getModificationTime());
        entry.setSize(content.length);
        entry.setIds(se.getId(), chunkNumber);
        entry.setNames("vvrte", "cpcc");

        os.putArchiveEntry(entry);
        os.write(content);
        os.closeArchiveEntry();
    }
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number.//  w ww. j  a  v a  2 s  .  co m
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleProperties(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber, boolean lastChunk) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Properties virtualVehicleProps = fillVirtualVehicleProps(virtualVehicle, lastChunk);
    virtualVehicleProps.store(baos, "Virtual Vehicle Properties");
    baos.close();

    byte[] propBytes = baos.toByteArray();

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_PROPERTIES);
    entry.setModTime(new Date());
    entry.setSize(propBytes.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(propBytes);
    os.closeArchiveEntry();
}

From source file:net.duckling.ddl.service.export.impl.ExportServiceImpl.java

private void writeMimetype(ArchiveOutputStream out) {
    String mimeType = getTemplate(epubPath + MIMETYPE);
    InputStream in = new ByteArrayInputStream(mimeType.getBytes());
    try {/* w w w  .j  av  a  2 s .  c  o  m*/
        out.putArchiveEntry(new ZipArchiveEntry("mimetype"));
        IOUtils.copy(in, out);
        in.close();
        out.closeArchiveEntry();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:net.duckling.ddl.service.export.impl.ExportServiceImpl.java

private void writeCover(String path, ArchiveOutputStream out) {
    try {/*w  w w.j av  a 2  s.com*/
        FileInputStream fis = new FileInputStream(epubPath + COVER);
        String entry = null != path ? path + "/cover.jpg" : "cover.jpg";
        out.putArchiveEntry(new ZipArchiveEntry(entry));
        IOUtils.copy(fis, out);
        fis.close();
        out.closeArchiveEntry();
    } catch (FileNotFoundException e) {
        LOG.error(COVER + "");
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:net.duckling.ddl.service.export.impl.ExportServiceImpl.java

private void writeTagIndex(String path, String tagname, List<Integer> rids, String tname, boolean isEpub,
        ArchiveOutputStream out, VWBContext context) {
    String html = getIndexHtml(tagname, rids, tname, isEpub, context);
    InputStream in = new ByteArrayInputStream(html.getBytes());
    try {/*from   w  w  w  .  ja v a2  s. com*/
        out.putArchiveEntry(new ZipArchiveEntry(path + "/index.html"));
        IOUtils.copy(in, out);
        in.close();
        out.closeArchiveEntry();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}