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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:com.mulesoft.jockey.maven.GenerateMojo.java

private void createZip(File distDir) throws MojoExecutionException {
    File output = new File(buildDirectory, distributionName + ".zip");
    try {//w  w w  .j av  a2  s.  co  m
        final OutputStream out = new FileOutputStream(output);
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);

        copyArchiveFile(distDir, os, true);

        os.finish();

        os.close();
        out.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create zip file.", e);
    } catch (ArchiveException e) {
        throw new MojoExecutionException("Could not create zip file.", e);
    }
    projectHelper.attachArtifact(project, "zip", "", output);

}

From source file:com.surevine.gateway.scm.gatewayclient.GatewayPackage.java

void createTar(final Path tarPath, final Path... paths) throws IOException, ArchiveException {
    ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
            Files.newOutputStream(tarPath));
    try {//from   www.j  a va2s  .c o m
        for (Path path : paths) {
            TarArchiveEntry entry = new TarArchiveEntry(path.toFile());
            entry.setName(path.getFileName().toString());
            os.putArchiveEntry(entry);
            Files.copy(path, os);
            os.closeArchiveEntry();
        }
    } finally {
        os.close();
    }
}

From source file:fr.gael.ccsds.sip.archive.TarArchiveManager.java

/**
 * Produces TAR archive//from   w ww .j  a v  a2 s  . com
 */
@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}

From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java

@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();
    final CompressorStreamFactory csf = new CompressorStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // copy the existing entries
        ArchiveEntry nextEntry;/*from   ww  w.j av a  2s  . c om*/
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}

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

@Test(dataProvider = "unknownEntryTypeDataProvider", expectedExceptions = {
        IOException.class }, expectedExceptionsMessageRegExp = "Can not store unknown type of entry .*")
public void shouldThrowIOEOnUnknownEntryType(String entryName, byte[] content)
        throws ArchiveException, IOException {
    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArchiveOutputStream os = factory.createArchiveOutputStream("tar", baos);

    appendEntryToStream(entryName, content, os);
    os.close();
    baos.close();/*  www. j  av  a2  s .  c  om*/

    byte[] chunk = baos.toByteArray();

    migrator.storeChunk(new ByteArrayInputStream(chunk));
}

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

@Test(dataProvider = "unknownVirtualVehicleEntryDataProvider", expectedExceptions = {
        IOException.class }, expectedExceptionsMessageRegExp = "Can not store unknown virtual vehicle entry .*")
public void shouldThrowIOEOnUnknownVirtualVehicleEntry(String entryName, byte[] content)
        throws ArchiveException, IOException {
    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArchiveOutputStream os = factory.createArchiveOutputStream("tar", baos);

    appendEntryToStream(entryName, content, os);
    os.close();
    baos.close();//ww w  . j  a  v  a2  s.co  m

    byte[] chunk = baos.toByteArray();

    migrator.storeChunk(new ByteArrayInputStream(chunk));
}

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

/**
 * {@inheritDoc}//from   ww  w .j  a v  a 2s .  c om
 */
@Override
public byte[] findChunk(VirtualVehicle virtualVehicle, String lastStorageName, int chunkNumber)
        throws IOException, ArchiveException {
    String name = lastStorageName != null && lastStorageName.startsWith("storage/")
            ? lastStorageName.substring(8)
            : "";

    List<VirtualVehicleStorage> storageChunk = vvRepository
            .findStorageItemsByVirtualVehicle(virtualVehicle.getId(), name, chunkSize);

    boolean lastChunk = storageChunk.size() == 0 || storageChunk.size() < chunkSize;

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", baos);

    writeVirtualVehicleProperties(virtualVehicle, outStream, chunkNumber, lastChunk);
    if (chunkNumber == 0) {
        writeVirtualVehicleSourceCode(virtualVehicle, outStream, chunkNumber);
        writeVirtualVehicleContinuation(virtualVehicle, outStream, chunkNumber);
    }

    writeVirtualVehicleStorageChunk(virtualVehicle, outStream, chunkNumber, storageChunk);

    outStream.close();
    baos.close();

    if (lastChunk) {
        virtualVehicle.setState(VirtualVehicleState.MIGRATION_COMPLETED_SND);
    }

    return baos.toByteArray();
}

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

@Test
public void shouldWriteTarFile() throws IOException, ArchiveException {
    byte[] c1 = "content1\n".getBytes("UTF-8");
    byte[] c2 = "content2 text\n".getBytes("UTF-8");

    Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L));
    Date t2 = new Date(t1.getTime() - 30000);

    FileOutputStream fos = new FileOutputStream("bugger1.tar");

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos);

    TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1");
    archiveEntry1.setModTime(t1);/*from ww  w .  j  av a  2 s  .  co m*/
    archiveEntry1.setSize(c1.length);
    archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE);
    archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE);

    outStream.putArchiveEntry(archiveEntry1);
    outStream.write(c1);
    outStream.closeArchiveEntry();

    TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2");
    archiveEntry2.setModTime(t2);
    archiveEntry2.setSize(c2.length);
    archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO);
    archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO);

    outStream.putArchiveEntry(archiveEntry2);
    outStream.write(c2);
    outStream.closeArchiveEntry();

    outStream.close();

    FileInputStream fis = new FileInputStream("bugger1.tar");
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis);

    TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry1.getModTime()).isEqualTo(t1);
    assertThat(entry1.getSize()).isEqualTo(c1.length);
    assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE);
    assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE);
    assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE);
    assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE);
    ByteArrayOutputStream b1 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b1);
    b1.close();
    assertThat(b1.toByteArray().length).isEqualTo(c1.length);
    assertThat(b1.toByteArray()).isEqualTo(c1);

    TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry2.getModTime()).isEqualTo(t2);
    assertThat(entry2.getSize()).isEqualTo(c2.length);
    assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO);
    assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO);
    assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO);
    assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b2);
    b2.close();
    assertThat(b2.toByteArray().length).isEqualTo(c2.length);
    assertThat(b2.toByteArray()).isEqualTo(c2);

    TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry3).isNull();

    inStream.close();
}

From source file:com.iorga.webappwatcher.EventLogManager.java

public void writeEventLogsToHttpServletResponse(final HttpServletResponse httpResponse,
        final Iterable<String> fileNames) throws IOException {
    // First, we check if the given paths are in the log path
    final Set<String> eventLogs = Sets.newHashSet(listEventLogsNameInThePath());
    ArchiveOutputStream outputStream = null;
    try {/*from w w w.j a v a  2 s  .  c  o  m*/
        for (final String fileName : fileNames) {
            if (eventLogs.contains(fileName) && new File(getLogPathDirectory(), fileName).exists()) {
                outputStream = writeEventLogToHttpServletResponse(httpResponse, fileName, outputStream);
            }
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }

    if (outputStream == null) {
        // No file were appended
        httpResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
        final PrintWriter writer = httpResponse.getWriter();
        writer.write("No log available at the moment.");
    }
}

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

private void writeZip2Client(VWBContext context, String tname, Map<String, List<Tag>> tagMap,
        HttpServletResponse resp) {// w  w w .  ja  v a2 s .c  o m
    Team team = teamService.getTeamByName(tname);
    setResponseHeader(tname + ".zip", resp);
    ArchiveOutputStream out = getArchiveOutputStream(resp);
    writeCss(tname, out);
    Map<String, String> id2Title = new HashMap<String, String>();
    writeForTag(tname, tagMap, team.getId(), context, out, id2Title, null, false);
    writeCatalog(tname, id2Title, out);
    writeOverview(tname, tagMap, out, context, false);
    try {
        out.close();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}