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

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

Introduction

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

Prototype

public abstract void putArchiveEntry(ArchiveEntry entry) throws IOException;

Source Link

Document

Writes the headers for an archive entry to the output stream.

Usage

From source file:jetbrains.exodus.util.CompressBackupUtil.java

/**
 * Adds the file to the tar archive represented by output stream. It's caller's responsibility to close output stream
 * properly.//from  ww w. j av a  2 s. c om
 *
 * @param out           target archive.
 * @param pathInArchive relative path in archive. It will lead the name of the file in the archive.
 * @param source        file to be added.
 * @param fileSize      size of the file (which is known in most cases).
 * @throws IOException in case of any issues with underlying store.
 */
public static void archiveFile(@NotNull final ArchiveOutputStream out, @NotNull final String pathInArchive,
        @NotNull final File source, final long fileSize) throws IOException {
    if (!source.isFile()) {
        throw new IllegalArgumentException("Provided source is not a file: " + source.getAbsolutePath());
    }
    //noinspection ChainOfInstanceofChecks
    if (out instanceof TarArchiveOutputStream) {
        final TarArchiveEntry entry = new TarArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setModTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else if (out instanceof ZipArchiveOutputStream) {
        final ZipArchiveEntry entry = new ZipArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else {
        throw new IOException("Unknown archive output stream");
    }
    try (InputStream input = new FileInputStream(source)) {
        IOUtil.copyStreams(input, fileSize, out, IOUtil.BUFFER_ALLOCATOR);
    }
    out.closeArchiveEntry();
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.CompressionTools.java

private static void compressArchive(final Path pathToCompress, final ArchiveOutputStream archiveOutputStream,
        final ArchiveEntryFactory archiveEntryFactory, final CompressionType compressionType,
        final BuildListener listener) throws IOException {
    final List<File> files = addFilesToCompress(pathToCompress, listener);

    LoggingHelper.log(listener, "Compressing directory '%s' as a '%s' archive", pathToCompress.toString(),
            compressionType.name());//from w  w  w  . j av  a2 s  . co  m

    for (final File file : files) {
        final String newTarFileName = pathToCompress.relativize(file.toPath()).toString();
        final ArchiveEntry archiveEntry = archiveEntryFactory.create(file, newTarFileName);
        archiveOutputStream.putArchiveEntry(archiveEntry);

        try (final FileInputStream fileInputStream = new FileInputStream(file)) {
            IOUtils.copy(fileInputStream, archiveOutputStream);
        }

        archiveOutputStream.closeArchiveEntry();
    }
}

From source file:com.mirth.connect.util.ArchiveUtils.java

/**
 * Recursively copies folders/files into the given ArchiveOutputStream.
 *///from  ww w.  java  2s  . c  om
private static void createFolderArchive(File folder, ArchiveOutputStream archiveOutputStream, String rootFolder)
        throws CompressException {
    byte[] buffer = new byte[BUFFER_SIZE];

    for (File file : folder.listFiles()) {
        if (file.isDirectory()) {
            createFolderArchive(file, archiveOutputStream, rootFolder);
        } else {
            try {
                // extract/remove the rootFolder from the file's absolute path before adding it to the archive
                String entryName = file.getAbsolutePath();

                if (entryName.substring(0, rootFolder.length()).equals(rootFolder)) {
                    entryName = entryName.substring(rootFolder.length());
                }

                archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, entryName));
                InputStream inputStream = new FileInputStream(file);

                logger.debug("Adding \"" + entryName + "\" to archive");

                try {
                    IOUtils.copyLarge(inputStream, archiveOutputStream, buffer);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                    archiveOutputStream.closeArchiveEntry();
                }
            } catch (Exception e) {
                throw new CompressException(e);
            }
        }
    }
}

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

private static void appendEntryToStream(String entryName, byte[] content, ArchiveOutputStream os)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setModTime(new Date());
    entry.setSize(content.length);/*from w  w w. j  a  v  a  2 s .  c o  m*/
    entry.setIds(0, 0);
    entry.setNames("vvrte", "cpcc");

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

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

private static void copyEntry(ArchiveOutputStream archiveOutputStream, ArchiveInputStream ais,
        ArchiveEntry tempEntry, ArchiveEntryWrapper newEntry) throws IOException {
    byte[] data = null;
    if (!tempEntry.isDirectory()) {
        try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
            IOUtils.copy(ais, byteStream);
            data = byteStream.toByteArray();
        }/*from ww  w.  j  a  v a 2s  .c o  m*/

        // change line breaks
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            if (LineEnds.convert(new ByteArrayInputStream(data), stream, LineEnds.STYLE_UNIX)) {
                data = stream.toByteArray();
            }
        } catch (BinaryDataException ignored) {
            // ignore binary data
        }
    }

    if (data != null) {
        newEntry.setSize(data.length);
    }

    archiveOutputStream.putArchiveEntry(newEntry.getArchiveEntry());

    if (data != null) {
        IOUtils.copy(new ByteArrayInputStream(data), archiveOutputStream);
    }

    archiveOutputStream.closeArchiveEntry();
}

From source file:com.openkm.misc.ZipTest.java

public void testApache() throws IOException, ArchiveException {
    log.debug("testApache()");
    File zip = File.createTempFile("apache_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
    aos.putArchiveEntry(new ZipArchiveEntry("coeta"));
    aos.closeArchiveEntry();/*from w  w w  . j  av  a  2 s.  co m*/
    aos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
    ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
    assertEquals(zae.getName(), "coeta");
    ais.close();
}

From source file:com.fizzed.stork.deploy.Archive.java

static public void packEntry(ArchiveOutputStream aos, Path dirOrFile, String base, boolean appendName)
        throws IOException {
    String entryName = base;//from ww w .  java  2 s. co m

    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + dirOrFile.getFileName();
            } else {
                entryName += dirOrFile.getFileName();
            }
        } else {
            entryName += dirOrFile.getFileName();
        }
    }

    ArchiveEntry entry = aos.createArchiveEntry(dirOrFile.toFile(), entryName);

    if (Files.isRegularFile(dirOrFile)) {
        if (entry instanceof TarArchiveEntry && Files.isExecutable(dirOrFile)) {
            // -rwxr-xr-x
            ((TarArchiveEntry) entry).setMode(493);
        } else {
            // keep default mode
        }
    }

    aos.putArchiveEntry(entry);

    if (Files.isRegularFile(dirOrFile)) {
        Files.copy(dirOrFile, aos);
        aos.closeArchiveEntry();
    } else {
        aos.closeArchiveEntry();
        List<Path> children = Files.list(dirOrFile).collect(Collectors.toList());
        if (children != null) {
            for (Path childFile : children) {
                packEntry(aos, childFile, entryName + "/", true);
            }
        }
    }
}

From source file:algorithm.ZipPackaging.java

private void archiveFile(ArchiveOutputStream archiveOutputStream, File file)
        throws IOException, FileNotFoundException {
    archiveOutputStream.putArchiveEntry(new ZipArchiveEntry(file.getName()));
    FileInputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
    inputStream.close();/*  w  w w.  j  a v a  2 s. c o m*/
}

From source file:com.impetus.ankush.agent.utils.ZipFiles.java

/**
 * Zip file./*from ww  w.j  a  va  2  s.  c  o  m*/
 *
 * @param filePath the file path
 * @return the string
 */
public String zipFile(String filePath) {
    try {
        /* Create Output Stream that will have final zip files */
        OutputStream zipOutput = new FileOutputStream(new File(filePath + ".zip"));
        /*
         * Create Archive Output Stream that attaches File Output Stream / and
         * specifies type of compression
         */
        ArchiveOutputStream logicalZip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipOutput);
        /* Create Archieve entry - write header information */
        logicalZip.putArchiveEntry(new ZipArchiveEntry(FilenameUtils.getName(filePath)));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(new File(filePath)), logicalZip);
        /* Close Archieve entry, write trailer information */
        logicalZip.closeArchiveEntry();

        /* Finish addition of entries to the file */
        logicalZip.finish();
        /* Close output stream, our files are zipped */
        zipOutput.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }
    return filePath + ".zip";
}

From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java

/**
 * Add a file to an archive.//from   w w w  .  j a  v  a 2s.  co  m
 * 
 * @param f the file to add.
 * @param zout the archive stream. {@link TarArchiveOutputStream} or {@link ZipArchiveOutputStream}
 * @throws IOException when writeing to the stream fails.
 */
public void addFileToArchive(CaArrayFile f, ArchiveOutputStream zout) throws IOException {
    final ArchiveEntry ae = createArchiveEntry(zout, f.getName(), f.getUncompressedSize());
    zout.putArchiveEntry(ae);
    this.dataStorageFacade.copyDataToStream(f.getDataHandle(), zout);
    zout.closeArchiveEntry();
}